From e59d3b74e54864241fed8cc3475e4f87ac4722fa Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 30 Aug 2026 17:25:27 +0000 Subject: [PATCH] Key the runtime transpiler cache on remove_cjs_module_wrapper The `bun -` and `bun -e` entry point is printed without the CommonJS wrapper and evaluated as a script. The same bytes run as a file are printed wrapped. Both transpiles had the same cache key, so one run was served the other's output: stdin printed nothing, or the file failed with "ReferenceError: module is not defined". `remove_cjs_module_wrapper` joins the hashed feature bools and the cache version moves from 27 to 28. --- src/js_parser/parser.rs | 3 +- src/jsc/RuntimeTranspilerCache.rs | 6 +++- test/cli/run/transpiler-cache.test.ts | 52 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/js_parser/parser.rs b/src/js_parser/parser.rs index 52d8fc0309ab..40dd04327955 100644 --- a/src/js_parser/parser.rs +++ b/src/js_parser/parser.rs @@ -355,7 +355,7 @@ pub mod Runtime { pub(crate) fn hash_for_runtime_transpiler(&self, hasher: &mut Wyhash) { debug_assert!(self.runtime_transpiler_cache.is_some()); - let bools: [bool; 17] = [ + let bools: [bool; 18] = [ self.top_level_await, self.auto_import_jsx, self.allow_runtime, @@ -373,6 +373,7 @@ pub mod Runtime { self.standard_decorators, self.lower_using, self.repl_mode, + self.remove_cjs_module_wrapper, // note that we do not include .inject_jest_globals, as we bail out of the cache entirely if this is true ]; diff --git a/src/jsc/RuntimeTranspilerCache.rs b/src/jsc/RuntimeTranspilerCache.rs index 90b1b4a461f9..307377164f8b 100644 --- a/src/jsc/RuntimeTranspilerCache.rs +++ b/src/jsc/RuntimeTranspilerCache.rs @@ -55,7 +55,11 @@ bun_core::declare_scope!(cache, visible); /// offsets picked by a header byte) plus a body of tagged records with /// u8/u16/u32 ids and implied slots dropped, instead of fixed u32 arrays. /// Version 27: ModuleInfo string table holds Latin-1 / UTF-16 bodies, not WTF-8. -const EXPECTED_VERSION: u32 = 27; +/// Version 28: `remove_cjs_module_wrapper` participates in the features hash. The +/// `bun -` and `bun -e` entry point is printed without the CommonJS wrapper, and +/// entries written before this shared a key with the wrapped output of the same +/// bytes run as a file. +const EXPECTED_VERSION: u32 = 28; /// Source files smaller than this are not written to / read from the on-disk /// transpiler cache. Originally 50 KiB, which excluded almost every file in a diff --git a/test/cli/run/transpiler-cache.test.ts b/test/cli/run/transpiler-cache.test.ts index 0f40a5eec949..a841988df876 100644 --- a/test/cli/run/transpiler-cache.test.ts +++ b/test/cli/run/transpiler-cache.test.ts @@ -320,6 +320,58 @@ describe("transpiler cache", () => { expect(newCacheCount()).toBe(0); // cache hit, order doesn't matter }); + // `bun -` prints the entry point without the CommonJS wrapper and evaluates + // it as a script. The same bytes run as a file are printed wrapped in + // `(function(exports, require, module, __filename, __dirname) {})` and the + // loader calls that function. The two transpiles must not share an entry. + describe("a CommonJS entry point from stdin does not share an entry with the same file", () => { + // stdin is transpiled with the tsx loader, so only a .tsx file agrees with + // it on the rest of the cache key. + const filler = "\n//" + Buffer.alloc(5 * 1024, "f").toString(); + const cjs = `console.log("ran", typeof module.exports);\nmodule.exports = { a: 1 };${filler}`; + + async function bunRunStdin(code: string) { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-"], + cwd: temp_dir, + env, + stdin: Buffer.from(code), + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode, signalCode: proc.signalCode }; + } + + test("file first, then stdin", async () => { + writeFileSync(join(temp_dir, "a.tsx"), cjs); + expect(await bunRun(join(temp_dir, "a.tsx"), env)).toSpawn("ran object"); + expect(newCacheCount()).toBe(1); + + // Served the wrapped output, the script evaluates to a function that is + // never called and prints nothing. + expect(await bunRunStdin(cjs)).toSpawn("ran object"); + expect(newCacheCount()).toBe(0); // different features hash: deleted + written + + expect(await bunRunStdin(cjs)).toSpawn("ran object"); + expect(newCacheCount()).toBe(0); // cache hit + }); + + test("stdin first, then file", async () => { + writeFileSync(join(temp_dir, "a.tsx"), cjs); + expect(await bunRunStdin(cjs)).toSpawn("ran object"); + expect(newCacheCount()).toBe(1); + + // Served the unwrapped output, the file run has no `module` binding and + // fails with "ReferenceError: module is not defined". + expect(await bunRun(join(temp_dir, "a.tsx"), env)).toSpawn("ran object"); + expect(newCacheCount()).toBe(0); + + expect(await bunRun(join(temp_dir, "a.tsx"), env)).toSpawn("ran object"); + expect(newCacheCount()).toBe(0); + }); + }); + // Serving the entry point from the cache must not change how the modules it // loads are resolved. Both of these are gated on the `has_loaded` flag, which // used to be set only on the path that runs the printer.