Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/install/PackageManager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,10 @@ pub struct CatalogUpdateInfo {
pub dep_name: Box<[u8]>,
pub original_version_literal: Box<[u8]>,
pub is_alias: bool,
/// The literal `bun update` resolved, computed once in
/// `Lockfile::preprocess_updating_catalogs` and applied to both
/// `lockfile.catalogs` and package.json. `None` keeps the original.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub resolved_version_literal: Option<Box<[u8]>>,
}

#[derive(Default)]
Expand Down
117 changes: 7 additions & 110 deletions src/install/PackageManager/PackageJSONEditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@
dep_name: Box::from(key_str),
original_version_literal: Box::from(version_literal),
is_alias: alias_at_index.is_some(),
resolved_version_literal: None,
});

if update_to_latest {
Expand Down Expand Up @@ -670,10 +671,10 @@

/// Writes resolved versions back into the recorded catalog entries, preserving
/// the original pin style. Unresolved entries are restored. Returns `changed`.
pub(crate) fn edit_catalogs_after_update(
manager: &mut PackageManager,
root_package_json: &Expr,
options: EditOptions,
_options: EditOptions,

Check warning on line 677 in src/install/PackageManager/PackageJSONEditor.rs

View check run for this annotation

Claude / Claude Code Review

Dead _options parameter in edit_catalogs_after_update

The `_options: EditOptions` parameter is now dead — this PR moved its only consumer (`options.exact_versions`) into `Lockfile::preprocess_updating_catalogs`/`format_catalog_update_literal`, and the rename to `_options` silences the warning rather than removing it. REVIEW.md's "Delete dead code in the same PR that makes it dead" explicitly names "parameters discarded in the body" as required scope, so drop the parameter here and the `EditOptions { exact_versions: ..., .. }` argument at both call
Comment thread
robobun marked this conversation as resolved.
Resolved
) -> Result<bool, bun_alloc::AllocError> {
// see note in `edit_update_no_args` — always avoid the store
let _guard = ExprDisabler::scope();
Expand All @@ -684,113 +685,6 @@
}

let arena = &manager.ast_arena;
let lockfile = &*manager.lockfile;
let string_buf = lockfile.buffers.string_bytes.as_slice();
let package_resolutions = lockfile.packages.items_resolution();

let mut new_literals: Vec<Option<Vec<u8>>> = vec![None; infos.len()];
debug_assert_eq!(
lockfile.buffers.dependencies.len(),
lockfile.buffers.resolutions.len()
);
for (dep, &package_id) in lockfile
.buffers
.dependencies
.iter()
.zip(lockfile.buffers.resolutions.iter())
{
if dep.version.tag != dependency::Tag::Catalog {
continue;
}
if package_id == INVALID_PACKAGE_ID {
continue;
}

let dep_name = dep.name.slice(string_buf);
let catalog_name = dep.version.catalog().slice(string_buf);
let Some(index) = infos.iter().position(|info| {
strings::eql_long(&info.dep_name, dep_name, true)
&& strings::eql_long(&info.catalog_name, catalog_name, true)
}) else {
continue;
};
if new_literals[index].is_some() {
continue;
}

let resolution = &package_resolutions[package_id as usize];
if resolution.tag != resolution::Tag::Npm {
continue;
}

if !manager.options.do_.contains(Do::UPDATE_TO_LATEST) {
// plain `bun update` does not move an exact pin (matches direct-dep behavior)
let resolved_version = lockfile
.resolve_catalog_dependency(dep)
.unwrap_or_else(|| dep.version.clone());
if let Some(npm_version) = resolved_version.try_npm() {
if npm_version.version.is_exact() {
continue;
}
}
}

let info = &infos[index];
let version_fmt = resolution.npm().version.fmt(string_buf);
let new_version: Vec<u8> = 'new_version: {
if options.exact_versions {
let mut v = Vec::new();
write!(&mut v, "{}", version_fmt).expect("infallible: in-memory write");
break 'new_version v;
}

let version_literal: &[u8] = 'version_literal: {
if !info.is_alias {
break 'version_literal &info.original_version_literal;
}
if let Some(at_index) =
strings::last_index_of_char(&info.original_version_literal, b'@')
{
break 'version_literal &info.original_version_literal[at_index + 1..];
}
&info.original_version_literal
};

let pinned_version = semver::Version::which_version_is_pinned(version_literal);
let mut v = Vec::new();
match pinned_version {
semver::PinnedVersion::Patch => {
write!(&mut v, "{}", version_fmt).expect("infallible: in-memory write")
}
semver::PinnedVersion::Minor => {
write!(&mut v, "~{}", version_fmt).expect("infallible: in-memory write")
}
semver::PinnedVersion::Major => {
write!(&mut v, "^{}", version_fmt).expect("infallible: in-memory write")
}
}
v
};

new_literals[index] = Some(if info.is_alias {
let dep_literal = &info.original_version_literal;
if let Some(at_index) = strings::last_index_of_char(dep_literal, b'@') {
let mut v = Vec::new();
write!(
&mut v,
"{}@{}",
bstr::BStr::new(&dep_literal[0..at_index]),
bstr::BStr::new(&new_version)
)
.expect("infallible: in-memory write");
v
} else {
new_version
}
} else {
new_version
});
}

let mut changed = false;
for_each_catalog_object(root_package_json, |catalog_name, mut catalog_expr| {
Expand Down Expand Up @@ -820,9 +714,12 @@
};

let info = &infos[index];
let new_literal: &[u8] = match &new_literals[index] {
// `Lockfile::preprocess_updating_catalogs` resolved this literal
// together with the lockfile's catalog map, so package.json and
// bun.lock always agree. `None` restores the original (may still
// be the temporary `latest`).
Comment thread
robobun marked this conversation as resolved.
Resolved
let new_literal: &[u8] = match &info.resolved_version_literal {
Some(v) => arena_str(arena, v),
// unresolved: restore the original (may still be the temporary `latest`)
None => arena_dup(arena, &info.original_version_literal),
};

Expand Down
207 changes: 206 additions & 1 deletion src/install/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
use crate::migration;
use crate::package_manager::WorkspaceFilter;
use crate::package_manager_real::{
Options as PackageManagerOptions, options::LogLevel, populate_manifest_cache,
CatalogUpdateInfo, Options as PackageManagerOptions, options::Do, options::LogLevel,
populate_manifest_cache,
};
use crate::resolution_real::{self as resolution, Resolution};
use crate::string_builder;
Expand Down Expand Up @@ -854,6 +855,135 @@
Ok(())
}

/// `bun update` with catalogs records each catalog entry it edits in
/// `manager.updating_catalogs`; with `--latest` the entry is rewritten to
/// `latest` before resolution. Compute the resolved literal for each entry
/// and write it into `old.catalogs` so the saved lockfile matches the
/// package.json `edit_catalogs_after_update` will write.
Comment thread
robobun marked this conversation as resolved.
Resolved
fn preprocess_updating_catalogs(
old: &mut Lockfile,
manager: &mut PackageManager,
exact_versions: bool,
) -> Result<(), BunError> {
let update_to_latest = manager.options.do_.contains(Do::UPDATE_TO_LATEST);
let mut infos = core::mem::take(&mut manager.updating_catalogs);

{
let string_buf = old.buffers.string_bytes.as_slice();
let package_resolutions = old.packages.items_resolution();
let packages_len = old.packages.len();
debug_assert_eq!(
old.buffers.dependencies.len(),
old.buffers.resolutions.len()
);
for (dep, &package_id) in old
.buffers
.dependencies
.iter()
.zip(old.buffers.resolutions.iter())
{
if dep.version.tag != dependency::Tag::Catalog {
continue;
}
if package_id as usize >= packages_len {
continue;
}
let resolution = &package_resolutions[package_id as usize];
if resolution.tag != ResolutionTag::Npm {
continue;
}

let dep_name = dep.name.slice(string_buf);
let catalog_name = dep.version.catalog().slice(string_buf);
let Some(info) = infos.iter_mut().find(|info| {
strings::eql_long(&info.dep_name, dep_name, true)
&& strings::eql_long(&info.catalog_name, catalog_name, true)
}) else {
continue;
};
if info.resolved_version_literal.is_some() {
continue;
}

if !update_to_latest {
let Some(catalog_dep) = old.catalogs.get(old, *dep.version.catalog(), dep.name)
else {
continue;
};
if let Some(npm_version) = catalog_dep.version.try_npm() {
if npm_version.version.is_exact() {
continue;
}
}
}

let new_literal =
format_catalog_update_literal(resolution, string_buf, info, exact_versions);
info.resolved_version_literal = Some(new_literal.into_boxed_slice());
}
}

// Split-borrow: `string_builder!` only takes `old.buffers.string_bytes`
// + `old.string_pool`, leaving `old.catalogs` free.
Comment thread
robobun marked this conversation as resolved.
Resolved
let mut string_builder = string_builder!(old);
for info in infos.iter() {
// Unresolved entries are left as-is; with `--latest` they still hold
// the temporary `latest`, so restore the original literal.
Comment thread
robobun marked this conversation as resolved.
Resolved
let new_literal = info
.resolved_version_literal
.as_deref()
.unwrap_or(&info.original_version_literal);
if new_literal.len() >= SemverString::MAX_INLINE_LEN {
string_builder.cap += new_literal.len();
}
if info.catalog_name.len() >= SemverString::MAX_INLINE_LEN {
string_builder.cap += info.catalog_name.len();
}
if info.dep_name.len() >= SemverString::MAX_INLINE_LEN {
string_builder.cap += info.dep_name.len();
}
}
string_builder.allocate()?;

for info in infos.iter() {
let new_literal = info
.resolved_version_literal
.as_deref()
.unwrap_or(&info.original_version_literal);
let catalog_name = string_builder.append::<SemverString>(&info.catalog_name);
let dep_name = string_builder.append::<SemverString>(&info.dep_name);
let external_version = string_builder.append::<ExternalString>(new_literal);
let sliced = external_version
.value
.sliced(string_builder.string_bytes.as_slice());
let Some(version) = dependency::parse(
dep_name,
SemverStringBuilder::string_hash(&info.dep_name),
sliced.slice,
&sliced,
None,
&mut *manager,
) else {
continue;
};
let group = old
.catalogs
.get_or_put_group(string_builder.string_bytes.as_slice(), catalog_name)?;
let buf = string_builder.string_bytes.as_slice();
let ctx = bun_semver::string::ArrayHashContext {
arg_buf: buf,
existing_buf: buf,
};
let entry = group.get_or_put_adapted(&dep_name, &ctx)?;
if entry.found_existing {
entry.value_ptr.version = version;
}

Check warning on line 980 in src/install/lockfile.rs

View check run for this annotation

Claude / Claude Code Review

get_or_put_adapted leaves default entry in lockfile catalog on miss

`get_or_put_adapted` inserts a `(K::default(), V::default())` entry on miss, but this loop only writes `entry.value_ptr.version` when `found_existing` is true — so a miss would leave a default `(SemverString::default(), Dependency::default())` entry in `old.catalogs` that gets serialized into `bun.lock`. The intent here is 'update if exists, else no-op', so a non-inserting lookup (e.g. `get_adapted` / `catalogs.get`) would be correct-by-construction; the miss path looks unreachable today since `
Comment thread
robobun marked this conversation as resolved.
Resolved
}
string_builder.clamp();
manager.updating_catalogs = infos;
Ok(())
}

pub fn resolve_catalog_dependency(&self, dep: &Dependency) -> Option<DependencyVersion> {
if dep.version.tag != dependency::Tag::Catalog {
return Some(dep.version.clone());
Expand Down Expand Up @@ -992,6 +1122,10 @@
clean_preprocess_update_requests_cold(old, manager, updates, exact_versions)?;
}

if !manager.updating_catalogs.is_empty() {
clean_preprocess_updating_catalogs_cold(old, manager, exact_versions)?;
}

// Caller owns the new lockfile; return `Box<Lockfile>` so Drop reclaims
// it (never `Box::leak` to satisfy a lifetime).
let mut new: Box<Lockfile> = Box::default();
Expand Down Expand Up @@ -1226,6 +1360,77 @@
Lockfile::preprocess_update_requests(old, manager, updates, exact_versions)
}

#[cold]
#[inline(never)]
fn clean_preprocess_updating_catalogs_cold(
old: &mut Lockfile,
manager: &mut PackageManager,
exact_versions: bool,
) -> Result<(), BunError> {
Lockfile::preprocess_updating_catalogs(old, manager, exact_versions)
}

/// Version literal a catalog entry is rewritten to after `bun update`:
/// the resolved npm version with the entry's original `^`/`~`/exact pin
/// preserved (or exact under `--save-exact`), and the `npm:<name>@` prefix
/// kept for alias entries.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn format_catalog_update_literal(
Comment thread
robobun marked this conversation as resolved.
Resolved
resolution: &Resolution,
string_buf: &[u8],
info: &CatalogUpdateInfo,
exact_versions: bool,
) -> Vec<u8> {
let version_fmt = resolution.npm().version.fmt(string_buf);
let new_version: Vec<u8> = 'new_version: {
if exact_versions {
let mut v = Vec::new();
write!(&mut v, "{}", version_fmt).expect("infallible: in-memory write");
break 'new_version v;
}

let version_literal: &[u8] = 'version_literal: {
if !info.is_alias {
break 'version_literal &info.original_version_literal;
}
if let Some(at_index) =
strings::last_index_of_char(&info.original_version_literal, b'@')
{
break 'version_literal &info.original_version_literal[at_index + 1..];
}
&info.original_version_literal
};

let mut v = Vec::new();
match Semver::Version::which_version_is_pinned(version_literal) {
Semver::PinnedVersion::Patch => {
write!(&mut v, "{}", version_fmt).expect("infallible: in-memory write")
}
Semver::PinnedVersion::Minor => {
write!(&mut v, "~{}", version_fmt).expect("infallible: in-memory write")
}
Semver::PinnedVersion::Major => {
write!(&mut v, "^{}", version_fmt).expect("infallible: in-memory write")
}
}
v
};

if info.is_alias {
if let Some(at_index) = strings::last_index_of_char(&info.original_version_literal, b'@') {
let mut v = Vec::new();
write!(
&mut v,
"{}@{}",
bstr::BStr::new(&info.original_version_literal[0..at_index]),
bstr::BStr::new(&new_version)
)
.expect("infallible: in-memory write");
return v;
}
}
new_version
}

#[cold]
#[inline(never)]
fn clean_verbose_timer_start() -> Result<Timer, BunError> {
Expand Down
Loading
Loading