mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
fix(install): show dependency name when file: path resolution fails (#26340)
## Summary - When `bun install` encounters a stale lockfile with a `file:` dependency path that differs from the package.json, it now shows which dependency caused the issue instead of the misleading "Bun could not find a package.json file to install from" error. ## Test plan - Added regression test in `test/regression/issue/26337.test.ts` - Verified test fails with system bun (`USE_SYSTEM_BUN=1 bun test test/regression/issue/26337.test.ts`) - Verified test passes with debug build (`bun bd test test/regression/issue/26337.test.ts`) Fixes #26337 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -364,12 +364,14 @@ pub fn installWithManager(
|
||||
for (manager.lockfile.buffers.dependencies.items, 0..) |*dependency, dependency_i| {
|
||||
if (std.mem.indexOfScalar(PackageNameHash, all_name_hashes, dependency.name_hash)) |_| {
|
||||
manager.lockfile.buffers.resolutions.items[dependency_i] = invalid_package_id;
|
||||
try manager.enqueueDependencyWithMain(
|
||||
manager.enqueueDependencyWithMain(
|
||||
@truncate(dependency_i),
|
||||
dependency,
|
||||
invalid_package_id,
|
||||
false,
|
||||
);
|
||||
) catch |err| {
|
||||
addDependencyError(manager, dependency, err);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -380,12 +382,14 @@ pub fn installWithManager(
|
||||
if (dep.version.tag != .catalog) continue;
|
||||
|
||||
manager.lockfile.buffers.resolutions.items[dep_id] = invalid_package_id;
|
||||
try manager.enqueueDependencyWithMain(
|
||||
manager.enqueueDependencyWithMain(
|
||||
dep_id,
|
||||
dep,
|
||||
invalid_package_id,
|
||||
false,
|
||||
);
|
||||
) catch |err| {
|
||||
addDependencyError(manager, dep, err);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,12 +405,14 @@ pub fn installWithManager(
|
||||
if (mapping[counter_i] == invalid_package_id) {
|
||||
const dependency_i = counter_i + off;
|
||||
const dependency = manager.lockfile.buffers.dependencies.items[dependency_i];
|
||||
try manager.enqueueDependencyWithMain(
|
||||
manager.enqueueDependencyWithMain(
|
||||
dependency_i,
|
||||
&dependency,
|
||||
manager.lockfile.buffers.resolutions.items[dependency_i],
|
||||
false,
|
||||
);
|
||||
) catch |err| {
|
||||
addDependencyError(manager, &dependency, err);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1101,6 +1107,28 @@ pub fn getWorkspaceFilters(manager: *PackageManager, original_cwd: []const u8) !
|
||||
return .{ workspace_filters.items, install_root_dependencies };
|
||||
}
|
||||
|
||||
/// Adds a contextual error for a dependency resolution failure.
|
||||
/// This provides better error messages than just propagating the raw error.
|
||||
/// The error is logged to manager.log, and the install will fail later when
|
||||
/// manager.log.hasErrors() is checked.
|
||||
fn addDependencyError(manager: *PackageManager, dependency: *const Dependency, err: anyerror) void {
|
||||
const lockfile = manager.lockfile;
|
||||
const note = .{
|
||||
.fmt = "error occurred while resolving {f}",
|
||||
.args = .{bun.fmt.fmtPath(u8, lockfile.str(&dependency.realname()), .{
|
||||
.path_sep = switch (dependency.version.tag) {
|
||||
.folder => .auto,
|
||||
else => .any,
|
||||
},
|
||||
})},
|
||||
};
|
||||
|
||||
if (dependency.behavior.isOptional() or dependency.behavior.isPeer())
|
||||
manager.log.addWarningWithNote(null, .{}, manager.allocator, @errorName(err), note.fmt, note.args) catch unreachable
|
||||
else
|
||||
manager.log.addZigErrorWithNote(manager.allocator, err, note.fmt, note.args) catch unreachable;
|
||||
}
|
||||
|
||||
const security_scanner = @import("./security_scanner.zig");
|
||||
const std = @import("std");
|
||||
const installHoistedPackages = @import("../hoisted_install.zig").installHoistedPackages;
|
||||
|
||||
78
test/regression/issue/26337.test.ts
Normal file
78
test/regression/issue/26337.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
// https://github.com/oven-sh/bun/issues/26337
|
||||
// Test that `bun install` with a stale lockfile that has a `file:` dependency path
|
||||
// that differs from the package.json shows a helpful error message indicating which
|
||||
// dependency caused the issue, rather than the misleading "Bun could not find a
|
||||
// package.json file to install from" error.
|
||||
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { bunEnv, bunExe, tempDir } from "harness";
|
||||
|
||||
describe("issue #26337 - missing file: dependency error should show dependency name", () => {
|
||||
it("should show which dependency path is missing when lockfile has stale file: path", async () => {
|
||||
// Create a workspace with a valid file: dependency
|
||||
using dir = tempDir("issue-26337", {
|
||||
"package.json": JSON.stringify({
|
||||
name: "repro",
|
||||
dependencies: {
|
||||
"@scope/dep": "file:./packages/@scope/dep",
|
||||
},
|
||||
}),
|
||||
"packages/@scope/dep/package.json": JSON.stringify({
|
||||
name: "@scope/dep",
|
||||
version: "1.0.0",
|
||||
}),
|
||||
});
|
||||
|
||||
// First install to create a lockfile with the valid path
|
||||
await using installProc = Bun.spawn({
|
||||
cmd: [bunExe(), "install"],
|
||||
cwd: String(dir),
|
||||
env: bunEnv,
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
|
||||
// Consume streams to prevent buffer filling
|
||||
const [, , installExitCode] = await Promise.all([
|
||||
installProc.stdout.text(),
|
||||
installProc.stderr.text(),
|
||||
installProc.exited,
|
||||
]);
|
||||
expect(installExitCode).toBe(0);
|
||||
|
||||
// Now update the package.json to point to a non-existent path
|
||||
// This creates the stale lockfile scenario
|
||||
await Bun.write(
|
||||
`${dir}/package.json`,
|
||||
JSON.stringify({
|
||||
name: "repro",
|
||||
dependencies: {
|
||||
"@scope/dep": "file:./nonexistent/path",
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// Run bun install again - this should show a helpful error
|
||||
await using failProc = Bun.spawn({
|
||||
cmd: [bunExe(), "install"],
|
||||
cwd: String(dir),
|
||||
env: bunEnv,
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
|
||||
const [stdout, stderr, exitCode] = await Promise.all([
|
||||
failProc.stdout.text(),
|
||||
failProc.stderr.text(),
|
||||
failProc.exited,
|
||||
]);
|
||||
|
||||
// The error output should mention the dependency name
|
||||
const output = stdout + stderr;
|
||||
expect(output).toContain("@scope/dep");
|
||||
expect(output).toContain("error occurred while resolving");
|
||||
|
||||
// The install should fail
|
||||
expect(exitCode).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user