Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion src/install/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bun_collections::{
};
use bun_core::fmt::PathSep;
use bun_core::{Global, Output};
use bun_paths::{MAX_PATH_BYTES, SEP, SEP_STR, platform, resolve_path};
use bun_paths::{AutoAbsPathChecked, MAX_PATH_BYTES, SEP, SEP_STR, platform, resolve_path};
// `bun_install` sits above `bun_resolver` in the crate graph (no cycle), so use
// the real resolver `FileSystem` directly — same as `PackageManager.rs`.
use crate::bun_json as JSON;
Expand Down Expand Up @@ -2102,6 +2102,14 @@ pub(crate) fn linked_workspace_path(
range.satisfies(version, buf, buf).then_some(path)
}

/// `None` when the path does not fit a path buffer.
pub(crate) fn workspace_package_json_path(workspace_path: &[u8]) -> Option<AutoAbsPathChecked> {
let mut path = AutoAbsPathChecked::init_top_level_dir();
// A path from a lockfile is unchecked. `join` resolves an absolute one where `append` asserts on it.
path.join(&[workspace_path, b"package.json"]).ok()?;
Some(path)
}

impl Lockfile {
pub(crate) fn init_empty_value() -> Self {
Lockfile {
Expand Down
11 changes: 5 additions & 6 deletions src/install/lockfile/Package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::mem;
use bun_collections::{ArrayHashMap, ArrayIdentityContext, MultiArrayList, StringSet, index_sort};
use bun_core::strings;
use bun_core::{Global, Output};
use bun_paths::{self as path, AutoAbsPath, MAX_PATH_BYTES, resolve_path};
use bun_paths::{self as path, MAX_PATH_BYTES, resolve_path};
use bun_resolver::fs::FileSystem;
use bun_semver::semver_query::Wildcard;
use bun_semver::version::VersionInt;
Expand Down Expand Up @@ -1465,12 +1465,11 @@ impl Diff {
break 'update_mapping false;
};

let mut package_json_path: AutoAbsPath = AutoAbsPath::init_top_level_dir();

let _ = package_json_path.append(
let Some(package_json_path) = lockfile::workspace_package_json_path(
workspace_path.slice(to_lockfile.buffers.string_bytes.as_slice()),
);
let _ = package_json_path.append(b"package.json");
) else {
break 'update_mapping false;
};

// `bun.sys.File.toSource` was removed from
// T1 (`bun_sys`) because `bun_ast::Source` lives in T2.
Expand Down
12 changes: 6 additions & 6 deletions src/install/lockfile/pruned_workspaces.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use bstr::BStr;
use bun_paths::AutoAbsPath;
use bun_semver::String;
use bun_semver::string::Builder as StringBuilderNs;

use crate::dependency::{Dependency, Tag as DependencyVersionTag, VersionExt as _};
use crate::lockfile::DependencySlice;
use crate::lockfile::package::PackageColumns as _;
use crate::lockfile_real::{CatalogMap, Lockfile};
use crate::lockfile_real::{CatalogMap, Lockfile, workspace_package_json_path};
use crate::{PackageID, PackageNameHash, ResolutionTag};

pub(crate) fn workspace_is_missing_on_disk(
Expand All @@ -16,10 +15,11 @@ pub(crate) fn workspace_is_missing_on_disk(
let Some(workspace_path) = lockfile.workspace_paths.get(&workspace_name_hash).copied() else {
return false;
};
let mut package_json_path: AutoAbsPath = AutoAbsPath::init_top_level_dir();
let _ =
package_json_path.append(workspace_path.slice(lockfile.buffers.string_bytes.as_slice()));
let _ = package_json_path.append(b"package.json");
let Some(mut package_json_path) =
workspace_package_json_path(workspace_path.slice(lockfile.buffers.string_bytes.as_slice()))
else {
return true;
};
!bun_sys::exists_z(package_json_path.slice_z())
}

Expand Down
64 changes: 33 additions & 31 deletions src/install/pnpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,28 +741,30 @@ pub(crate) fn migrate_pnpm_lockfile<'a>(
continue;
}

let mut pkg_json_path = bun_paths::AutoAbsPath::init_top_level_dir();
let _ = pkg_json_path.append(importer_path); // OOM/capacity error is non-actionable here
let _ = pkg_json_path.append(b"package.json"); // OOM/capacity error is non-actionable here
let importer_does_not_exist = |log: &mut bun_ast::Log| {
log.add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"pnpm-lock.yaml lists importer '{}' but '{}/package.json' does not exist",
bstr::BStr::new(importer_path),
bstr::BStr::new(importer_path)
),
);
invalid_pnpm_lockfile()
};

let Some(pkg_json_path) = lockfile::workspace_package_json_path(importer_path) else {
return Err(importer_does_not_exist(log));
};

let importer_pkg_json = match manager.workspace_package_json_cache.get_with_path(
log,
pkg_json_path.slice(),
Default::default(),
) {
crate::GetJsonResult::Entry(j) => j,
crate::GetJsonResult::ReadErr(_) => {
log.add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"pnpm-lock.yaml lists importer '{}' but '{}/package.json' does not exist",
bstr::BStr::new(importer_path),
bstr::BStr::new(importer_path)
),
);
return Err(invalid_pnpm_lockfile());
}
crate::GetJsonResult::ReadErr(_) => return Err(importer_does_not_exist(log)),
crate::GetJsonResult::ParseErr(_) => return Err(invalid_pnpm_lockfile()),
};

Expand Down Expand Up @@ -875,14 +877,16 @@ pub(crate) fn migrate_pnpm_lockfile<'a>(
..Default::default()
};

let mut path_buf = bun_paths::AutoAbsPath::init_top_level_dir();
let _ = path_buf.append(path); // OOM/capacity error is non-actionable here
let abs_path: Box<[u8]> = Box::from(path_buf.slice());
let _ = path_buf.append(b"package.json"); // OOM/capacity error is non-actionable here
let mut abs_path = bun_paths::AutoAbsPath::init_top_level_dir();
let _ = abs_path.join(&[path]); // fits: the importer loop above built this path plus "/package.json"

let Some(package_json_path) = lockfile::workspace_package_json_path(path) else {
return Err(invalid_pnpm_lockfile());
};

let workspace_pkg_json = match manager
.workspace_package_json_cache
.get_with_path(log, path_buf.slice(), Default::default())
.get_with_path(log, package_json_path.slice(), Default::default())
.unwrap()
{
Ok(j) => j,
Expand Down Expand Up @@ -937,7 +941,7 @@ pub(crate) fn migrate_pnpm_lockfile<'a>(

let pkg_id = lockfile.append_package_dedupe(&mut pkg)?;

let entry = pkg_map.get_or_put(&abs_path)?;
let entry = pkg_map.get_or_put(abs_path.slice())?;
if entry.found_existing {
return Err(invalid_pnpm_lockfile());
}
Expand Down Expand Up @@ -1003,16 +1007,14 @@ pub(crate) fn migrate_pnpm_lockfile<'a>(
if dep.version.tag == dependency::VersionTag::Workspace {
let mut link_path_buf =
bun_paths::AutoAbsPath::init_top_level_dir();
let _ = link_path_buf.append(workspace_path); // OOM/capacity error is non-actionable here
let _ = link_path_buf.join(&[link_path]); // path-buffer overflow unreachable for bounded inputs
let _ = link_path_buf.join(&[workspace_path, link_path]); // path-buffer overflow unreachable for bounded inputs
Comment thread
robobun marked this conversation as resolved.
Resolved

for existing_workspace_path in lockfile.workspace_paths.values() {
let existing =
existing_workspace_path.slice(string_bytes!(lockfile));
let mut workspace_path_buf =
bun_paths::AutoAbsPath::init_top_level_dir();
// OOM/capacity error is non-actionable here
let _ = workspace_path_buf.append(
existing_workspace_path.slice(string_bytes!(lockfile)),
);
let _ = workspace_path_buf.join(&[existing]); // path-buffer overflow unreachable for bounded inputs

if strings::eql_long(
workspace_path_buf.slice(),
Expand Down Expand Up @@ -2241,13 +2243,13 @@ fn parse_append_importer_dependencies(
continue;
}

let mut path_buf = bun_paths::AutoAbsPath::init_top_level_dir();
let _ = path_buf.append(path); // OOM/capacity error is non-actionable here
let _ = path_buf.append(b"package.json"); // OOM/capacity error is non-actionable here
let Some(package_json_path) = lockfile::workspace_package_json_path(path) else {
return Err(ParseAppendDependenciesError::InvalidPnpmLockfile);
};

let workspace_pkg_json = match manager
.workspace_package_json_cache
.get_with_path(log, path_buf.slice(), Default::default())
.get_with_path(log, package_json_path.slice(), Default::default())
.unwrap()
{
Ok(j) => j,
Expand Down
30 changes: 29 additions & 1 deletion test/cli/install/bun-add-filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { file, write } from "bun";
import { afterAll, beforeAll, expect, test } from "bun:test";
import { chmod, exists, mkdir, rm } from "fs/promises";
import { VerdaccioRegistry, bunEnv, bunExe, isWindows, normalizeBunSnapshot } from "harness";
import { VerdaccioRegistry, bunEnv, bunExe, isWindows, normalizeBunSnapshot, tempDir } from "harness";
import { join } from "path";

const registry = new VerdaccioRegistry();
Expand Down Expand Up @@ -998,6 +998,34 @@ test.concurrent("add --filter with an existing lockfile re-resolves only the add
});
});

test.concurrent("add/remove --filter updates bun.lock for a workspace outside the root", async () => {
using dir = tempDir("add-filter-outside-root", {
"root/package.json": JSON.stringify({ name: "root", workspaces: ["../x", "../y"] }),
"x/package.json": JSON.stringify({ name: "x", version: "1.0.0" }),
"y/package.json": JSON.stringify({ name: "y", version: "1.0.0" }),
});
const root = join(String(dir), "root");
const x = () => file(join(String(dir), "x", "package.json")).json();
const lockedX = async () => (await lockfileJson(root)).workspaces["../x"];
// Hoisted: on Windows the isolated linker fails to link the dependencies of a workspace outside the root (ENOENT).
async function runOk(args: string[]) {
const { stderr, exitCode } = await run(args, String(dir), { cwd: root, linker: "hoisted" });
expect(stderr).not.toContain("error:");
expect(exitCode).toBe(0);
}

await runOk(["install"]);
expect(await lockedX()).toStrictEqual({ name: "x", version: "1.0.0" });

await runOk(["add", "--filter", "x", "y@workspace:*"]);
expect(await x()).toStrictEqual({ name: "x", version: "1.0.0", dependencies: { y: "workspace:*" } });
expect(await lockedX()).toStrictEqual({ name: "x", version: "1.0.0", dependencies: { y: "workspace:*" } });

await runOk(["remove", "--filter", "x", "y"]);
expect(await x()).toStrictEqual({ name: "x", version: "1.0.0" });
expect(await lockedX()).toStrictEqual({ name: "x", version: "1.0.0" });
});

test.concurrent("--dry-run writes nothing", async () => {
const dir = await makeMonorepo();
const before = await pkgText(dir, "api");
Expand Down
52 changes: 52 additions & 0 deletions test/cli/install/frozen-lockfile-pruned.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ async function verbatimTree(linker: Linker) {
return { packageDir, full };
}

// The root no longer lists "other", and bun.lock lists it at `otherPath(packageDir)`.
async function relocatedOtherTree(otherPath: (packageDir: string) => string, onDisk: string[]) {
const { packageDir } = await registry.createTestDir({ bunfigOpts: { linker: "hoisted" } });
await writeTree(packageDir, turboOutput, onDisk);
const path = otherPath(packageDir);
const lock = (await fullLockfile("hoisted")).replaceAll("packages/other", path);
expect(lock).toContain(`"${path}": {`);
expect(lock).toContain(`"other@workspace:${path}"`);
await write(join(packageDir, "bun.lock"), lock);
return { packageDir, lock };
}

// bun.lock holds an absolute path for a workspace on another Windows drive.
const absoluteOtherPath = (packageDir: string) => join(packageDir, "packages", "other").replaceAll("\\", "/");

const lockText = (dir: string) => file(join(dir, "bun.lock")).text();

async function editApp(dir: string, edit: (app: any) => void) {
Expand Down Expand Up @@ -1301,6 +1316,43 @@ describe("hoisted", () => {
expect(await lockText(packageDir)).toBe(full);
});

test.concurrent("a workspace at an absolute path in bun.lock that is on disk is not treated as pruned", async () => {
const { packageDir, lock } = await relocatedOtherTree(absoluteOtherPath, Object.keys(monorepo.packages));

const { stderr } = await frozen(packageDir, "hoisted", 1);

expect(stderr).not.toContain("skipped");
expect(await lockText(packageDir)).toBe(lock);
});

test.concurrent(
"a workspace at an absolute path in bun.lock that is missing from disk is treated as pruned",
async () => {
const { packageDir, lock } = await relocatedOtherTree(absoluteOtherPath, survivors);

const { stderr } = await frozen(packageDir, "hoisted", 0);

expect(stderr).toContain(prunedNote);
expect(await lockText(packageDir)).toBe(lock);
},
);

test.concurrent.each<[string, (packageDir: string) => string]>([
["an absolute path", absoluteOtherPath],
["a path that does not fit a path buffer", () => Buffer.alloc(100_000, "a").toString()],
])("a plain install removes an unlisted workspace that bun.lock lists at %s", async (_, otherPath) => {
const { packageDir } = await relocatedOtherTree(otherPath, Object.keys(monorepo.packages));
const { full: expected } = await fullInstall("hoisted", {
root: turboOutput.root,
packages: { "packages/app": appPackageJson, "packages/shared": sharedPackageJson },
});

const { stderr } = await install(packageDir, "hoisted");

expect(stderr).toContain("Saved lockfile");
expect(await lockText(packageDir)).toBe(expected);
});

test.concurrent(
"single-package project: trustedDependencies stripped from bun.lock does not drop a peer-held package under --frozen-lockfile",
async () => {
Expand Down
70 changes: 70 additions & 0 deletions test/cli/install/migration/pnpm-lock-migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,74 @@ snapshots:
expect(stderr).toContain("could not find any other lockfile");
expect(stderr).not.toContain("migrated lockfile from pnpm-lock.yaml");
});

// `a` depends on `b` through a workspace link, so every lookup of an importer's directory runs.
const linkedWorkspaces = {
"package.json": JSON.stringify({ name: "root", workspaces: ["packages/*"] }),
"packages/a/package.json": JSON.stringify({ name: "a", version: "1.0.0", dependencies: { b: "workspace:*" } }),
"packages/b/package.json": JSON.stringify({ name: "b", version: "1.0.0" }),
};

async function installWithImporters(dir: string, importerA: string, importerB: string) {
fs.writeFileSync(
join(dir, "pnpm-lock.yaml"),
`lockfileVersion: '9.0'

importers:

.: {}

'${importerA}':
dependencies:
b:
specifier: workspace:*
version: link:../b

'${importerB}': {}
`,
);
await using proc = Bun.spawn({
cmd: [bunExe(), "install"],
cwd: dir,
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
return { stderr, exitCode };
}

const bunLock = (dir: string) => fs.readFileSync(join(dir, "bun.lock"), "utf8");

test.concurrent("an absolute importer path migrates like the relative one", async () => {
using relativeDir = tempDir("pnpm-migrate-relative-importer", linkedWorkspaces);
using absoluteDir = tempDir("pnpm-migrate-absolute-importer", linkedWorkspaces);
const root = String(absoluteDir).replaceAll("\\", "/");

const [relative, absolute] = await Promise.all([
installWithImporters(String(relativeDir), "packages/a", "packages/b"),
installWithImporters(String(absoluteDir), `${root}/packages/a`, `${root}/packages/b`),
]);

expect(absolute.stderr).not.toContain("error:");
expect(absolute.stderr).toContain("migrated lockfile from pnpm-lock.yaml");
expect(absolute.exitCode).toBe(0);
expect(relative.stderr).toContain("migrated lockfile from pnpm-lock.yaml");
expect(bunLock(String(absoluteDir))).toContain('"a": ["a@workspace:packages/a"]');
expect(bunLock(String(absoluteDir))).toBe(bunLock(String(relativeDir)));
});

test.concurrent("an importer path that does not fit a path buffer fails the migration", async () => {
using dir = tempDir("pnpm-migrate-long-importer", linkedWorkspaces);
const longPath = Buffer.alloc(100_000, "a").toString();

const { stderr, exitCode } = await installWithImporters(String(dir), longPath, "packages/b");

expect(stderr).toContain(
`pnpm-lock.yaml lists importer '${longPath}' but '${longPath}/package.json' does not exist`,
);
expect(stderr).toContain("failed to migrate lockfile: 'pnpm-lock.yaml'");
expect(exitCode).toBe(0);
expect(bunLock(String(dir))).toContain('"a": ["a@workspace:packages/a"]');
});
});
Loading