diff --git a/scripts/splitting-fuzz.ts b/scripts/splitting-fuzz.ts index 1a4352e07bd8..90fcee3c57cc 100644 --- a/scripts/splitting-fuzz.ts +++ b/scripts/splitting-fuzz.ts @@ -1,31 +1,45 @@ -// bun scripts/splitting-fuzz.ts [first seed] +// bun scripts/splitting-fuzz.ts [first seed] [--unsplit [--baseline=]] // Runs test/bundler/splitting-fuzz.ts over many seeds against and prints the seeds where chunk folding made a -// bundle worse than the unfolded one (rerun a seed with `` = 1 to look at it). +// bundle worse than the unfolded one (rerun a seed with `` = 1 to look at it). With --unsplit it builds each +// graph without code splitting and prints the seeds where the bundle differs from the unbundled program. With a +// baseline binary it prints the seeds where the bundle is worse than the one that the baseline builds. import { resolve } from "node:path"; -import { checkGraph } from "../test/bundler/splitting-fuzz"; +import { checkGraph, checkUnsplit } from "../test/bundler/splitting-fuzz"; -const [bun, count = "100", first = "1"] = process.argv.slice(2); -if (!bun) { - console.error("usage: bun scripts/splitting-fuzz.ts [first seed]"); +const flags = process.argv.slice(2).filter(arg => arg.startsWith("--")); +const [bun, count = "100", first = "1"] = process.argv.slice(2).filter(arg => !arg.startsWith("--")); +const unsplit = flags.includes("--unsplit"); +const baselineFlag = flags.find(flag => flag.startsWith("--baseline=")); +if (!bun || (baselineFlag && !unsplit)) { + console.error("usage: bun scripts/splitting-fuzz.ts [first seed] [--unsplit [--baseline=]]"); process.exit(1); } -const target = Bun.which(bun) ?? resolve(bun); -const tally = { ok: 0, skipped: 0, worse: 0, folded: 0 }; +const binary = (name: string) => Bun.which(name) ?? resolve(name); +const target = binary(bun); +const baseline = baselineFlag ? binary(baselineFlag.slice("--baseline=".length)) : undefined; +const tally = { ok: 0, skipped: 0, worse: 0, different: 0, folded: 0, unjudged: 0 }; const parallel = 8; const end = Number(first) + Number(count); for (let seed = Number(first); seed < end; seed += parallel) { const batch = []; - for (let s = seed; s < Math.min(end, seed + parallel); s++) batch.push(checkGraph(target, s)); + for (let s = seed; s < Math.min(end, seed + parallel); s++) { + batch.push(unsplit ? checkUnsplit(target, s, baseline) : checkGraph(target, s)); + } for (const result of await Promise.all(batch)) { tally[result.status]++; - if (result.folded) tally.folded++; - if (result.status !== "ok") - console.log(`seed ${result.seed} ${result.status}:\n ` + result.problems.slice(0, 6).join("\n ")); + if ("folded" in result && result.folded) tally.folded++; + const unjudged = "unjudged" in result ? result.unjudged.map(why => "no verdict: " + why) : []; + if (unjudged.length) tally.unjudged++; + if (result.status !== "ok" || unjudged.length) { + console.log( + `seed ${result.seed} ${result.status}:\n ` + [...result.problems.slice(0, 6), ...unjudged].join("\n "), + ); + } } } console.log(JSON.stringify({ graphs: Number(count), firstSeed: Number(first), ...tally })); // Folding changes about one graph in sixteen. -if (tally.folded === 0 && Number(count) >= 200 && tally.skipped < Number(count)) { +if (!unsplit && tally.folded === 0 && Number(count) >= 200 && tally.skipped < Number(count)) { console.error("folding changed no bundle: does read foldChunksForTesting (--expose-internals gate)?"); process.exit(1); } diff --git a/src/bundler/LinkerContext.rs b/src/bundler/LinkerContext.rs index f51818fb9c88..21870d1c9fbc 100644 --- a/src/bundler/LinkerContext.rs +++ b/src/bundler/LinkerContext.rs @@ -5109,11 +5109,15 @@ pub struct StmtList { pub(crate) all_stmts: Vec, } +/// The statements a wrapper runs before its body. They keep source order until one is for an async dependency. pub struct InsideWrapperPrefix { pub(crate) stmts: Vec, pub(crate) sync_dependencies_end: usize, // if true it will exist at `sync_dependencies_end` pub(crate) has_async_dependency: bool, + in_source_order: bool, + /// While `in_source_order`: the index in `stmts` of each sync dependency. + sync_dependency_indices: Vec, } impl InsideWrapperPrefix { @@ -5122,6 +5126,8 @@ impl InsideWrapperPrefix { stmts: Vec::new(), sync_dependencies_end: 0, has_async_dependency: false, + in_source_order: true, + sync_dependency_indices: Vec::new(), } } @@ -5131,6 +5137,8 @@ impl InsideWrapperPrefix { self.stmts.clear(); self.sync_dependencies_end = 0; self.has_async_dependency = false; + self.in_source_order = true; + self.sync_dependency_indices.clear(); } } @@ -5146,25 +5154,41 @@ impl InsideWrapperPrefix { } fn append_sync_dependency(&mut self, call_expr: Expr) -> Result<(), AllocError> { - self.stmts.insert( - self.sync_dependencies_end, - Stmt::alloc( - S::SExpr { - value: call_expr, - ..Default::default() - }, - call_expr.loc, - ), + let stmt = Stmt::alloc( + S::SExpr { + value: call_expr, + ..Default::default() + }, + call_expr.loc, ); + if self.in_source_order { + self.sync_dependency_indices.push(self.stmts.len()); + self.stmts.push(stmt); + return Ok(()); + } + self.stmts.insert(self.sync_dependencies_end, stmt); self.sync_dependencies_end += 1; Ok(()) } + /// From the first async dependency on: each sync dependency, the `await`, then the other statements. + pub(crate) fn leave_source_order(&mut self) { + if !self.in_source_order { + return; + } + self.in_source_order = false; + for index in self.sync_dependency_indices.drain(..) { + self.stmts[self.sync_dependencies_end..=index].rotate_right(1); + self.sync_dependencies_end += 1; + } + } + fn append_async_dependency( &mut self, call_expr: Expr, promise_all_ref: Ref, ) -> Result<(), AllocError> { + self.leave_source_order(); if !self.has_async_dependency { self.has_async_dependency = true; self.stmts.insert( @@ -5309,3 +5333,192 @@ pub enum StmtListWhich { InsideWrapperSuffix, AllStmts, } + +#[cfg(test)] +mod inside_wrapper_prefix_tests { + use super::*; + use bun_ast::RefTag; + use bun_ast::expr::Data as ExprData; + use bun_ast::stmt::Data as StmtData; + + /// One statement of a list, by what the linker appends for it. + #[derive(Clone, Copy, PartialEq, Eq)] + enum Kind { + /// `require_x()`, `__reExport`, the `init_x()` of an `export *` of a sync module + NonDependency, + /// `init_x()` of an `import` of a sync module + Sync, + /// `init_x()` of an `import` of an async module + Async, + /// the `init_x()` of an `export *` of an async module + AsyncExportStar, + } + const KINDS: [Kind; 4] = [ + Kind::NonDependency, + Kind::Sync, + Kind::Async, + Kind::AsyncExportStar, + ]; + + /// A printed statement: the position of its source statement, or the positions that one `await` joins. + #[derive(Debug, PartialEq, Eq)] + enum Printed { + Statement(i32), + Await(Vec), + } + + fn promise_all() -> Ref { + Ref::new(1, 0, RefTag::Symbol) + } + + fn call_at(position: usize) -> Expr { + let loc = Loc { + start: position as i32, + }; + Expr::init( + E::Call { + target: Expr::init_identifier(Ref::new(2, 0, RefTag::Symbol), loc), + ..Default::default() + }, + loc, + ) + } + + fn append(prefix: &mut InsideWrapperPrefix, list: &[Kind]) { + for (position, kind) in list.iter().enumerate() { + let call = call_at(position); + let statement = Stmt::alloc( + S::SExpr { + value: call, + ..Default::default() + }, + call.loc, + ); + match kind { + Kind::NonDependency => prefix.append_non_dependency(statement).unwrap(), + Kind::Sync => prefix.append_sync_dependency(call).unwrap(), + Kind::Async => prefix.append_async_dependency(call, promise_all()).unwrap(), + Kind::AsyncExportStar => { + prefix.leave_source_order(); + prefix.append_non_dependency(statement).unwrap(); + } + } + } + } + + fn printed(prefix: &InsideWrapperPrefix) -> Vec { + prefix + .stmts + .iter() + .map(|stmt| { + let StmtData::SExpr(statement) = stmt.data else { + unreachable!() + }; + let ExprData::EAwait(awaited) = statement.value.data else { + return Printed::Statement(stmt.loc.start); + }; + let ExprData::ECall(call) = awaited.value.data else { + unreachable!() + }; + let ExprData::EIdentifier(target) = call.target.data else { + unreachable!() + }; + if !target.ref_.eql(promise_all()) { + return Printed::Await(vec![awaited.value.loc.start]); + } + let ExprData::EArray(joined) = call.args.slice()[0].data else { + unreachable!() + }; + Printed::Await( + joined + .items + .slice() + .iter() + .map(|item| item.loc.start) + .collect(), + ) + }) + .collect() + } + + /// Source order with no async kind, else each sync dependency, one `await`, then the other statements. + fn expected(list: &[Kind]) -> Vec { + let positions = |kinds: &[Kind]| -> Vec { + (0..list.len()) + .filter(|&position| kinds.contains(&list[position])) + .map(|position| position as i32) + .collect() + }; + if !list.contains(&Kind::Async) && !list.contains(&Kind::AsyncExportStar) { + return positions(&KINDS) + .into_iter() + .map(Printed::Statement) + .collect(); + } + let mut out: Vec = positions(&[Kind::Sync]) + .into_iter() + .map(Printed::Statement) + .collect(); + let awaited = positions(&[Kind::Async]); + if !awaited.is_empty() { + out.push(Printed::Await(awaited)); + } + out.extend( + positions(&[Kind::NonDependency, Kind::AsyncExportStar]) + .into_iter() + .map(Printed::Statement), + ); + out + } + + fn lists(max_len: usize) -> Vec> { + let mut all: Vec> = vec![Vec::new()]; + let mut from = 0; + for _ in 0..max_len { + let to = all.len(); + for index in from..to { + for kind in KINDS { + let mut longer = all[index].clone(); + longer.push(kind); + all.push(longer); + } + } + from = to; + } + all + } + + fn with_stores(run: impl FnOnce()) { + bun_ast::expr::data::Store::create(); + bun_ast::stmt::data::Store::create(); + run(); + } + + #[test] + fn every_list_prints_in_its_layout() { + with_stores(|| { + for list in lists(7) { + let mut prefix = InsideWrapperPrefix::init(); + append(&mut prefix, &list); + assert_eq!(printed(&prefix), expected(&list)); + } + }); + } + + #[test] + fn reset_starts_a_list_over() { + with_stores(|| { + let all = lists(4); + let mut prefix = InsideWrapperPrefix::init(); + for first in &all { + for second in &all { + prefix.reset(); + append(&mut prefix, first); + prefix.reset(); + append(&mut prefix, second); + assert_eq!(printed(&prefix), expected(second)); + } + } + }); + } +} diff --git a/src/bundler/linker_context/convertStmtsForChunk.rs b/src/bundler/linker_context/convertStmtsForChunk.rs index c3794a8e6bf8..c16b6c808f71 100644 --- a/src/bundler/linker_context/convertStmtsForChunk.rs +++ b/src/bundler/linker_context/convertStmtsForChunk.rs @@ -274,6 +274,9 @@ pub(crate) fn convert_stmts_for_chunk( let flag = c.graph.meta.items_flags()[other_source_index]; let wrapper_ref = c.graph.ast.items_wrapper_ref()[other_source_index]; if flag.wrap == WrapKind::Esm && wrapper_ref.is_valid() { + if flag.is_async_or_has_async_dependency { + stmts.inside_wrapper_prefix.leave_source_order(); + } stmts .inside_wrapper_prefix .append_non_dependency(Stmt::alloc( diff --git a/test/aot/code-from-disk.test.ts b/test/aot/code-from-disk.test.ts index 461a3afac7ae..882f4436a8d3 100644 --- a/test/aot/code-from-disk.test.ts +++ b/test/aot/code-from-disk.test.ts @@ -533,7 +533,7 @@ const cases: Record = { prints: "a file of the program asks\nthe last file runs\nthe entry point runs\nthe file in front gets 42\nit gets 42\n", }, - // A bundle runs the module that throws in front of the CommonJS file. Compiled ahead of time the files run in the order of `bun run`. + // In a bundle the entry point is running when the CommonJS file requires it, and the `require()` answers at once. Compiled ahead of time it runs the module that throws first, as `bun run` does. "a file of the program that requires the entry point then catches what a module of the graph throws": { program: { "main.ts": @@ -558,7 +558,8 @@ const cases: Record = { require("node:fs").writeFileSync("written.mjs", "import " + JSON.stringify(Bun.main) + ";");`, "written.mjs": "", }, - prints: "the last module runs\nnobody catches RangeError thrown by the last module\n", + prints: + "a file of the program asks\nit gets 42\nthe last module runs\nnobody catches RangeError thrown by the last module\n", differs: "a file of the program asks\nthe last module runs\nit catches RangeError thrown by the last module\nnobody catches RangeError thrown by the last module\n", }, diff --git a/test/bundler/bundler_bytecode_portable.test.ts b/test/bundler/bundler_bytecode_portable.test.ts index 903ce546536c..6862850c7d36 100644 --- a/test/bundler/bundler_bytecode_portable.test.ts +++ b/test/bundler/bundler_bytecode_portable.test.ts @@ -517,10 +517,10 @@ describe("bytecode cache portability", () => { }, }, "bun build --bytecode libraries.js": { - "js": "7b8ce5d1861461086be174e60797d52794328828458a2086f4d49b39a3f8b28a", + "js": "eb0e822054bfe7b763045bbc5d10485c137da910b74263709477ac03169235da", "jsc": { - "bytes": 21177808, - "sha256": "62bc4de3ff1aba67c153ff59c0e42e4d999b90ef6b87067005bcff30cc9d5e2c", + "bytes": 21177816, + "sha256": "a69a98ecdd8af96c29cbbd1a18b8276551c6ee378c8d2a4d523e9971f7499628", }, }, "bun build --bytecode lodash/lodash.js": { diff --git a/test/bundler/bundler_edgecase.test.ts b/test/bundler/bundler_edgecase.test.ts index b65ae9be6569..3e733f532db9 100644 --- a/test/bundler/bundler_edgecase.test.ts +++ b/test/bundler/bundler_edgecase.test.ts @@ -1,9 +1,10 @@ import { describe, expect, test } from "bun:test"; -import { bunEnv, bunExe, tempDir } from "harness"; +import { bunEnv, bunExe, nodeExeAtLeast, tempDir } from "harness"; import { readdirSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { unsupported } from "../aot/harness/commands"; -import { type BundlerTestInput, decodeSourceMappingsLine, itBundled } from "./expectBundled"; +import { type BundlerTestInput, decodeSourceMappingsLine, dedent, itBundled } from "./expectBundled"; +import { checkUnsplit, run } from "./splitting-fuzz"; // A public path composes with the referenced file's path relative to the output // directory, never relative to the importing chunk (esbuild's semantics). @@ -4440,6 +4441,632 @@ describe("bundler", () => { run: { stdout: "m user" }, }); + // A file calls its dependencies before its own code: `init_x()` for a wrapped + // ES module, `require_x()` for a CommonJS one. An `import()` or a `require()` + // of a module anywhere in the bundle is what wraps it. The calls keep the + // order of the statements while none of them is for an async dependency. + // + // node is the reference for that order. Where it is installed, a case also + // runs node on its sources and expects `nodePrints`: the output of the + // bundle, unless the case pins output that differs. + const node = nodeExeAtLeast("20.0.0"); + function itBundledAndNode(id: string, opts: BundlerTestInput & { run: { stdout?: string } }, nodePrints?: string) { + test.skipIf(!node).concurrent(`${id}: node on the sources`, async () => { + using dir = tempDir("bundle-and-node", { + "package.json": `{ "type": "module" }`, + ...(opts.files as Record), + }); + const { stdout, exitCode } = await run([node!, "entry.js"], String(dir), bunEnv); + expect({ stdout, exitCode }).toEqual({ stdout: dedent(nodePrints ?? opts.run.stdout!) + "\n", exitCode: 0 }); + }); + return itBundled(id, opts); + } + + // The table: the order in which a file starts two dependencies. Each one logs + // its name when it starts. The expected output is the order of the statements. + // A pair with an async dependency is todo: the sync ES import, or the + // `await`, still comes first there, as in every release since 1.2.23. + const dependencies: Record = { + "CommonJS import": { statement: `import "./c.cjs";`, logs: "c" }, + "external import": { statement: `import "ext";`, logs: "ext" }, + "export * of CommonJS": { statement: `export * from "./c.cjs";`, logs: "c" }, + "export * of sync ES": { statement: `export * from "./p.js";`, logs: "p", file: "p.js" }, + "export * of async ES": { statement: `export * from "./a.js";`, logs: "a", file: "a.js" }, + "sync ES import": { statement: `import "./s.js";`, logs: "s", file: "s.js" }, + "async ES import": { statement: `import "./b.js";`, logs: "b", file: "b.js" }, + }; + const pairs: [first: string, second: string, todo?: boolean][] = [ + ["CommonJS import", "sync ES import"], + ["external import", "sync ES import"], + ["export * of CommonJS", "sync ES import"], + ["export * of sync ES", "sync ES import"], + ["CommonJS import", "async ES import", true], + ["export * of CommonJS", "async ES import", true], + ["export * of sync ES", "async ES import", true], + ["export * of async ES", "sync ES import", true], + ["export * of async ES", "async ES import", true], + ["async ES import", "sync ES import", true], + ]; + for (const [first, second, todo] of pairs) { + for (const importer of ["a file with no wrapper", "a wrapped file"]) { + test.todoIf(!!todo).concurrent(`edgecase/DependencyStartOrder: ${first}, ${second} in ${importer}`, async () => { + const list = `${dependencies[first].statement}\n${dependencies[second].statement}\nconsole.log("m");\n`; + const wrapped = [first, second].flatMap(name => dependencies[name].file ?? []); + // The external import prints as `require("ext")` in cjs output only. + const format = first === "external import" ? "cjs" : "esm"; + using dir = tempDir("dependency-start-order", { + "package.json": `{ "type": "module" }`, + "c.cjs": `console.log("c");\nmodule.exports = { c: 1 };\n`, + "p.js": `console.log("p");\nexport const p = 1;\n`, + "a.js": `console.log("a");\nawait 0;\nexport const a = 1;\n`, + "s.js": `console.log("s");\n`, + "b.js": `console.log("b");\nawait 0;\n`, + "node_modules/ext/package.json": `{ "name": "ext", "main": "index.js" }`, + "node_modules/ext/index.js": `console.log("ext");\n`, + ...(importer === "a wrapped file" + ? { "m.js": list, "entry.js": `import("./m.js");\n` } + : { "entry.js": `${list}export const lazy = () => [${wrapped.map(file => `import("./${file}")`)}];\n` }), + }); + const cwd = String(dir); + const bundle = `out.${format === "cjs" ? "cjs" : "js"}`; + const build = await run( + [ + bunExe(), + "build", + "entry.js", + `--outfile=${bundle}`, + `--format=${format}`, + "--target=node", + "--external", + "ext", + ], + cwd, + bunEnv, + ); + expect({ stderr: build.stderr, exitCode: build.exitCode }).toEqual({ stderr: "", exitCode: 0 }); + + const order = `${dependencies[first].logs}\n${dependencies[second].logs}\nm\n`; + if (node) { + const { stdout, exitCode } = await run([node, "entry.js"], cwd, bunEnv); + expect({ stdout, exitCode }).toEqual({ stdout: order, exitCode: 0 }); + } + expect(await run([bunExe(), bundle], cwd, bunEnv)).toEqual({ stdout: order, stderr: "", exitCode: 0 }); + }); + } + } + + // The next cases have no async dependency in the list under test. + // mixed.js has `import` and `module.exports`, so it keeps a `__commonJS` + // wrapper, and that wraps its imports with no `import()` in the bundle. node + // and bun do not run this source. esbuild and bun 1.2.22 print this order. + itBundled("edgecase/WrappedESMImportAfterCommonJSImportInCommonJSWrapper", { + files: { + "/entry.js": /* js */ ` + import mixed from "./mixed.js"; + console.log("entry", mixed.v); + `, + "/mixed.js": /* js */ ` + import c from "./c.cjs"; + import "./s.js"; + console.log("mixed body"); + module.exports = { v: c.v }; + `, + "/c.cjs": /* js */ ` + console.log("c"); + globalThis.ready = true; + module.exports = { v: 1 }; + `, + "/s.js": /* js */ `console.log("s, ready =", globalThis.ready);`, + }, + format: "esm", + run: { + stdout: ` + c + s, ready = true + mixed body + entry 1 + `, + }, + }); + // native.cjs throws, so registry.js does not start. The second `import()` + // throws the same error again. + itBundledAndNode("edgecase/ThrowingCommonJSImportBeforeWrappedESMImport", { + files: { + "/entry.js": /* js */ ` + for (const attempt of ["first", "second"]) { + try { + await import("./feature.js"); + } catch (e) { + console.log(attempt, "caught:", e.message, "| registry:", globalThis.registry); + } + } + const { reg } = await import("./registry.js"); + console.log("registry later:", reg); + `, + "/feature.js": /* js */ ` + import "./native.cjs"; + import "./registry.js"; + console.log("feature body"); + `, + "/native.cjs": /* js */ ` + console.log("native start"); + throw new Error("unsupported platform"); + `, + "/registry.js": /* js */ ` + console.log("registry body"); + globalThis.registry = "filled"; + export const reg = "filled"; + `, + }, + format: "esm", + run: { + stdout: ` + native start + first caught: unsupported platform | registry: undefined + second caught: unsupported platform | registry: undefined + registry body + registry later: filled + `, + }, + }); + // The entry has no wrapper. u.js has none too, so its code ends the first + // list of dependency statements of the entry. The second list has no async + // dependency: d.cjs runs before s. + itBundledAndNode("edgecase/CommonJSImportBeforeWrappedESMImportAfterUnwrappedImport", { + files: { + "/entry.js": /* js */ ` + import "./a.js"; + import "./u.js"; + import "./d.cjs"; + import "./s.js"; + console.log("entry"); + export const lazy = () => [import("./a.js"), import("./s.js")]; + `, + "/a.js": /* js */ ` + console.log("a start"); + await 0; + `, + "/u.js": /* js */ `console.log("u");`, + "/d.cjs": /* js */ ` + console.log("d"); + globalThis.ready = true; + `, + "/s.js": /* js */ `console.log("s, ready =", globalThis.ready);`, + }, + format: "esm", + run: { + stdout: ` + a start + u + d + s, ready = true + entry + `, + }, + }); + // With `--splitting`, a file has a wrapper only when CommonJS reaches it. + // load.cjs makes the `import()`, so both modes wrap x and its dependencies. + for (const splitting of [false, true]) { + const bundled = splitting ? itBundled : itBundledAndNode; + bundled(`edgecase/WrappedESMImportAfterCommonJSImportBehindCommonJS${splitting ? "Splitting" : ""}`, { + files: { + "/entry.js": /* js */ ` + import load from "./load.cjs"; + await load(); + console.log("entry"); + `, + "/load.cjs": /* js */ `module.exports = () => import("./x.js");`, + "/x.js": /* js */ ` + import c from "./c.cjs"; + import "./b.js"; + console.log("x body", c.value); + `, + "/b.js": /* js */ `console.log("b, ready =", globalThis.ready);`, + "/c.cjs": /* js */ ` + console.log("c"); + globalThis.ready = true; + module.exports = { value: "C" }; + `, + }, + format: "esm", + splitting, + outdir: "/out", + run: { + file: "/out/entry.js", + stdout: ` + c + b, ready = true + x body C + entry + `, + }, + }); + } + + // `a.mjs` imports a CommonJS file, then an ES module that a lazy route also + // reaches, so the bundle wraps both. `init_w()` stays below the `require_c()` + // of the import before it: every bundle prints what the source prints. + // `exports.x = 1` keeps its wrapper because `Object.keys(d)` holds the + // default import as a value. `module.exports = {...}` always keeps it. + for (const [name, commonjs] of [ + ["LiftedExportsHeldAsValue", `exports.x = 1;`], + ["ModuleExportsObject", `module.exports = { x: 1 };`], + ]) { + test.concurrent(`edgecase/WrappedESMImportAfterCommonJSImport${name}`, async () => { + using dir = tempDir("cjs-then-wrapped-esm", { + "entry.mjs": `import "./a.mjs";\nimport("./lazy.mjs");\n`, + "a.mjs": `import d from "./c.cjs";\nimport "./w.mjs";\nconsole.log("a", Object.keys(d).join());\n`, + "c.cjs": `console.log("cjs");\nglobalThis.ready = true;\n${commonjs}\n`, + "lazy.mjs": `import "./w.mjs";\n`, + "w.mjs": `console.log("w, ready =", globalThis.ready);\n`, + }); + const cwd = String(dir); + const source = await run([bunExe(), "entry.mjs"], cwd, bunEnv); + expect(source).toEqual({ stdout: "cjs\nw, ready = true\na x\n", stderr: "", exitCode: 0 }); + if (node) expect((await run([node, "entry.mjs"], cwd, bunEnv)).stdout).toBe(source.stdout); + + const modes = { + bun: ["--target=bun"], + node: ["--target=node"], + browser: ["--target=browser"], + minify: ["--target=bun", "--minify"], + }; + const bundles = await Promise.all( + Object.entries(modes).map(async ([mode, flags]) => { + const build = await run([bunExe(), "build", ...flags, "entry.mjs", `--outdir=out-${mode}`], cwd, bunEnv); + expect({ stderr: build.stderr, exitCode: build.exitCode }).toEqual({ stderr: "", exitCode: 0 }); + return [mode, await run([bunExe(), `out-${mode}/entry.js`], cwd, bunEnv)]; + }), + ); + expect(Object.fromEntries(bundles)).toEqual({ bun: source, node: source, browser: source, minify: source }); + }); + } + + // Random graphs, each run by bun from its sources and as a bundle without + // code splitting (splitting-fuzz.ts). The bundle reads every value and keeps + // every order of top-level effects that the sources have. Each graph has a + // CommonJS file before an ES import that reads what it sets. + for (const seed of [1, 9, 47, 62]) { + test.concurrent(`edgecase/UnsplitBundleRunsLikeItsSource: graph ${seed}`, async () => { + expect(await checkUnsplit(bunExe(), seed)).toEqual({ seed, status: "ok", problems: [], unjudged: [] }); + }); + } + + // The next cases pin output that node does not print for the sources. The + // last argument is what node prints. In each, a statement that now runs + // where the source has it starts a module sooner than node does, because an + // `import()` of an async module of the bundle starts that module inside the + // call. bun 1.2.22 and esbuild print the same. The order of #22704 ran store + // first and printed node's output. + // router.cjs calls `import()` on page, and page starts before store. + itBundledAndNode( + "edgecase/ImportCallInCommonJSDependencyRunsBeforeLaterImport", + { + files: { + "/entry.js": /* js */ ` + import "./app.js"; + export const lazy = () => import("./app.js"); + `, + "/app.js": /* js */ ` + import "./router.cjs"; + import "./store.js"; + `, + "/router.cjs": /* js */ `import("./page.js");`, + "/store.js": /* js */ `globalThis.store = "ready";`, + "/page.js": /* js */ ` + console.log("page sees store:", globalThis.store); + await 0; + `, + }, + format: "esm", + run: { stdout: "page sees store: undefined" }, + }, + "page sees store: ready", + ); + // The same after the code of an unwrapped module, in an entry that has an + // async dependency before that module. + itBundledAndNode( + "edgecase/ImportCallInCommonJSDependencyRunsBeforeLaterImportAfterUnwrappedImport", + { + files: { + "/entry.js": /* js */ ` + import "./a.js"; + import "./u.js"; + import "./router.cjs"; + import "./store.js"; + export const lazy = () => [import("./a.js"), import("./store.js")]; + `, + "/a.js": /* js */ `await 0;`, + "/u.js": /* js */ `console.log("u");`, + "/router.cjs": /* js */ `import("./page.js");`, + "/store.js": /* js */ `globalThis.store = "ready";`, + "/page.js": /* js */ ` + console.log("page sees store:", globalThis.store); + await 0; + `, + }, + format: "esm", + run: { stdout: "u\npage sees store: undefined" }, + }, + "u\npage sees store: ready", + ); + // The same through `export *` of a sync ES module, with no CommonJS. + itBundledAndNode( + "edgecase/ImportCallInExportStarDependencyRunsBeforeLaterImport", + { + files: { + "/entry.js": /* js */ `await import("./barrel.js");`, + "/barrel.js": /* js */ ` + export * from "./router.js"; + import "./store.js"; + `, + "/router.js": /* js */ ` + import("./page.js"); + export const route = "/"; + `, + "/store.js": /* js */ `globalThis.store = "ready";`, + "/page.js": /* js */ ` + console.log("page sees store:", globalThis.store); + await 0; + `, + }, + format: "esm", + run: { stdout: "page sees store: undefined" }, + }, + "page sees store: ready", + ); + // page starts inside the `import()` of router.cjs and imports nav, and nav + // calls `import()` on page, which is running: that call throws. + itBundledAndNode( + "edgecase/ImportCallInCommonJSDependencyThenImportCallOfRunningModule", + { + files: { + "/entry.js": /* js */ ` + import "./router.cjs"; + import "./nav.js"; + console.log("entry done"); + `, + "/router.cjs": /* js */ `import("./page.js");`, + "/nav.js": /* js */ `import("./page.js").then(() => console.log("nav: page loaded"));`, + "/page.js": /* js */ ` + import "./nav.js"; + console.log("page start"); + await 0; + console.log("page end"); + `, + }, + format: "esm", + run: { error: "TypeError: undefined is not an object (evaluating 'init_page().then')" }, + }, + "entry done\npage start\npage end\nnav: page loaded", + ); + // legacy.cjs requires page, the file that imports it. bun runs the other + // imports of page inside that `require()`, and a bundle cannot: theme has + // not run. node does not run the sources. bun prints `theme`, + // `legacy renders dark`, `entry got function` for them when no package.json + // says `"type": "module"`, and so did the order of #22704. + itBundled("edgecase/CommonJSImportThatRequiresItsImporterRunsBeforeLaterImport", { + files: { + "/entry.js": /* js */ ` + const { render } = await import("./page.js"); + console.log("entry got", typeof render); + `, + "/page.js": /* js */ ` + import "./legacy.cjs"; + import { theme } from "./theme.js"; + export function render() { + return theme.name; + } + `, + "/legacy.cjs": /* js */ ` + const { render } = require("./page.js"); + console.log("legacy renders", render()); + `, + "/theme.js": /* js */ ` + console.log("theme"); + export const theme = { name: "dark" }; + `, + }, + format: "esm", + run: { error: "TypeError: undefined is not an object (evaluating 'theme.name')" }, + }); + // The import of the JSX runtime that the compiler adds is the last import of + // page.jsx in a bundle, so user.cjs runs before it. Unbundled it is the + // first statement of the file, and bun runs the runtime first. + itBundled("edgecase/CommonJSImportRunsBeforeGeneratedJSXImport", { + files: { + "/entry.js": /* js */ ` + const { default: Page } = await import("./page.jsx"); + console.log("entry got", typeof Page); + `, + "/page.jsx": /* jsx */ ` + import "./user.cjs"; + export default function Page() { + return
; + } + `, + "/user.cjs": /* js */ ` + console.log("user, runtime ran:", globalThis.runtime === true); + globalThis.user = true; + `, + "/node_modules/rt/package.json": `{ "name": "rt", "type": "module", "exports": { "./jsx-runtime": "./jsx-runtime.js", "./jsx-dev-runtime": "./jsx-runtime.js" } }`, + "/node_modules/rt/jsx-runtime.js": /* js */ ` + console.log("runtime, user ran:", globalThis.user === true); + globalThis.runtime = true; + export const jsx = (type, props) => ({ type, props }); + export const jsxs = jsx; + export const jsxDEV = jsx; + export const Fragment = "f"; + `, + }, + jsx: { runtime: "automatic", importSource: "rt" }, + format: "esm", + run: { + stdout: ` + user, runtime ran: false + runtime, user ran: true + entry got function + `, + }, + }); + + // The next cases have an async dependency in the list, so the list is: each + // sync ES import, the `await`, then the other statements. A CommonJS import + // that the source has first runs after the `await`. Their output is node's. + // The first three have no sync ES import in a list: they hold what source + // order up to the first async dependency would break. + // app calls `import()` on page in its first part, and page starts inside the + // call. legacy.cjs runs one job later, after the synchronous code of app. + itBundledAndNode("edgecase/CommonJSImportBeforeAsyncImportInImportCallTargetRunsAfterCallerCode", { + files: { + "/entry.js": /* js */ `import "./app.js";`, + "/app.js": /* js */ ` + import("./page.js"); + globalThis.appReady = true; + await 0; + `, + "/page.js": /* js */ ` + import "./legacy.cjs"; + import "./app.js"; + `, + "/legacy.cjs": /* js */ `console.log("legacy sees appReady:", globalThis.appReady);`, + }, + format: "esm", + run: { stdout: "legacy sees appReady: true" }, + }); + // a waits for b and c, and both import a. a is running, so b continues one + // job after its call, and so does c. legacy.cjs runs after the first part of b. + itBundledAndNode("edgecase/CommonJSImportBeforeAsyncImportRunsAfterWaitingImportCycleMember", { + files: { + "/entry.js": /* js */ ` + import "./a.js"; + export const lazy = () => import("./a.js"); + `, + "/a.js": /* js */ ` + import "./b.js"; + import "./c.js"; + `, + "/b.js": /* js */ ` + import "./a.js"; + globalThis.state = "b started"; + await 0; + `, + "/c.js": /* js */ ` + import "./legacy.cjs"; + import "./a.js"; + `, + "/legacy.cjs": /* js */ `console.log("legacy sees state:", globalThis.state);`, + }, + format: "esm", + run: { stdout: "legacy sees state: b started" }, + }); + // route waits for state and page at the same time. Both import config, which + // has ended. state continues one job after its call, and page too, so + // legacy.cjs runs after the body of state. + itBundledAndNode("edgecase/CommonJSImportBeforeAsyncImportRunsAfterEarlierSiblingBody", { + files: { + "/entry.js": /* js */ ` + import "./config.js"; + await import("./route.js"); + `, + "/config.js": /* js */ `await 0;`, + "/route.js": /* js */ ` + import "./state.js"; + import "./page.js"; + `, + "/state.js": /* js */ ` + import "./config.js"; + globalThis.state = { from: "state.js" }; + `, + "/page.js": /* js */ ` + import "./legacy.cjs"; + import "./config.js"; + `, + "/legacy.cjs": /* js */ `console.log("legacy sees state:", JSON.stringify(globalThis.state));`, + }, + format: "esm", + run: { stdout: `legacy sees state: {"from":"state.js"}` }, + }); + // An `export *` of an async module is an async dependency too. feature calls + // `import()` on page, and page starts inside the call. store runs before + // feature, so page reads what store sets. + itBundledAndNode("edgecase/ImportCallInAsyncExportStarDependencyRunsAfterLaterImport", { + files: { + "/entry.js": /* js */ `await import("./barrel.js");`, + "/barrel.js": /* js */ ` + export * from "./feature.js"; + import "./store.js"; + `, + "/feature.js": /* js */ ` + import("./page.js"); + await 0; + export const feature = 1; + `, + "/store.js": /* js */ `globalThis.store = "ready";`, + "/page.js": /* js */ ` + console.log("page sees store:", globalThis.store); + await 0; + `, + }, + format: "esm", + run: { stdout: "page sees store: ready" }, + }); + // The printed list of a wrapper that has an async dependency. It is not the + // order of the statements: node starts c, s, d and t before a ends. + itBundled("edgecase/WrapperWithAsyncDependencyKeepsDependencyLayout", { + files: { + "/entry.js": /* js */ `await import("./x.js");`, + "/x.js": /* js */ ` + import "./c.cjs"; + import "./s.js"; + import "./d.cjs"; + import "./t.js"; + import "./a.js"; + import "./e.cjs"; + import "./u.js"; + console.log("x"); + `, + "/c.cjs": /* js */ `console.log("c");`, + "/d.cjs": /* js */ `console.log("d");`, + "/e.cjs": /* js */ `console.log("e");`, + "/s.js": /* js */ `console.log("s");`, + "/t.js": /* js */ `console.log("t");`, + "/u.js": /* js */ `console.log("u");`, + "/a.js": /* js */ `await 0;`, + }, + format: "esm", + onAfterBundle(api) { + api.expectFile("/out.js").toContain(`var init_x = __esm(async () => { + init_s(); + init_t(); + init_u(); + await init_a(); + import_c = __toESM(require_c()); + import_d = __toESM(require_d()); + import_e = __toESM(require_e()); + console.log("x"); +});`); + }, + }); + // A CommonJS import, a sync ES import, then an async one: di runs first. + itBundledAndNode("edgecase/CommonJSImportBeforeWrappedESMImportWithLaterAsyncImport", { + todo: true, // the entry prints `init_di()` above `await init_late()` and `require_polyfill()`: `di sees P: undefined` + files: { + "/entry.js": /* js */ ` + import "./polyfill.cjs"; + import "./di.js"; + import "./late.js"; + console.log("entry"); + export const lazy = () => [import("./di.js"), import("./late.js")]; + `, + "/polyfill.cjs": /* js */ `globalThis.P = 1;`, + "/di.js": /* js */ `console.log("di sees P:", globalThis.P);`, + "/late.js": /* js */ `await 0;`, + }, + format: "esm", + run: { stdout: "di sees P: 1\nentry" }, + }); + for (const splitting of [false, true]) { itBundled(`edgecase/ImportCycleKeepsEvaluationOrder${splitting ? "Splitting" : ""}`, { files: { diff --git a/test/bundler/splitting-fuzz.ts b/test/bundler/splitting-fuzz.ts index f3e3d339ee1c..d91cc22dba70 100644 --- a/test/bundler/splitting-fuzz.ts +++ b/test/bundler/splitting-fuzz.ts @@ -10,6 +10,12 @@ // the unbundled program on some fact and the folded one does not. The facts compared are (1) each value read, and // (2) the relative order of the top-level effects. // +// `checkUnsplit` is a second check on the same graphs, built without code splitting. There every module that an +// `import()` or a `require()` reaches is wrapped in a function that its importers call, so the order that the bundle +// runs them in is the linker's to get right. The bundle must read every value and keep every order of top-level +// effects that the unbundled program has. Given a second binary as a baseline, it reports only what the baseline's +// bundle has right and this one does not. +// // scripts/splitting-fuzz.ts runs many seeds against a given binary. import { spawn } from "bun"; import { bunEnv, tempDir } from "harness"; @@ -29,7 +35,7 @@ function rng(seed: number) { type Graph = { files: Record; entries: string[] }; -function generateGraph(seed: number): Graph { +function generateGraph(seed: number, unsplit = false): Graph { const rnd = rng(seed); const int = (n: number) => Math.floor(rnd() * n); const chance = (p: number) => rnd() < p; @@ -83,10 +89,19 @@ function generateGraph(seed: number): Graph { // Draws for shapes added after seeds were pinned come from a stream of their own, so old seeds keep their graphs. const later = rng(seed ^ 0x5bd1e995); + // Shapes that only the unsplit check draws, from a third stream. + const wrapped = rng(seed ^ 0x1b873593); const files: Record = {}; for (let i = 0; i < n; i++) { let src = `import { note, read, attempt, later } from "./log.ts";\n`; + const [polyfillDraw, starDraw, starTarget] = [wrapped(), wrapped(), wrapped()]; + // A CommonJS file that has to run before the import after it (a polyfill and the package that needs it). + if (unsplit && polyfillDraw < 0.15 && !pure.has(i)) { + src += `import "./p${i}.cjs";\nimport "./q${i}.ts";\n`; + files[`p${i}.cjs`] = `globalThis.p${i} = "set";\nmodule.exports = {};\n`; + files[`q${i}.ts`] = `import { read } from "./log.ts";\nread("q${i} (globalThis as any).p${i});\n`; + } const importsItself = later() < 0.1 && !pure.has(i); if (importsItself) src += `import * as self${i} from "./f${i}.ts";\n`; const uses: string[] = []; @@ -103,6 +118,17 @@ function generateGraph(seed: number): Graph { ? `export { get${j} as via${i}_get${j} } from "./f${j}.ts";\n` : `export * as ns${i}_${j} from "./f${j}.ts";\n`; } + // `export *` of a file that no import statement of this file names. A file that can be require()d stays one. + const star = 1 + Math.floor(starTarget * (n - 1)); + if ( + unsplit && + starDraw < 0.25 && + star !== i && + !staticImports[i].includes(star) && + (reachesAwait.has(i) || !reachesAwait.has(star)) + ) { + src += `export * from "./f${star}.ts";\n`; + } const isPure = pure.has(i); if (!isPure) src += `note("f${i}");\n`; if (awaits.has(i)) src += `await null;\n`; @@ -170,6 +196,12 @@ export async function finish() { files["entry2.ts"] = `import { finish } from "./log.ts";\nawait import("./f${other}.ts");\nawait finish();\n`; entries.push("entry2.ts"); } + // An entry that imports the graph with import statements has no wrapper. The files that an import() reaches have one. + const [entryDraw, secondDraw] = [wrapped(), wrapped()]; + if (unsplit && entryDraw < 0.4) { + const second = entryDraw < 0.2 ? `import "./f${1 + Math.floor(secondDraw * (n - 1))}.ts";\n` : ""; + files["entry.ts"] = `import { finish } from "./log.ts";\nimport "./f0.ts";\n${second}await finish();\n`; + } return { files, entries }; } @@ -178,12 +210,21 @@ type Env = Record; // Evaluation order is what is compared; the async transpiler makes it vary from run to run. export const env: Env = { ...bunEnv, BUN_FEATURE_FLAG_DISABLE_ASYNC_TRANSPILER: "1" }; +// A program whose top-level await never settles does not exit (#45262), so every child has a deadline. +const deadline = 60_000; +const killed = 128 + 9; + export async function run(cmd: string[], cwd: string, env: Env) { - await using proc = spawn({ cmd, cwd, stdout: "pipe", stderr: "pipe", env }); + await using proc = spawn({ cmd, cwd, stdout: "pipe", stderr: "pipe", env, timeout: deadline, killSignal: "SIGKILL" }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); return { stdout, stderr, exitCode }; } +const failure = (result: { stderr: string; exitCode: number }) => + result.exitCode === killed + ? `was killed (the deadline is ${deadline / 1000} s)` + : "failed: " + result.stderr.slice(0, 400); + /** The values a log says were read, and its top-level effects in order. */ function facts(log: string) { const reads = new Map(); @@ -197,14 +238,19 @@ function facts(log: string) { return { reads, effects }; } -function worse(reference: string, unfolded: string, folded: string): string[] { +function worse( + reference: string, + unfolded: string, + folded: string, + [before, after] = ["unfolded", "folded"], +): string[] { const [ref, off, on] = [facts(reference), facts(unfolded), facts(folded)]; const problems: string[] = []; // A line like "f3:class=!" is only written when something failed, so a key one log lacks counts as "fine" there. for (const what of new Set([...ref.reads.keys(), ...off.reads.keys(), ...on.reads.keys()])) { const [value, offValue, onValue] = [ref, off, on].map(log => log.reads.get(what) ?? "fine"); if (offValue === value && onValue !== value) { - problems.push(`${what}: ${value} unbundled and unfolded, ${on.reads.get(what) ?? "missing"} folded`); + problems.push(`${what}: ${value} unbundled and ${before}, ${on.reads.get(what) ?? "missing"} ${after}`); } } const position = (effects: string[]) => new Map(effects.map((effect, index) => [effect, index])); @@ -213,7 +259,7 @@ function worse(reference: string, unfolded: string, folded: string): string[] { for (let b = a + 1; b < ref.effects.length; b++) { const [x, y] = [ref.effects[a], ref.effects[b]]; const agrees = (at: Map) => at.has(x) && at.has(y) && at.get(x)! < at.get(y)!; - if (agrees(offAt) && !agrees(onAt)) problems.push(`order of ${x} and ${y} kept unfolded, lost folded`); + if (agrees(offAt) && !agrees(onAt)) problems.push(`order of ${x} and ${y} kept ${before}, lost ${after}`); } } return problems; @@ -268,15 +314,87 @@ export async function checkGraph(bun: string, seed: number): Promise `${entry}: ${p}`)); }), ); const status = problems.length ? "worse" : skipped.length ? "skipped" : "ok"; return { seed, status, folded: true, problems: problems.length ? problems : skipped }; } + +/** + * "different": the bundle lost a value or an order that the unbundled program has, or only the bundle failed. + * "worse": the same, and the bundle that the `baseline` binary builds has that value or order. + * `unjudged`: the entries with no verdict. The unbundled run failed there, or the baseline's bundle did. + */ +export type UnsplitResult = { + seed: number; + status: "ok" | "skipped" | "different" | "worse"; + problems: string[]; + unjudged: string[]; +}; + +function differences(reference: string, bundled: string): string[] { + const [ref, out] = [facts(reference), facts(bundled)]; + const problems: string[] = []; + for (const what of new Set([...ref.reads.keys(), ...out.reads.keys()])) { + const [value, outValue] = [ref, out].map(log => log.reads.get(what) ?? "fine"); + if (outValue !== value) problems.push(`${what}: ${value} unbundled, ${out.reads.get(what) ?? "missing"} bundled`); + } + const at = new Map(out.effects.map((effect, index) => [effect, index])); + for (let a = 0; a < ref.effects.length; a++) { + for (let b = a + 1; b < ref.effects.length; b++) { + const [x, y] = [ref.effects[a], ref.effects[b]]; + if (!(at.has(x) && at.has(y) && at.get(x)! < at.get(y)!)) problems.push(`order of ${x} and ${y} lost`); + } + } + return problems; +} + +export async function checkUnsplit(bun: string, seed: number, baseline?: string): Promise { + const graph = generateGraph(seed, true); + using dir = tempDir("unsplit-fuzz", { + ...graph.files, + "build.ts": ` + const result = await Bun.build({ + entrypoints: ${JSON.stringify(graph.entries)}.map(entry => import.meta.dir + "/" + entry), + outdir: import.meta.dir + "/" + process.argv[2], + target: "bun", + format: "esm", + }); + if (!result.success) throw new Error(result.logs.join("\\n")); + `, + }); + const cwd = String(dir); + const builds = await Promise.all([ + run([bun, "build.ts", "out"], cwd, env), + ...(baseline ? [run([baseline, "build.ts", "base"], cwd, env)] : []), + ]); + const unbuilt = builds.find(build => build.exitCode !== 0); + if (unbuilt) return { seed, status: "skipped", problems: [], unjudged: [`the build ${failure(unbuilt)}`] }; + + const problems: string[] = []; + const unjudged: string[] = []; + await Promise.all( + graph.entries.map(async entry => { + const js = entry.replace(/\.ts$/, ".js"); + const [reference, bundled, before] = await Promise.all([ + run([bun, entry], cwd, env), + run([bun, join("out", js)], cwd, env), + baseline ? run([bun, join("base", js)], cwd, env) : undefined, + ]); + if (reference.exitCode !== 0) unjudged.push(`${entry}: the unbundled run ${failure(reference)}`); + else if (before && before.exitCode !== 0) unjudged.push(`${entry}: the baseline's bundle ${failure(before)}`); + else if (bundled.exitCode !== 0) problems.push(`${entry}: only the bundle ${failure(bundled)}`); + else if (before) { + const lost = worse(reference.stdout, before.stdout, bundled.stdout, ["with the baseline", "with this build"]); + problems.push(...lost.map(problem => `${entry}: ${problem}`)); + } else problems.push(...differences(reference.stdout, bundled.stdout).map(problem => `${entry}: ${problem}`)); + }), + ); + const judged = unjudged.length < graph.entries.length; + const status = problems.length ? (baseline ? "worse" : "different") : judged ? "ok" : "skipped"; + return { seed, status, problems, unjudged }; +}