Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
a60131ebb4 fix: don't create empty node_modules when no dependencies exist
Fixes #5392

When running `bun install` on a package.json with no dependencies, an empty `node_modules` folder was being created. This change adds checks in both hoisted and isolated install strategies to skip node_modules creation when there are no packages to install.

The fix preserves node_modules creation when using filters like `--production` that may result in zero packages after filtering but had packages in the original lockfile.

Changes:
- Check both filtered and original dependency counts in hoisted install
- Check both store entries and lockfile packages in isolated install
- Return early with empty Summary if truly no dependencies
- Add regression test that verifies no node_modules created when empty

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-11 03:24:28 +00:00
3 changed files with 72 additions and 0 deletions

View File

@@ -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

View File

@@ -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");

View File

@@ -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);
});