Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
71f1057027 fix: use workspace path as cache key instead of name
Fixes a bug where workspaces with the same package name but in different
paths would incorrectly report "Workspace name already exists" errors.

The deduplication check was using the package name as the cache key,
which caused false positives when multiple workspace packages had the
same name but different paths (and potentially different versions).

Changed to use the workspace path as the cache key, which is the unique
identifier for each workspace. This aligns with how pnpm handles
workspace packages - allowing duplicate names with different versions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-11 23:36:32 +00:00
2 changed files with 48 additions and 2 deletions

View File

@@ -1771,8 +1771,10 @@ pub fn Package(comptime SemverIntType: type) type {
defer seen_workspace_names.deinit(allocator);
for (workspace_names.values(), workspace_names.keys()) |entry, path| {
// workspace names from their package jsons. duplicates not allowed
const gop = try seen_workspace_names.getOrPut(allocator, @truncate(String.Builder.stringHash(entry.name)));
// workspace names from their package jsons
// Use path as cache key since each workspace path must be unique
// This allows multiple workspaces with the same name but different versions (like pnpm)
const gop = try seen_workspace_names.getOrPut(allocator, @truncate(String.Builder.stringHash(path)));
if (gop.found_existing) {
// this path does alot of extra work to format the error message
// but this is ok because the install is going to fail anyways, so this

View File

@@ -0,0 +1,44 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
test("workspace name validation should use path as cache key, not name", async () => {
// This reproduces the issue where multiple workspaces with the same name
// incorrectly report duplicate workspace name errors
using dir = tempDir("workspace-name-cache", {
"package.json": JSON.stringify({
name: "root",
workspaces: ["apps/*"],
}),
"apps/1000/package.json": JSON.stringify({
name: "1000",
version: "1.0.0",
}),
"apps/3000/package.json": JSON.stringify({
name: "1000",
version: "1.0.0",
}),
"apps/5000/package.json": JSON.stringify({
name: "1000",
version: "1.0.0",
}),
"apps/10000/package.json": JSON.stringify({
name: "1000",
version: "1.0.0",
}),
});
await using proc = Bun.spawn({
cmd: [bunExe(), "install"],
env: bunEnv,
cwd: String(dir),
stderr: "pipe",
stdout: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
// The install should succeed - having the same name in different workspace paths is valid
// (though not a good practice, it shouldn't error)
expect(exitCode).toBe(0);
expect(stderr).not.toContain('Workspace name "1000" already exists');
});