diff --git a/src/bun.js.zig b/src/bun.js.zig index bcd1af82d4df..b1391373498f 100644 --- a/src/bun.js.zig +++ b/src/bun.js.zig @@ -18,9 +18,14 @@ pub const Run = struct { bun.jsc.initialize(false); bun.analytics.Features.standalone_executable += 1; - const graph_ptr = try bun.default_allocator.create(bun.StandaloneModuleGraph); - graph_ptr.* = graph; - graph_ptr.set(); + // Use existing graph if already set (set early in cli.zig for execArgv case), + // otherwise create and set it now + const graph_ptr = bun.StandaloneModuleGraph.get() orelse blk: { + const ptr = try bun.default_allocator.create(bun.StandaloneModuleGraph); + ptr.* = graph; + ptr.set(); + break :blk ptr; + }; js_ast.Expr.Data.Store.create(); js_ast.Stmt.Data.Store.create(); diff --git a/src/cli.zig b/src/cli.zig index 5aa859a828c5..031d88883cb6 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -683,6 +683,12 @@ pub const Command = struct { if (try bun.StandaloneModuleGraph.fromExecutable(bun.default_allocator)) |graph| { var offset_for_passthrough: usize = 0; + // Set the graph globally early so loadConfig can check compile flags + // (e.g., disable_autoload_bunfig) before Command.init loads config + const graph_ptr = try bun.default_allocator.create(bun.StandaloneModuleGraph); + graph_ptr.* = graph; + graph_ptr.set(); + const ctx: *ContextData = brk: { if (graph.compile_exec_argv.len > 0) { const original_argv_len = bun.argv.len; diff --git a/src/cli/Arguments.zig b/src/cli/Arguments.zig index 2a11d8332013..90b0db7af9dd 100644 --- a/src/cli/Arguments.zig +++ b/src/cli/Arguments.zig @@ -295,6 +295,13 @@ fn getHomeConfigPath(buf: *bun.PathBuffer) ?[:0]const u8 { return null; } pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: Command.Context, comptime cmd: Command.Tag) OOM!void { + // For standalone executables, respect compile-time flag to disable bunfig autoloading + if (bun.StandaloneModuleGraph.get()) |graph| { + if (graph.flags.disable_autoload_bunfig) { + return; + } + } + var config_buf: bun.PathBuffer = undefined; if (comptime cmd.readGlobalConfig()) { if (!ctx.has_loaded_global_config) { diff --git a/test/bundler/bundler_compile_autoload.test.ts b/test/bundler/bundler_compile_autoload.test.ts index e4940237066a..7b6e0a6e99bb 100644 --- a/test/bundler/bundler_compile_autoload.test.ts +++ b/test/bundler/bundler_compile_autoload.test.ts @@ -213,6 +213,59 @@ console.log("PRELOAD"); }, }); + // Test that autoloadBunfig: false is respected when execArgv is set + // Regression test: execArgv was causing bunfig.toml to be loaded even when disabled + itBundled("compile/AutoloadBunfigDisabledWithExecArgv", { + compile: { + autoloadBunfig: false, + execArgv: ["--smol"], + }, + files: { + "/entry.ts": /* js */ ` + console.log("ENTRY"); + `, + }, + runtimeFiles: { + "/bunfig.toml": ` +preload = ["./preload.ts"] + `, + "/preload.ts": ` +console.log("PRELOAD"); + `, + }, + run: { + // When bunfig is disabled, preload should NOT execute even with execArgv + stdout: "ENTRY", + setCwd: true, + }, + }); + + // Test CLI backend: autoloadBunfig: false with execArgv + itBundled("compile/AutoloadBunfigDisabledWithExecArgvCLI", { + compile: { + autoloadBunfig: false, + execArgv: ["--smol"], + }, + backend: "cli", + files: { + "/entry.ts": /* js */ ` + console.log("ENTRY"); + `, + }, + runtimeFiles: { + "/bunfig.toml": ` +preload = ["./preload.ts"] + `, + "/preload.ts": ` +console.log("PRELOAD"); + `, + }, + run: { + stdout: "ENTRY", + setCwd: true, + }, + }); + // Test CLI backend with autoloadBunfig: true itBundled("compile/AutoloadBunfigEnabledCLI", { compile: {