Fix the crash when importing a module that does not exist. (#4348)

Close: #4240
This commit is contained in:
Ai Hoshino
2023-08-26 16:14:40 +08:00
committed by GitHub
parent e1dacf88d0
commit 910daeff27
2 changed files with 28 additions and 0 deletions

View File

@@ -1818,6 +1818,12 @@ pub const PackageManager = struct {
else => |pkg_id| pkg_id,
};
if (resolution_id == invalid_package_id) {
return .{
.not_found = {},
};
}
return .{
.resolution = .{
.resolution = this.lockfile.packages.items(.resolution)[resolution_id],

View File

@@ -234,3 +234,25 @@ for (const entry of await decompress(Buffer.from(buffer))) {
]);
expect(await exited2).toBe(0);
});
it("should not crash when downloading a non-existent module, issue#4240", async () => {
await writeFile(
join(run_dir, "test.js"),
`
import { prueba } from "pruebadfasdfasdkafasdyuif.js";
`,
);
const { exited: exited } = spawn({
cmd: [bunExe(), "test.js"],
cwd: run_dir,
stdin: null,
stdout: "pipe",
stderr: "pipe",
env: {
...env,
BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"),
},
});
// The exit code will not be 1 if it panics.
expect(await exited).toBe(1);
});