diff --git a/src/install/hoisted_install.zig b/src/install/hoisted_install.zig index 456921494c53b3..db33f9cff80907 100644 --- a/src/install/hoisted_install.zig +++ b/src/install/hoisted_install.zig @@ -43,6 +43,12 @@ pub fn installHoistedPackages( } } + // If there are no dependencies to install and the original lockfile had no dependencies either, don't create node_modules + // We still create node_modules if the original lockfile has packages (e.g., with --production filtering devDeps) + if (this.lockfile.buffers.hoisted_dependencies.items.len == 0 and original_tree_dep_ids.items.len == 0) { + return PackageInstall.Summary{}; + } + // If there was already a valid lockfile and so we did not resolve, i.e. there was zero network activity // the packages could still not be in the cache dir // this would be a common scenario in a CI environment diff --git a/src/install/isolated_install.zig b/src/install/isolated_install.zig index e4a44b501c301f..19500d6d2db5e6 100644 --- a/src/install/isolated_install.zig +++ b/src/install/isolated_install.zig @@ -614,6 +614,12 @@ pub fn installIsolatedPackages( }; }; + // If there are no packages to install and no lockfile packages, don't create node_modules + // We still create node_modules if lockfile has packages (e.g., with --production filtering devDeps) + if (store.entries.len == 0 and lockfile.packages.len == 0) { + return PackageInstall.Summary{}; + } + // setup node_modules/.bun const is_new_bun_modules = is_new_bun_modules: { const node_modules_path = bun.OSPathLiteral("node_modules"); diff --git a/test/regression/issue/5392.test.ts b/test/regression/issue/5392.test.ts new file mode 100644 index 00000000000000..c214860306fb47 --- /dev/null +++ b/test/regression/issue/5392.test.ts @@ -0,0 +1,60 @@ +import { expect, test } from "bun:test"; +import { bunEnv, bunExe, tempDir } from "harness"; +import * as fs from "node:fs"; + +test("bun install should not create node_modules when there are no dependencies - issue #5392", async () => { + using dir = tempDir("issue-5392", { + "package.json": JSON.stringify({ + name: "bun-install-test", + module: "index.ts", + type: "module", + devDependencies: {}, + peerDependencies: {}, + }), + }); + + await using proc = Bun.spawn({ + cmd: [bunExe(), "install"], + env: bunEnv, + cwd: String(dir), + stderr: "pipe", + stdout: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + // node_modules should not exist + const nodeModulesPath = `${dir}/node_modules`; + const nodeModulesExists = fs.existsSync(nodeModulesPath); + + expect(nodeModulesExists).toBe(false); + expect(exitCode).toBe(0); +}); + +test("bun install should create node_modules when there are dependencies", async () => { + using dir = tempDir("issue-5392-with-deps", { + "package.json": JSON.stringify({ + name: "bun-install-test-with-deps", + dependencies: { + "is-odd": "^3.0.1", + }, + }), + }); + + await using proc = Bun.spawn({ + cmd: [bunExe(), "install"], + env: bunEnv, + cwd: String(dir), + stderr: "pipe", + stdout: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + // node_modules should exist + const nodeModulesPath = `${dir}/node_modules`; + const nodeModulesExists = fs.existsSync(nodeModulesPath); + + expect(nodeModulesExists).toBe(true); + expect(exitCode).toBe(0); +});