Skip to content

Commit

Permalink
fix(install): workspace version added to package.json (#11241)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored May 22, 2024
1 parent ecb6c81 commit bb13798
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7088,14 +7088,16 @@ pub const PackageManager = struct {
},
}
}
break :brk null;

break :brk try allocator.dupe(u8, request.version.literal.slice(request.version_buf));
},
.uninitialized => switch (request.version.tag) {
.uninitialized => try allocator.dupe(u8, latest),
else => null,
else => try allocator.dupe(u8, request.version.literal.slice(request.version_buf)),
},
else => null,
} orelse try allocator.dupe(u8, request.version.literal.slice(request.version_buf));
.workspace => try allocator.dupe(u8, "workspace:*"),
else => try allocator.dupe(u8, request.version.literal.slice(request.version_buf)),
};
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/cli/install/bun-workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { spawnSync } from "bun";
import { bunExe, bunEnv as env, runBunInstall, tmpdirSync, toMatchNodeModulesAt } from "harness";
import { join } from "path";
import { writeFileSync, mkdirSync, rmSync } from "fs";
import { writeFile, mkdir } from "fs/promises";
import { beforeEach, test, expect } from "bun:test";
import { install_test_helpers } from "bun:internal-for-testing";
const { parseLockfile } = install_test_helpers;
Expand Down Expand Up @@ -147,3 +148,57 @@ test("dependency on same name as workspace and dist-tag", async () => {
expect(lockfile).toMatchNodeModulesAt(packageDir);
expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual(["", "3 packages installed"]);
});

test("adding workspace in workspace edits package.json with correct version (workspace:*)", async () => {
await writeFile(
join(packageDir, "package.json"),
JSON.stringify({
name: "foo",
workspaces: ["packages/*", "apps/*"],
}),
);

await mkdir(join(packageDir, "packages", "pkg1"), { recursive: true });
await writeFile(
join(packageDir, "packages", "pkg1", "package.json"),
JSON.stringify({
name: "pkg1",
version: "1.0.0",
}),
);

await mkdir(join(packageDir, "apps", "pkg2"), { recursive: true });
await writeFile(
join(packageDir, "apps", "pkg2", "package.json"),
JSON.stringify({
name: "pkg2",
version: "1.0.0",
}),
);

const { stdout, exited } = Bun.spawn({
cmd: [bunExe(), "add", "pkg2@workspace:*"],
cwd: join(packageDir, "packages", "pkg1"),
stdout: "pipe",
stderr: "inherit",
env,
});
const out = await Bun.readableStreamToText(stdout);

expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
"",
"installed pkg2@workspace:apps/pkg2",
"",
"2 packages installed",
]);

expect(await exited).toBe(0);

expect(await Bun.file(join(packageDir, "packages", "pkg1", "package.json")).json()).toEqual({
name: "pkg1",
version: "1.0.0",
dependencies: {
pkg2: "workspace:*",
},
});
});

0 comments on commit bb13798

Please sign in to comment.