Skip to content
82 changes: 45 additions & 37 deletions src/install/lockfile/Package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,43 +1750,51 @@
.append::<String>(if relative.is_empty() { b"." } else { relative });
}
dependency::version::Tag::Npm => {
if let Some(workspace_version) = workspace_version {
let satisfies =
dependency_version
.npm()
.version
.satisfies(workspace_version, buf, buf);
if pm.options.link_workspace_packages && satisfies {
// `String::sliced` takes `&'a self`; bind the unwrapped
// value so the borrow outlives the parse call.
let wp = workspace_path.unwrap();
let path = wp.sliced(buf);
if let Some(mut dep) = dependency::parse_with_tag(
external_alias.value,
Some(external_alias.hash),
path.slice,
dependency::version::Tag::Workspace,
&path,
Some(&mut *log),
Some(&mut *pm),
) {
// Whole-struct move so `Drop` frees the old npm
// chain; keep the existing `literal`.
dep.literal = dependency_version.literal;
dependency_version = dep;
}
} else {
// It doesn't satisfy, but a workspace shares the same name. Override the workspace with the other dependency
for dep in &mut package_dependencies[0..dependencies_count as usize] {
if dep.name_hash == name_hash && dep.behavior.is_workspace() {
*dep = Dependency {
behavior: group.behavior,
name: external_alias.value,
name_hash: external_alias.hash,
version: dependency_version,
};
return Ok(None);
}
// A versionless workspace member satisfies a wildcard range
// (the resolver links it; see the resolve-from-workspace rule
// in `get_or_put_resolved_package`). Apply the same rule here
// so re-parsing package.json yields the workspace-tagged
// dependency a loaded lockfile holds, instead of a mismatch
// that re-saves an unchanged lockfile on every install.
Comment thread
robobun marked this conversation as resolved.
Resolved
let satisfies = match workspace_version {
Some(workspace_version) => dependency_version
.npm()
.version
.satisfies(workspace_version, buf, buf),
None => {
workspace_path.is_some() && dependency_version.npm().version.is_star()
}
};

Check failure on line 1767 in src/install/lockfile/Package.rs

View check run for this annotation

Claude / Claude Code Review

Parse/resolver mismatch persists for pre-release-versioned workspace members with wildcard deps

The `match` here is mutually exclusive but the resolver's rule (`PackageManagerEnqueue.rs:2292-2298`) is an inclusive `||` — they diverge when a workspace member has a **pre-release** version (e.g. `1.0.0-beta`) and is depended on via `"*"`: `satisfies` returns false (pre-release requires `pre_matched`) but `is_star()` is true, so the resolver links the workspace while parse leaves the dep npm-tagged and the re-save loop this PR fixes for versionless members remains. Mirror the resolver exactly:
Comment thread
robobun marked this conversation as resolved.
Resolved
if pm.options.link_workspace_packages && satisfies {
// `String::sliced` takes `&'a self`; bind the unwrapped
// value so the borrow outlives the parse call.
Comment thread
robobun marked this conversation as resolved.
Resolved
let wp = workspace_path.unwrap();
let path = wp.sliced(buf);
if let Some(mut dep) = dependency::parse_with_tag(
external_alias.value,
Some(external_alias.hash),
path.slice,
dependency::version::Tag::Workspace,
&path,
Some(&mut *log),
Some(&mut *pm),
) {
// Whole-struct move so `Drop` frees the old npm
// chain; keep the existing `literal`.
Comment thread
robobun marked this conversation as resolved.
Resolved
dep.literal = dependency_version.literal;
dependency_version = dep;
}
} else if workspace_version.is_some() {
// It doesn't satisfy, but a workspace shares the same name. Override the workspace with the other dependency
for dep in &mut package_dependencies[0..dependencies_count as usize] {
if dep.name_hash == name_hash && dep.behavior.is_workspace() {
*dep = Dependency {
behavior: group.behavior,
name: external_alias.value,
name_hash: external_alias.hash,
version: dependency_version,
};
return Ok(None);
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions test/cli/install/bun-workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,69 @@ test("dependency on workspace without version in package.json", async () => {
}
});

test.concurrent(
"repeat install does not re-save the lockfile for a wildcard dependency on a versionless workspace member",
async () => {
using ctx = await setupTest();
const { packageDir, packageJson, env } = ctx;
await Promise.all([
write(
packageJson,
JSON.stringify({
name: "foo",
workspaces: ["packages/*"],
dependencies: {
"no-deps": "*",
},
}),
),
// no version field on purpose
write(join(packageDir, "packages", "no-deps", "package.json"), JSON.stringify({ name: "no-deps" })),
write(
join(packageDir, "packages", "pkg1", "package.json"),
JSON.stringify({
name: "pkg1",
dependencies: {
"no-deps": "*",
},
}),
),
]);

var { stderr, exited } = spawn({
cmd: [bunExe(), "install"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env,
});

var err = await stderr.text();
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("error:");
expect(await exited).toBe(0);

const lockBefore = await file(join(packageDir, "bun.lock")).text();
// the wildcard dependencies resolve to the workspace member, not the registry
expect(lockBefore).toContain("no-deps@workspace:packages/no-deps");

({ stderr, exited } = spawn({
cmd: [bunExe(), "install"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env,
}));

err = await stderr.text();
expect(err).not.toContain("Saved lockfile");
expect(err).not.toContain("error:");
expect(await exited).toBe(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

expect(await file(join(packageDir, "bun.lock")).text()).toBe(lockBefore);
},
);

test.concurrent("allowing negative workspace patterns", async () => {
using ctx = await setupTest();
const { packageDir, env } = ctx;
Expand Down
Loading