aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ai Hoshino <ambiguous404@gmail.com> 2023-08-26 16:14:40 +0800
committerGravatar GitHub <noreply@github.com> 2023-08-26 01:14:40 -0700
commit910daeff27ead119e15f35f6c1e0aa09d2aa7562 (patch)
tree417c6a9fa69dcfacd4520be908b889798923f97a
parente1dacf88d0778fb9f2aa6932dcc67f9980e03265 (diff)
downloadbun-910daeff27ead119e15f35f6c1e0aa09d2aa7562.tar.gz
bun-910daeff27ead119e15f35f6c1e0aa09d2aa7562.tar.zst
bun-910daeff27ead119e15f35f6c1e0aa09d2aa7562.zip
Fix the crash when importing a module that does not exist. (#4348)
Close: #4240
-rw-r--r--src/install/install.zig6
-rw-r--r--test/cli/install/bun-run.test.ts22
2 files changed, 28 insertions, 0 deletions
diff --git a/src/install/install.zig b/src/install/install.zig
index 7635220a7..868204ed6 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -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],
diff --git a/test/cli/install/bun-run.test.ts b/test/cli/install/bun-run.test.ts
index 4180c9716..781e11c43 100644
--- a/test/cli/install/bun-run.test.ts
+++ b/test/cli/install/bun-run.test.ts
@@ -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);
+});