From 846f6aa577119d2cb16ade0d709f279dd57b9aec Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Mon, 16 Feb 2026 07:23:46 +0000 Subject: [PATCH] fix(compile): enable package.json loading by default in compiled executables Compiled executables could not resolve external modules that use `main`, `exports`, or `module` fields in their package.json. This was a regression from #25340 which introduced `--compile-autoload-package-json` defaulting to `false`, causing the resolver to skip reading package.json files at runtime. Closes #27058 Co-Authored-By: Claude --- src/bun.js/api/JSBundler.zig | 2 +- src/cli.zig | 2 +- test/bundler/bundler_compile.test.ts | 48 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index 3d652f3b45..fa9120d6f6 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -276,7 +276,7 @@ pub const JSBundler = struct { autoload_dotenv: bool = true, autoload_bunfig: bool = true, autoload_tsconfig: bool = false, - autoload_package_json: bool = false, + autoload_package_json: bool = true, pub fn fromJS(globalThis: *jsc.JSGlobalObject, config: jsc.JSValue, allocator: std.mem.Allocator, compile_target: ?CompileTarget) JSError!?CompileOptions { var this = CompileOptions{ diff --git a/src/cli.zig b/src/cli.zig index ca3ea8c083..1dbacd8da9 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -477,7 +477,7 @@ pub const Command = struct { compile_autoload_dotenv: bool = true, compile_autoload_bunfig: bool = true, compile_autoload_tsconfig: bool = false, - compile_autoload_package_json: bool = false, + compile_autoload_package_json: bool = true, compile_executable_path: ?[]const u8 = null, windows: options.WindowsOptions = .{}, }; diff --git a/test/bundler/bundler_compile.test.ts b/test/bundler/bundler_compile.test.ts index c7758f2343..bdb82cf26e 100644 --- a/test/bundler/bundler_compile.test.ts +++ b/test/bundler/bundler_compile.test.ts @@ -564,6 +564,54 @@ describe("bundler", () => { }, compile: true, }); + // https://github.com/oven-sh/bun/issues/27058 + itBundled("compile/DynamicRequireWithPackageJsonMain", { + files: { + "/entry.tsx": /* tsx */ ` + const req = (x) => require(x); + const result = req('pkg-with-main'); + console.log(JSON.stringify(result)); + `, + "/node_modules/pkg-with-main/package.json": JSON.stringify({ name: "pkg-with-main", main: "lib/index.js" }), + "/node_modules/pkg-with-main/lib/index.js": "throw new Error('Must be runtime import.')", + }, + runtimeFiles: { + "/node_modules/pkg-with-main/package.json": JSON.stringify({ name: "pkg-with-main", main: "lib/index.js" }), + "/node_modules/pkg-with-main/lib/index.js": `module.exports = { hello: "world" };`, + }, + run: { + stdout: '{"hello":"world"}', + setCwd: true, + }, + compile: true, + }); + // https://github.com/oven-sh/bun/issues/27058 + itBundled("compile/DynamicRequireWithPackageJsonExports", { + files: { + "/entry.tsx": /* tsx */ ` + const req = (x) => require(x); + const result = req('pkg-with-exports'); + console.log(JSON.stringify(result)); + `, + "/node_modules/pkg-with-exports/package.json": JSON.stringify({ + name: "pkg-with-exports", + exports: { ".": "./src/entry.js" }, + }), + "/node_modules/pkg-with-exports/src/entry.js": "throw new Error('Must be runtime import.')", + }, + runtimeFiles: { + "/node_modules/pkg-with-exports/package.json": JSON.stringify({ + name: "pkg-with-exports", + exports: { ".": "./src/entry.js" }, + }), + "/node_modules/pkg-with-exports/src/entry.js": `module.exports = { status: "ok" };`, + }, + run: { + stdout: '{"status":"ok"}', + setCwd: true, + }, + compile: true, + }); // see comment in `usePackageManager` for why this is a test itBundled("compile/NoAutoInstall", { files: {