Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2e1826a
install: fail --frozen-lockfile when bun install would rewrite bun.lo…
robobun Sep 7, 2026
d68b5ec
install: tolerate trustedDependencies removals under --frozen-lockfil…
robobun Sep 8, 2026
787b6a6
install: name the package.json whose dependencies changed in the froz…
robobun Sep 8, 2026
f1bd3df
test: shorter describe title
robobun Sep 8, 2026
5a313c6
install: compare patchedDependencies only against a list bun.lock rec…
robobun Sep 8, 2026
69d6791
Merge remote-tracking branch 'origin/main' into robobun/8cfddafa/froz…
robobun Sep 8, 2026
ba6a209
test: cover a direct dependency added or removed while it stays in th…
robobun Sep 8, 2026
b5ee23b
install: shorten the frozen-lockfile manifest check comments
robobun Sep 8, 2026
0b2bd98
test: use test.each for the frozen-lockfile case tables
robobun Sep 8, 2026
5dbd043
install: one-line comments for the frozen-lockfile manifest check
robobun Sep 8, 2026
07e2e0c
install: re-read a workspace whose range on a sibling was retyped, so…
robobun Sep 9, 2026
f4340fe
install: one-line comment for the retyped workspace range check
robobun Sep 9, 2026
e51198c
docs: drop the npm ci comparison from the frozen-lockfile paragraph
robobun Sep 9, 2026
98ee3fd
docs: say when trustedDependencies and patchedDependencies are compar…
robobun Sep 9, 2026
f2ddc87
Merge remote-tracking branch 'origin/main' into robobun/8cfddafa/froz…
robobun Sep 18, 2026
1017e53
install: fail --frozen-lockfile on the first trustedDependencies or p…
robobun Sep 18, 2026
f09321d
install: do not fail --frozen-lockfile on bun.lock rows bun itself re…
robobun Sep 18, 2026
3af8ea2
docs: say that bun ci and bun install --production run the same froze…
robobun Sep 18, 2026
4a69a2b
test: a root dependency that a merge put into a workspace's block of …
robobun Sep 18, 2026
d8238f3
test: a workspace that drops a dependency the root still needs fails …
robobun Sep 19, 2026
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
2 changes: 2 additions & 0 deletions docs/pm/cli/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ For reproducible installs, use `--frozen-lockfile`. Bun installs the exact versi
bun install --frozen-lockfile
```

`package.json` disagrees with `bun.lock` when a dependency would resolve to a different package, and also when every package stays as locked but the next `bun install` would rewrite `bun.lock`: a version range written differently (`^1.0.0` changed to `>=1.0.0`), a dependency moved between `dependencies`, `devDependencies`, `optionalDependencies` and `peerDependencies`, or a changed `overrides`, `catalog`, `trustedDependencies` or `patchedDependencies` entry, like `pnpm install --frozen-lockfile` and `yarn install --immutable`. The error names the section and the `package.json` it is in. Run `bun install` and commit the updated `bun.lock` to fix it. `bun ci` and `bun install --production` run the same check, so a Dockerfile that installs with `--production` also fails on a `bun.lock` that is out of date. A bumped workspace `version` on its own is accepted. A release tool that also rewrites the ranges other workspaces declare on it needs one `bun install` before the commit. A workspace project has two exceptions, because a pruned checkout can look like this: a removed `trustedDependencies` name is accepted (`bun.lock` records one list for all workspaces), and a `trustedDependencies` or `patchedDependencies` section is only compared when `bun.lock` already records it. With the legacy binary `bun.lockb`, only resolution, `overrides` and `catalog` changes are detected.

Bun does not enable `--frozen-lockfile` automatically in CI; pass the flag or use `bun ci`. If there is no lockfile at all, `--frozen-lockfile` installs from `package.json` without writing one.

`--frozen-lockfile` works on a pruned monorepo checkout (e.g. `turbo prune` output, or a Docker context with only some workspace folders copied in). If a workspace listed in `bun.lock` is missing its `package.json` on disk, Bun skips it with a `note:` and does not install its exclusive dependencies. If a remaining workspace depends on a skipped one, the install fails.
Expand Down
30 changes: 29 additions & 1 deletion src/install/PackageManager/install_with_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn install_with_manager(
// this defaults to false
// but we force allowing updates to the lockfile when you do bun add
let mut had_any_diffs = false;
let mut loaded_manifest_sections: Option<lockfile::ManifestSections> = None;
let mut direct_deps_before = DirectDependencies::default();
manager.progress = Default::default();

Expand Down Expand Up @@ -235,6 +236,14 @@ pub fn install_with_manager(
};

had_any_diffs = manager.summary.has_diffs();
// For the frozen-lockfile check; the loaded sections are replaced below.
if had_any_diffs
&& manager.options.enable.frozen_lockfile()
&& ok.format == lockfile::Format::Text
&& ok.migrated == lockfile::Migrated::None
{
loaded_manifest_sections = Some(manager.lockfile.manifest_sections());
}
// The lockfile does not store the set. Every install takes it from the manifests.
manager
.lockfile
Expand Down Expand Up @@ -789,14 +798,25 @@ pub fn install_with_manager(
{
'frozen_lockfile: {
let changed_section = frozen_changed_section(manager, root_package_json_path);
// A section bun.lock records differently, and the directory of its package.json.
let mut mismatch: Option<(&'static str, Box<[u8]>)> = None;
if changed_section.is_none() {
if load_result.loaded_from_text_lockfile() {
if bun_core::handle_oom(Lockfile::eql(
&manager.lockfile,
&lockfile_before_clean,
lockfile_before_clean.loaded_package_count as usize,
)) {
break 'frozen_lockfile;
if let Some(loaded) = &loaded_manifest_sections {
mismatch = manager
.lockfile
.manifest_sections()
.changed_since(loaded)
.map(|(section, dir)| (section, Box::from(dir)));
Comment thread
robobun marked this conversation as resolved.
Resolved
}
if mismatch.is_none() {
break 'frozen_lockfile;
}
}
} else if !(manager
.lockfile
Expand All @@ -821,6 +841,14 @@ pub fn install_with_manager(
section,
loaded_lockfile_name(&load_result)
);
} else if let Some((section, dir)) = &mismatch {
bun_core::note!(
"{} does not match {} in {}{}package.json",
loaded_lockfile_name(&load_result),
section,
bstr::BStr::new(dir),
if dir.is_empty() { "" } else { "/" },
);
}
bun_core::note!(
"try re-running without <d>--frozen-lockfile<r> and commit the updated lockfile"
Expand Down
211 changes: 211 additions & 0 deletions src/install/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2874,7 +2874,218 @@ impl<'a> EqlSorter<'a> {
}
}

/// One dependency row of a workspace entry in bun.lock.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct ManifestDependency {
name: Box<[u8]>,
/// `Behavior` bits of the dependency groups it is listed under.
groups: u8,
literal: Box<[u8]>,
/// The range a `catalog:` literal stands for.
catalog_range: Option<Box<[u8]>>,
}

impl ManifestDependency {
const OPTIONAL_PEER: u8 =
dependency::Behavior::PEER.bits() | dependency::Behavior::OPTIONAL.bits();

/// Migrating a pnpm-lock.yaml records the catalog's range where package.json says `catalog:`.
fn is_recorded_as(&self, recorded: &Self) -> bool {
self.name == recorded.name
&& self.groups == recorded.groups
&& (self.literal == recorded.literal
|| self.catalog_range.as_ref() == Some(&recorded.literal))
Comment thread
robobun marked this conversation as resolved.
Resolved
}

/// A `peerDependenciesMeta`-only name is a `*` optional peer; older bun.lock files lack the row.
fn may_be_unrecorded(&self) -> bool {
self.groups == Self::OPTIONAL_PEER && *self.literal == *b"*"
}

/// Both sorted.
fn all_recorded(now: &[Self], recorded: &[Self]) -> bool {
let mut recorded = recorded.iter().peekable();
for dep in now {
if recorded.peek().is_some_and(|row| dep.is_recorded_as(row)) {
recorded.next();
} else if !dep.may_be_unrecorded() {
return false;
}
}
recorded.next().is_none()
}
}

/// The manifest-derived sections of bun.lock, mirrored from `bun_lock::Stringifier::save_from_binary`.
/// Overrides, catalogs and workspace versions are not part of it.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) struct ManifestSections {
/// Workspace path (`""` for the root) with its sorted dependencies.
workspaces: Vec<(Box<[u8]>, Vec<ManifestDependency>)>,
/// Sorted names that apply to the tree. `None` when the lockfile has no list.
trusted_dependencies: Option<Vec<Box<[u8]>>>,
/// Sorted (`name@version`, patch path) that apply to the tree. `None` when there are none.
patched_dependencies: Option<Vec<(Box<[u8]>, Box<[u8]>)>>,
}

impl ManifestSections {
/// The first section where `self` differs from `loaded`, with its package.json's directory.
pub(crate) fn changed_since(&self, loaded: &ManifestSections) -> Option<(&'static str, &[u8])> {
if self.workspaces.len() != loaded.workspaces.len() {
return Some(("workspaces", b""));
}
for ((path, now), (recorded_path, recorded)) in
self.workspaces.iter().zip(&loaded.workspaces)
{
if path != recorded_path {
return Some(("workspaces", b""));
}
if !ManifestDependency::all_recorded(now, recorded) {
return Some(("dependencies", path));
}
}

// A pruned checkout declares fewer trusted names, and its bun.lock may lack either list.
let maybe_pruned = loaded.workspaces.len() > 1;

let recorded = loaded.trusted_dependencies.as_deref();
if recorded.is_some() || !maybe_pruned {
let recorded = recorded.unwrap_or_default();
let declared = self.trusted_dependencies.as_deref().unwrap_or_default();
let added = declared
.iter()
.any(|name| recorded.binary_search(name).is_err());
let removed = !maybe_pruned && declared.len() < recorded.len();
if added || removed {
return Some(("trustedDependencies", b""));
}
}

let recorded = loaded.patched_dependencies.as_deref();
if (recorded.is_some() || !maybe_pruned)
&& self.patched_dependencies.as_deref().unwrap_or_default()
!= recorded.unwrap_or_default()
{
return Some(("patchedDependencies", b""));
}
None
}
}

impl Lockfile {
pub(crate) fn manifest_sections(&self) -> ManifestSections {
let buf = self.buffers.string_bytes.as_slice();
let deps_buf = self.buffers.dependencies.as_slice();
let hoisted_deps = self.buffers.hoisted_dependencies.as_slice();
let pkgs = self.packages.slice();
let pkg_names = pkgs.items_name();
let pkg_name_hashes = pkgs.items_name_hash();
let pkg_resolutions = pkgs.items_resolution();
let pkg_deps = pkgs.items_dependencies();

let groups = dependency::Behavior::PROD
| dependency::Behavior::DEV
| dependency::Behavior::OPTIONAL
| dependency::Behavior::PEER;
let mut workspaces = Vec::new();
for pkg_id in 0..pkgs.len() {
let res = &pkg_resolutions[pkg_id];
let path: &[u8] = match res.tag {
ResolutionTag::Root => b"",
ResolutionTag::Workspace => res.workspace().slice(buf),
_ => continue,
};
let mut deps: Vec<ManifestDependency> = pkg_deps[pkg_id]
.get(deps_buf)
.iter()
.filter(|dep| dep.behavior.intersects(groups))
.map(|dep| ManifestDependency {
name: Box::from(dep.name.slice(buf)),
groups: dep.behavior.intersection(groups).bits(),
literal: Box::from(dep.version.literal.slice(buf)),
catalog_range: (dep.version.tag == dependency::Tag::Catalog).then(|| {
Box::from(self.catalogs.resolve_range(buf, dep).literal.slice(buf))
}),
})
.collect();
deps.sort_unstable();
workspaces.push((Box::<[u8]>::from(path), deps));
}
workspaces.sort_unstable_by(|l, r| l.0.cmp(&r.0));

let mut trusted_dependencies: Option<Vec<Box<[u8]>>> =
self.trusted_dependencies.as_ref().map(|_| Vec::new());
let mut patched_dependencies: Option<Vec<(Box<[u8]>, Box<[u8]>)>> =
(self.patched_dependencies.count() > 0).then(Vec::new);
let mut name_and_version: Vec<u8> = Vec::new();
for tree in self.buffers.trees.iter() {
for &dep_id in tree.dependencies.get(hoisted_deps) {
if dep_id == invalid_dependency_id {
continue;
}
let pkg_id = self.buffers.resolutions[dep_id as usize];
if pkg_id == invalid_package_id {
continue;
}
let dep = &deps_buf[dep_id as usize];

if let (Some(trusted), Some(found)) =
(&self.trusted_dependencies, &mut trusted_dependencies)
{
let name = dep.name.slice(buf);
if trusted
.get(&(dep.name_hash as TruncatedPackageNameHash))
.is_some_and(|trusted_name| **trusted_name == *name)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
found.push(Box::from(name));
}
}

if let Some(found) = &mut patched_dependencies {
let res = &pkg_resolutions[pkg_id as usize];
name_and_version.clear();
let _ = write!(
&mut name_and_version,
"{}@",
bstr::BStr::new(pkg_names[pkg_id as usize].slice(buf))
);
if res.tag == ResolutionTag::Workspace {
if let Some(version) = self
.workspace_versions
.get(&pkg_name_hashes[pkg_id as usize])
{
let _ = write!(&mut name_and_version, "{}", version.fmt(buf));
}
} else {
let _ = write!(&mut name_and_version, "{}", res.fmt(buf, PathSep::Posix));
}
if let Some(patch) = self
.patched_dependencies
.get(&SemverStringBuilder::string_hash(&name_and_version))
{
found.push((
Box::from(name_and_version.as_slice()),
Box::from(patch.path.slice(buf)),
));
}
}
}
}
if let Some(found) = &mut trusted_dependencies {
found.sort_unstable();
found.dedup();
}
if let Some(found) = &mut patched_dependencies {
found.sort_unstable();
found.dedup();
}

ManifestSections {
workspaces,
trusted_dependencies,
patched_dependencies,
}
}

/// A placement of `r` bound past `r_loaded_package_count` was rebound after loading: a change.
pub(crate) fn eql(
&self,
Expand Down
25 changes: 19 additions & 6 deletions src/install/lockfile/Package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,12 +1428,25 @@ impl Diff {
let cur_to_i = to_i;
to_i += 1;

if Dependency::eql(
&to_deps!()[cur_to_i],
from_dep,
to_lockfile.buffers.string_bytes.as_slice(),
from_lockfile.buffers.string_bytes.as_slice(),
) {
// Workspace versions compare by path; bun.lock records the literal, so a retyped one is an update.
let workspace_literal_retyped = {
let to_dep = &to_deps!()[cur_to_i];
to_dep.version.tag == dependency::version::Tag::Workspace
&& !from_dep.behavior.is_workspace()
&& !to_dep.version.literal.eql(
from_dep.version.literal,
to_lockfile.buffers.string_bytes.as_slice(),
from_lockfile.buffers.string_bytes.as_slice(),
)
};
if !workspace_literal_retyped
&& Dependency::eql(
&to_deps!()[cur_to_i],
from_dep,
to_lockfile.buffers.string_bytes.as_slice(),
from_lockfile.buffers.string_bytes.as_slice(),
)
{
if let Some(updates) = update_requests {
if updates.is_empty()
|| (named_update_here
Expand Down
Loading
Loading