Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/js_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
];

Expand Down
6 changes: 5 additions & 1 deletion src/jsc/RuntimeTranspilerCache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions test/cli/run/transpiler-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading