Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 20 additions & 3 deletions src/install/PackageManager/PackageJSONEditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,19 @@ pub(crate) fn edit(
bun_ast::Loc::EMPTY,
));

// A replaced entry keeps the literal the before-install pass wrote until the write-back
// below decides what to save (the `workspace:` arm keeps it as typed).
Comment thread
robobun marked this conversation as resolved.
Outdated
let declared: &[u8] = match new_dependencies[k]
.value
.as_ref()
.and_then(Expr::as_utf8_string_literal)
{
Some(literal) => arena_dup(arena, literal),
None => b"",
};
new_dependencies[k].value = Some(Expr::allocate(
arena,
// we set it later
E::EString::init(b""),
E::EString::init(declared),
bun_ast::Loc::EMPTY,
));

Expand Down Expand Up @@ -1451,7 +1460,15 @@ pub(crate) fn edit(
arena_dup(arena, installed)
}

resolution::Tag::Workspace => b"workspace:*",
// `<member>@workspace:^` (or `~`, or a version) is saved as typed since `bun pm pack`
// derives the published range from the spelling; a member linked any other way
// (`<member>@1.0.0`, a folder path) is saved as `workspace:*`. `request.version` does
// not say which: on the root it is bound to the implicit row the `workspaces` list
// creates for the member, whose literal is the member's path.
Comment thread
robobun marked this conversation as resolved.
Outdated
resolution::Tag::Workspace => match dependency::Tag::infer(e_string.data.slice()) {
dependency::Tag::Workspace => e_string.data.slice(),
_ => b"workspace:*",
},
_ => arena_dup(arena, request.version.literal.slice(request.version_buf())),
};
if e_string.data.slice() != new_literal {
Expand Down
69 changes: 69 additions & 0 deletions test/cli/install/bun-update-lockfile-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,75 @@ describe.concurrent("bun add", () => {
await expectInSync(dir, ["", PKG1]);
});

// The spelling decides what `bun pm pack` / `bun publish` replace the range with (`^1.0.0`, `~1.0.0`, `1.0.0`); it used to be saved as `workspace:*`.
test.each(["workspace:^", "workspace:~", "workspace:1.0.0", "workspace:^1.0.0"])(
"bun add <workspace>@%s keeps the spelling",
async literal => {
const dir = await setup(MONOREPO());
await run(dir, "add", `pkg1@${literal}`);
expect((await pkg(dir)).dependencies).toStrictEqual({ pkg1: literal });
expect((await lock(dir)).workspaces[""].dependencies).toStrictEqual({ pkg1: literal });
await expectInSync(dir, ["", PKG1]);
},
);

test("bun add <workspace>@workspace:^ -d without a lockfile", async () => {
const dir = await setup(MONOREPO(), { install: false });
await run(dir, "add", "pkg1@workspace:^", "-d");
const manifest = await pkg(dir);
expect(manifest.dependencies).toBeUndefined();
expect(manifest.devDependencies).toStrictEqual({ pkg1: "workspace:^" });
expect((await lock(dir)).workspaces[""].devDependencies).toStrictEqual({ pkg1: "workspace:^" });
await expectInSync(dir, ["", PKG1]);
});

test("bun add <workspace>@workspace:~ from another member", async () => {
const dir = await setup(WORKSPACES({}, { pkg1: {}, pkg2: {} }));
await runIn(dir, PKG2, "add", "pkg1@workspace:~");
expect((await pkg(dir, PKG2)).dependencies).toStrictEqual({ pkg1: "workspace:~" });
expect((await lock(dir)).workspaces[PKG2].dependencies).toStrictEqual({ pkg1: "workspace:~" });
expect((await pkg(dir)).dependencies).toBeUndefined();
await expectInSync(dir, ["", PKG1, PKG2]);
});

test("bun add <workspace>@workspace:~ replaces the existing entry", async () => {
const dir = await setup(MONOREPO({}, { dependencies: { pkg1: "workspace:*" } }));
await run(dir, "add", "pkg1@workspace:~");
expect((await pkg(dir)).dependencies).toStrictEqual({ pkg1: "workspace:~" });
expect((await lock(dir)).workspaces[""].dependencies).toStrictEqual({ pkg1: "workspace:~" });
await expectInSync(dir, ["", PKG1]);
});

test("bun add <workspace>@workspace:^ rewrites an entry listed in devDependencies in place", async () => {
const dir = await setup(MONOREPO({}, { devDependencies: { pkg1: "workspace:*" } }));
await run(dir, "add", "pkg1@workspace:^");
const manifest = await pkg(dir);
expect(manifest.dependencies).toBeUndefined();
expect(manifest.devDependencies).toStrictEqual({ pkg1: "workspace:^" });
expect((await lock(dir)).workspaces[""].devDependencies).toStrictEqual({ pkg1: "workspace:^" });
await expectInSync(dir, ["", PKG1]);
});

test("bun add <workspace>@workspace:^ --filter keeps the spelling in every target", async () => {
const dir = await setup(WORKSPACES({}, { pkg1: {}, pkg2: {} }));
await run(dir, "add", "pkg1@workspace:^", "--filter", "pkg2", "--filter", "root");
expect((await pkg(dir)).dependencies).toStrictEqual({ pkg1: "workspace:^" });
expect((await pkg(dir, PKG2)).dependencies).toStrictEqual({ pkg1: "workspace:^" });
const { workspaces } = await lock(dir);
expect(workspaces[""].dependencies).toStrictEqual({ pkg1: "workspace:^" });
expect(workspaces[PKG2].dependencies).toStrictEqual({ pkg1: "workspace:^" });
await expectInSync(dir, ["", PKG1, PKG2]);
});

// A member linked through a plain range is still saved as `workspace:*`.
test.each(["1.0.0", "^1.0.0"])("bun add <workspace>@%s saves workspace:*", async range => {
const dir = await setup(MONOREPO());
await run(dir, "add", `pkg1@${range}`);
expect((await pkg(dir)).dependencies).toStrictEqual({ pkg1: "workspace:*" });
expect((await lock(dir)).workspaces[""].dependencies).toStrictEqual({ pkg1: "workspace:*" });
await expectInSync(dir, ["", PKG1]);
});

test("bun add --filter", async () => {
const dir = await setup(MONOREPO());
await run(dir, "add", "x@npm:no-deps@latest", "--filter", "pkg1");
Expand Down
Loading