Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
005a47c
install: migrate yarn 2+ (berry) lockfiles
MarshallOfSound Aug 22, 2026
edba7a3
yarn berry migration: review fixes
MarshallOfSound Aug 22, 2026
f04f2ab
yarn berry migration: second round of review fixes
MarshallOfSound Aug 22, 2026
270d683
yarn berry migration: third round of review fixes
MarshallOfSound Aug 22, 2026
87c5712
yarn berry migration: fourth round of review fixes
MarshallOfSound Aug 22, 2026
cc5d089
yarn berry migration test: assert which parent gets which no-deps ver…
MarshallOfSound Aug 22, 2026
1bd6b00
yarn berry migration: patches on non-npm packages, multi-file patches
MarshallOfSound Aug 22, 2026
ee35edf
yarn berry migration: scoped resolutions never leak to other parents;…
MarshallOfSound Aug 22, 2026
801a7e7
yarn berry migration: patch: over portal:/link:, conflicting patch files
MarshallOfSound Aug 22, 2026
014e7ff
Merge main
alii Oct 8, 2026
caeaaf5
yarn berry migration: fail when yarn.lock pins something bun cannot keep
alii Oct 8, 2026
2e2eb46
yarn berry migration: compute the registry fallbacks lazily
alii Oct 8, 2026
87f0ac6
yarn berry migration: handle the cases review found
alii Oct 8, 2026
81d8200
yarn berry migration: pick yarn's registry with a match
alii Oct 8, 2026
4207a69
yarn berry migration: join folder paths with the path helper
alii Oct 8, 2026
55eb375
yarn berry migration: write package.json when bun.lock is saved
alii Oct 8, 2026
c0ef9ad
yarn berry migration: write package.json for --lockfile-only too
alii Oct 8, 2026
2c5106c
yarn berry migration: test the remaining error paths
alii Oct 8, 2026
a87fbb1
temporary: script that migrates real yarn berry projects
alii Oct 8, 2026
066a147
yarn berry tests: one install cache per test
alii Oct 8, 2026
274f02b
bun patch --commit: fetch package.json again after the lockfile loads
alii Oct 8, 2026
038fcbe
yarn berry migration: redact registry urls in error messages
alii Oct 8, 2026
bfa5684
temporary: let the corpus script fall back to bun on PATH
alii Oct 8, 2026
e3231e8
yarn berry migration: keep registry credentials out of bun.lock
alii Oct 9, 2026
62fa01c
yarn berry migration: fix what real lockfiles showed
alii Oct 9, 2026
fff45f9
temporary: leave yarn v1 lockfiles out of the corpus check
alii Oct 9, 2026
c114b88
yarn berry migration: accept workspace ranges in both dependency groups
alii Oct 9, 2026
6a74e87
yarn berry migration: first package extension to add a name wins
alii Oct 9, 2026
525fc49
yarn berry migration: keep a devDependencies duplicate as its own edge
alii Oct 9, 2026
cec7920
remove the temporary corpus script
alii Oct 9, 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
34 changes: 34 additions & 0 deletions docs/pm/cli/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,40 @@ Bun migrates the following pnpm configuration from both `pnpm-lock.yaml` and `pn

After migration, you can safely remove `pnpm-lock.yaml` and `pnpm-workspace.yaml` files.

## Yarn migration

Bun migrates a `yarn.lock` the same way when no `bun.lock` exists: both Yarn v1 lockfiles and Yarn 2+ ("Berry", the YAML format with a `__metadata` header) are converted to `bun.lock` during `bun install` or `bun pm migrate`, keeping every locked version. The original `yarn.lock` is left in place.

A Yarn Berry lockfile is migrated only when Bun can keep everything it pins. If one package, patch, workspace or dependency cannot be carried over exactly, Bun prints the reason, does not migrate, and resolves from `package.json` instead (`bun pm migrate` exits with an error).

For Yarn Berry lockfiles the migration handles:

- npm packages, including `npm:` aliases, dist-tags, and per-package tarball URLs (`__archiveUrl`); integrity hashes are taken from the registry, because Berry's `checksum` is a hash of Yarn's own cache archive
- workspaces (from the root `package.json` `workspaces` globs) and `workspace:` ranges
- `patch:` dependencies: patches in your project (for example `.yarn/patches`) are added to `patchedDependencies` in `package.json` for npm, git and tarball packages, and `patch:` ranges in `package.json` are rewritten to the plain range they patch; Yarn's builtin compatibility patches are dropped
- `file:` folders and tarballs, tarball URLs, and git / `github:` dependencies (pinned to the locked commit)
- `portal:` and `link:` dependencies, which are installed like `file:` dependencies; Bun does not support these protocols in `package.json`, so the ranges are rewritten to `file:` and a warning reports that the folder is now copied instead of linked
- optional dependencies and peer dependencies (`dependenciesMeta` / `peerDependenciesMeta`), and `conditions` (`os` / `cpu`)
- `resolutions` in the root `package.json`, applied the way Yarn applies them: the first matching entry wins, and `parent@version/name` names the version the parent is locked to
- `catalog:` ranges, read from `catalog` / `catalogs` in `.yarnrc.yml` and copied into the `workspaces` object of `package.json`
- Yarn 2 and 3 lockfiles, which write ranges without quotes (`wrappy: 1`)

Limitations:

- `libc` conditions are ignored, and Yarn settings such as `nodeLinker`, `packageExtensions`, and `supportedArchitectures` are not migrated
- integrity hashes come from the registry manifest; Yarn's `checksum` is not checked

The lockfile is not migrated when it has:

- a dependency with no lockfile entry (`yarn.lock` is older than `package.json`), or a workspace that the `package.json` `workspaces` globs do not match
- a registry for Yarn (`npmRegistryServer` or `npmScopes` in a `.yarnrc.yml` of the project, a parent folder or the home folder, or `YARN_NPM_REGISTRY_SERVER`) that is not the registry Bun is configured with for that package, or that is written with an environment variable (`${VAR}`); configure the registry in `bunfig.toml` or `.npmrc` and run the migration again. Yarn fetches the `@jsr` scope from `npm.jsr.io` by default, so that scope needs the same configuration
- a package whose integrity the registry manifest does not give, for example an `__archiveUrl` on another host
- a patch that `patchedDependencies` cannot express: several patch files for one package, a patch on a `file:` / `portal:` / `link:` folder, or a patch file outside the project
- an entry with a protocol Bun does not have (for example `exec:`), or a git dependency on a package inside a repository (`#workspace=`)
- a `__metadata.version` outside 4 to 10, a key written twice, or a YAML merge key (`<<`)

The edits to `package.json` (rewritten ranges, `patchedDependencies`, catalogs) are written when `bun.lock` is saved. A command that does not write `package.json` (`--dry-run`, `--no-save`) does not migrate a lockfile that needs them.

---

<Install />
2 changes: 1 addition & 1 deletion docs/pm/lockfile.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For more on the format, see [the blog post](https://bun.com/blog/bun-lock-text-l

When you run `bun install` in a project without a `bun.lock`, Bun automatically migrates existing lockfiles:

- `yarn.lock` (v1)
- `yarn.lock` (Yarn v1, and Yarn 2+ "Berry")
- `package-lock.json` (npm, `lockfileVersion` 2, 3 or 4)
- `pnpm-lock.yaml` (pnpm)

Expand Down
1 change: 1 addition & 0 deletions src/install/PackageManager/install_with_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,7 @@ fn save_lockfile_only(
packages_len_before_install,
log_level,
)?;
super::package_json_write_back::flush(manager)?;

if manager.subcommand == Subcommand::Dedupe {
if manager.options.do_.summary() {
Expand Down
2 changes: 1 addition & 1 deletion src/install/PackageManager/package_json_write_back.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ fn unchanged_on_disk(manager: &mut PackageManager, target: &WorkspaceTarget) ->
}

/// Phase 2 (after bun.lock is saved): add `trustedDependencies` learned during the install and write every edited entry whose bytes differ from disk.
pub(crate) fn flush(manager: &mut PackageManager) -> Result<(), crate::Error> {
pub fn flush(manager: &mut PackageManager) -> Result<(), crate::Error> {
if manager.edited_package_jsons.is_empty()
|| !manager.options.do_.contains(Do::WRITE_PACKAGE_JSON)
{
Expand Down
23 changes: 19 additions & 4 deletions src/install/PackageManager/updatePackageJSONAndInstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,11 @@ fn update_package_json_and_install_with_manager_with_updates(
_ => {
if matches!(manager.options.patch_features, PatchFeatures::Commit { .. }) {
let mut pathbuf = bun_paths::path_buffer_pool::get();
if let Some(stuff) =
patch_package::do_patch_commit(manager, &mut pathbuf, log_level)?
{
let stuff = patch_package::do_patch_commit(manager, &mut pathbuf, log_level)?;
// `do_patch_commit` loads the lockfile. A yarn or pnpm migration there
// re-parses the cached entry, which frees the tree copied above.
current_package_json_root = current_package_json.root;
Comment thread
alii marked this conversation as resolved.
Resolved
if let Some(stuff) = stuff {
// we're inside a workspace package, we need to edit the
// root json, not the `current_package_json`
if stuff.not_in_workspace_root {
Expand Down Expand Up @@ -660,6 +662,7 @@ fn update_package_json_and_install_with_manager_with_updates(
}

if manager.options.do_.contains(Do::WRITE_PACKAGE_JSON) {
let after_install_source: Vec<u8>;
let (source, path): (&[u8], &ZStr) =
if matches!(manager.options.patch_features, PatchFeatures::Commit { .. }) {
'source_and_path: {
Expand Down Expand Up @@ -689,8 +692,20 @@ fn update_package_json_and_install_with_manager_with_updates(
);
}
} else {
// A yarn or pnpm lockfile migration during the install edits the cached entry
// (ranges only that package manager reads) after it was printed above.
let cached = manager.workspace_package_json_cache.get_with_path(
manager.log_mut(),
manager.original_package_json_path.as_bytes(),
GetJSONOptions::default(),
);
if let Ok(entry) = cached.unwrap() {
after_install_source = entry.source.contents.to_vec();
} else {
after_install_source = new_package_json_source;
}
(
&new_package_json_source,
&after_install_source,
manager.original_package_json_path.as_zstr(),
)
};
Expand Down
3 changes: 3 additions & 0 deletions src/install/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum Error {
DanglingSymlink,
#[error("UnsupportedYarnLockfileVersion")]
UnsupportedYarnLockfileVersion,
#[error("InvalidYarnBerryLockfile")]
InvalidYarnBerryLockfile,
#[error("InvalidPackageJSON")]
InvalidPackageJSON,
#[error("LockfileResolveFailed")]
Expand Down Expand Up @@ -265,6 +267,7 @@ impl Error {
Self::BufferTooSmall => "BufferTooSmall",
Self::DanglingSymlink => "DanglingSymlink",
Self::UnsupportedYarnLockfileVersion => "UnsupportedYarnLockfileVersion",
Self::InvalidYarnBerryLockfile => "InvalidYarnBerryLockfile",
Self::InvalidPackageJSON => "InvalidPackageJSON",
Self::LockfileResolveFailed => "LockfileResolveFailed",
Self::DistTagNotFound => "DistTagNotFound",
Expand Down
1 change: 1 addition & 0 deletions src/install/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub mod repository_real;
pub mod update_scope;
pub mod update_transitive;
pub mod yarn;
pub mod yarn_berry;

/// `repository` — re-export of the file-backed `repository_real` module
/// (src/install/repository.rs). The earlier inline stub duplicated the
Expand Down
29 changes: 24 additions & 5 deletions src/install/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,8 +1482,12 @@ pub(crate) type PendingResolutions = Vec<PendingResolution>;
// ────────────────────────────────────────────────────────────────────────────

impl Lockfile {
/// `FILL_INTEGRITY`: take the registry manifest's integrity for npm packages
/// the migrated lockfile had none for (yarn berry records a checksum of its
/// own zip archive, not of the tarball).
pub(crate) fn fetch_necessary_package_metadata_after_yarn_or_pnpm_migration<
const UPDATE_OS_CPU: bool,
const FILL_INTEGRITY: bool,
>(
&mut self,
manager: &mut PackageManager,
Expand Down Expand Up @@ -1525,10 +1529,7 @@ impl Lockfile {
meta,
..
} = pkgs.split_mut();
// One loop serves both modes: bind `pkg_metas` as an empty slice
// when the const generic is false.
let pkg_metas: &mut [self::package::meta::Meta] =
if UPDATE_OS_CPU { meta } else { &mut [] };
let pkg_metas: &mut [self::package::meta::Meta] = meta;

for i in 0..len {
let pkg_name = pkg_names[i];
Expand Down Expand Up @@ -1606,8 +1607,8 @@ impl Lockfile {

builder.clamp();

let pkg_meta = &mut pkg_metas[i];
if UPDATE_OS_CPU {
let pkg_meta = &mut pkg_metas[i];
// Update os/cpu metadata if not already set
if pkg_meta.os == Npm::OperatingSystem::ALL {
pkg_meta.os = pkg.package.os;
Expand All @@ -1616,6 +1617,24 @@ impl Lockfile {
pkg_meta.arch = pkg.package.cpu;
}
}
// Only when the tarball is the one the manifest describes: under the
// registry it was fetched from, or its recorded `dist.tarball`. A
// fresh resolve records the same value.
if FILL_INTEGRITY
&& !pkg_meta.integrity.tag.is_supported()
&& pkg.package.integrity.tag.is_supported()
{
let url = pkg_res
.npm()
.url
.slice(self.buffers.string_bytes.as_slice());
if url.is_empty()
|| bun_lock::url_is_under_registry(url, scope.url.href())
|| url == pkg.package.tarball_url.slice(&manifest.string_buf)
{
pkg_meta.integrity = pkg.package.integrity;
}
}
}
_ => {}
}
Expand Down
7 changes: 7 additions & 0 deletions src/install/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ pub fn detect_and_load_other_lockfile<'a>(
let migrate_result = match yarn::migrate_yarn_lockfile(this, manager, log, &data, dir) {
Ok(r) => r,
Err(e) => {
// The reasons are in `log`. Left there, they would fail the
// install that resolves from package.json instead.
if !manager.options.log_level.is_silent() && log.has_errors() {
let _ = log.print(std::ptr::from_mut(Output::error_writer()));
Output::flush();
}
log.reset();
return LoadResult::Err(LoadResultErr {
step: LoadStep::Migrating,
value: e,
Expand Down
5 changes: 3 additions & 2 deletions src/install/pnpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ fn e_object(expr: &Expr) -> &E::Object {
}
}

fn e_object_mut(expr: &mut Expr) -> &mut E::Object {
pub(crate) fn e_object_mut(expr: &mut Expr) -> &mut E::Object {
match &mut expr.data {
ExprData::EObject(o) => &mut **o,
_ => unreachable!("e_object_mut called on non-object"),
Expand Down Expand Up @@ -1620,7 +1620,8 @@ pub(crate) fn migrate_pnpm_lockfile<'a>(
lockfile.tag_workspace_links(manager.options.link_workspace_packages);
lockfile.resolve(log)?;

lockfile.fetch_necessary_package_metadata_after_yarn_or_pnpm_migration::<false>(manager)?;
lockfile
.fetch_necessary_package_metadata_after_yarn_or_pnpm_migration::<false, false>(manager)?;

update_package_json_after_migration(manager, log, dir, &found_patches)?;

Expand Down
18 changes: 15 additions & 3 deletions src/install/yarn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,20 @@ pub(crate) fn migrate_yarn_lockfile<'a>(
data: &[u8],
dir: Fd,
) -> Result<LoadResult<'a>, Error> {
// yarn v2+ (berry) lockfiles are not supported; only the v1 format migrates.
if !strings::index_of(data, b"# yarn lockfile v1").is_some() {
// yarn 2+ (berry) lockfiles are YAML with a `__metadata` header. Yarn reads a
// file as v1 only when the marker is in the comment lines at its top, so a
// berry file that holds the marker further down is still a berry file.
let is_berry = strings::contains(data, b"\n__metadata:") || data.starts_with(b"__metadata:");
let has_v1_marker = match strings::index_of(data, b"# yarn lockfile v1") {
Some(at) if is_berry => strings::split(&data[..at], b"\n")
.all(|line| line.is_empty() || line.starts_with(b"#") || line == b"\r"),
Some(_) => true,
None => false,
};
if !has_v1_marker {
if is_berry {
return crate::yarn_berry::migrate_yarn_berry_lockfile(this, manager, log, data);
}
return Err(crate::Error::UnsupportedYarnLockfileVersion);
}

Expand Down Expand Up @@ -1917,7 +1929,7 @@ pub(crate) fn migrate_yarn_lockfile<'a>(
return Err(crate::Error::LockfileResolveFailed);
}

this.fetch_necessary_package_metadata_after_yarn_or_pnpm_migration::<true>(manager)?;
this.fetch_necessary_package_metadata_after_yarn_or_pnpm_migration::<true, false>(manager)?;

if cfg!(debug_assertions) {
this.verify_data()?;
Expand Down
Loading
Loading