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 <noreply@anthropic.com>
This commit is contained in:
Claude Bot
2026-02-16 07:23:46 +00:00
parent 70b354aa04
commit 846f6aa577
3 changed files with 50 additions and 2 deletions

View File

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

View File

@@ -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 = .{},
};

View File

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