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
2 changes: 1 addition & 1 deletion src/install/PackageInstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ impl<'a> PackageInstall<'a> {
}

let _ = sys::unlinkat(destination_dir, entry.path);
sys::symlinkat(entry.basename, destination_dir.fd(), entry.path)?;
sys::symlinkat(target, destination_dir.fd(), entry.path)?;
}
}
_ => {}
Expand Down
16 changes: 12 additions & 4 deletions src/install/lockfile/Tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,10 +822,18 @@ impl Tree {
}
tree_id = tree.parent;
}
break 'hoisted HoistDependencyResult::Placement(Placement {
id: next_id,
bundled: false,
});

// `next_id` is its own hoist root: the folder package is placed in this
// node, unless the same name is already placed here (the same `file:`
// folder in two dependency groups), which dedupes like any other package.
Comment thread
robobun marked this conversation as resolved.
Resolved
break 'hoisted Tree::hoist_dependency::<true, METHOD>(
next_id,
next_id,
pkg_id,
dep_id,
resolution_list,
builder,
);
}

Tree::hoist_dependency::<true, METHOD>(
Expand Down
101 changes: 100 additions & 1 deletion test/cli/install/bun-lock.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { file, spawn, write } from "bun";
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { readlinkSync } from "fs";
import { readlinkSync, realpathSync } from "fs";
import { access, copyFile, cp, exists, open, rm, writeFile } from "fs/promises";
import {
bunExe,
Expand Down Expand Up @@ -1826,3 +1826,102 @@ describe.each(["hoisted", "isolated"] as const)("peer no published version satis
expect(await file(lockfilePath).text()).toBe(lockfile);
});
});

describe.concurrent("a file: folder listed in both dependencies and devDependencies", () => {
async function install(cwd: string, ...args: string[]) {
await using proc = spawn({
cmd: [bunExe(), "install", ...args],
cwd,
env,
stdout: "pipe",
stderr: "pipe",
});
const [out, err, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// the duplicate dependency warning is expected, an error is not
expect({ args, err, code }).toEqual({ args, err: expect.not.stringContaining("error:"), code: 0 });
return { out, err };
}

// every key of the "packages" object (only package entries have an array value)
const packageKeys = (lockfile: string) => lockfile.match(/^ {4}"[^"]*": \[/gm);

it.each(["hoisted", "isolated"] as const)(
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
"writes one packages entry and --frozen-lockfile accepts it (%s linker)",
async linker => {
using dir = tempDir("dup-folder-dep-", {
"vendor/a/package.json": JSON.stringify({ name: "a", version: "1.0.0" }),
"package.json": JSON.stringify({
name: "app",
dependencies: { a: "file:./vendor/a" },
devDependencies: { a: "file:./vendor/a" },
}),
"bunfig.toml": Bun.TOML.stringify({ install: { linker } }),
});
const lockfilePath = join(String(dir), "bun.lock");

let { err } = await install(String(dir));
expect(err).toContain("Saved lockfile");
const lockfile = await file(lockfilePath).text();
// one key, not two: a duplicate object key is rejected by bun's own lockfile parser
expect(packageKeys(lockfile)).toEqual([' "a": [']);

({ err } = await install(String(dir), "--frozen-lockfile"));
expect(err).not.toContain("Ignoring lockfile");
expect(await file(lockfilePath).text()).toBe(lockfile);
},
);

it("keeps one entry when the two groups point at different folders", async () => {
using dir = tempDir("dup-folder-dep-", {
"v1/a/package.json": JSON.stringify({ name: "a", version: "1.0.0" }),
"v2/a/package.json": JSON.stringify({ name: "a", version: "2.0.0" }),
"package.json": JSON.stringify({
name: "app",
dependencies: { a: "file:./v1/a" },
devDependencies: { a: "file:./v2/a" },
}),
});
const lockfilePath = join(String(dir), "bun.lock");

await install(String(dir));
const lockfile = await file(lockfilePath).text();
expect(packageKeys(lockfile)).toEqual([' "a": [']);

const { err } = await install(String(dir), "--frozen-lockfile");
expect(err).not.toContain("Ignoring lockfile");
expect(await file(lockfilePath).text()).toBe(lockfile);
});

// A folder outside the project installs through per-file symlinks. Installing it
// twice into the same node_modules folder made the second pass hit EEXIST, and the
// EEXIST fallback linked each file to its own basename: `package.json -> package.json`.
it.skipIf(isWindows)("a folder outside the project does not become self-referencing symlinks", async () => {
using dir = tempDir("dup-folder-dep-", {
"a/package.json": JSON.stringify({ name: "a", version: "1.0.0", main: "index.js" }),
"a/index.js": `module.exports = "a from outside the project";`,
"app/package.json": JSON.stringify({
name: "app",
dependencies: { a: "file:../a" },
devDependencies: { a: "file:../a" },
}),
"app/bunfig.toml": Bun.TOML.stringify({ install: { linker: "hoisted" } }),
});
// the symlinks bun writes are absolute real paths (the tmpdir may sit behind a symlink)
const root = realpathSync(String(dir));
const appDir = join(root, "app");

await install(appDir);
expect(packageKeys(await file(join(appDir, "bun.lock")).text())).toEqual([' "a": [']);
expect(readlinkSync(join(appDir, "node_modules", "a", "package.json"))).toBe(join(root, "a", "package.json"));

await using proc = spawn({
cmd: [bunExe(), "-p", `require("a")`],
Comment thread
robobun marked this conversation as resolved.
Resolved
cwd: appDir,
env,
stdout: "pipe",
stderr: "pipe",
});
const [out, err, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ out: out.trim(), err, code }).toEqual({ out: "a from outside the project", err: "", code: 0 });
});
});
Loading