Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bada17a
bundler: evaluate a wrapped module's dependencies in source order and…
robobun Sep 6, 2026
44fb00d
test: update the libraries.js bytecode snapshot for the dependency or…
robobun Sep 6, 2026
5f7d42a
bundler: shorten the comments on the wrapper prefix and async propaga…
robobun Sep 6, 2026
be90257
bundler: mark __promiseAll live for the __reExport of the first async…
robobun Sep 6, 2026
6904566
bundler: join async dependencies with Promise.all and call import() t…
robobun Sep 6, 2026
46f615f
test: regenerate the async wrapper snapshots for Promise.all and the …
robobun Sep 6, 2026
9f8c400
ci: retrigger
robobun Sep 6, 2026
184b4e0
bundler: discard the value of a joined require or __reExport so Promi…
robobun Sep 6, 2026
d47c137
Merge branch 'main' into robobun/46e46b5b/bundler-wrapper-init-order
robobun Oct 7, 2026
50c815f
test: a CommonJS import before a wrapped ES module import runs first …
robobun Oct 7, 2026
de69ca3
test: update the libraries.js bytecode snapshot for the dependency or…
robobun Oct 7, 2026
479d178
test: expect the import() form of an async module in UsingWithHoistab…
robobun Oct 7, 2026
2e3caf9
test: pin the rejection that ExportStarOfRejectedAsyncDependencyRejec…
robobun Oct 7, 2026
37b107f
bundler: join statements that can throw through a generator so a star…
robobun Oct 7, 2026
4100a21
bundler: optional spaces around the import() retry, and a test for a …
robobun Oct 7, 2026
5402f83
bundler: convert a joined declaration only when every binding is an i…
robobun Oct 7, 2026
990b9e9
bundler: shorten the doc comment of the wrapper prefix
robobun Oct 7, 2026
31cd4dd
bundler: keep the await of one async dependency bare, and skip it for…
robobun Oct 12, 2026
6700468
test: regenerate the async wrapper snapshots for the conditional await
robobun Oct 12, 2026
d104be9
test: build the async import cycle with both source index orders of i…
robobun Oct 12, 2026
ccbc1ab
bundler: shorten the comments of the wrapper prefix
robobun Oct 12, 2026
184d3e6
bundler: one-line comments at the statements the wrapper prefix emits…
robobun Oct 12, 2026
1aefaeb
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 12, 2026
fc0581c
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 23, 2026
09d9c21
test: pin the libraries.js bundle and bytecode hashes of the tree mer…
robobun Oct 23, 2026
dba3c27
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 26, 2026
f117e32
test: pin the libraries.js bytecode hash of the tree merged with main
robobun Oct 26, 2026
49b0642
bundler: start an import()ed async module after the caller's synchron…
robobun Oct 26, 2026
587e1d7
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 27, 2026
b41fec6
bundler: await an async dependency with a bare await, and drop the as…
robobun Oct 27, 2026
a3a5113
test: pin the libraries.js bundle and bytecode hashes of the tree mer…
robobun Oct 27, 2026
2c90d79
bundler: print shorter forms for the rejection handler and the import…
robobun Oct 27, 2026
3c299f9
test: import() cases for a caller in an import cycle and for an impor…
robobun Oct 27, 2026
8771df1
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 28, 2026
41ba934
bundler: keep main's prefix layout after the first async dependency
robobun Oct 28, 2026
2e5aedc
test: re-cut the order cases, pin the known losses, and add an unspli…
robobun Oct 28, 2026
2fb1b77
test: a todo case for the importer of an async import cycle member
robobun Oct 28, 2026
75c5638
test: pin an import() in an export * dependency that runs before the …
robobun Oct 28, 2026
38ddf82
bundler: leave an export star of an async module as on main
robobun Oct 28, 2026
c347be9
bundler: print main's prefix layout from an async export star on
robobun Oct 28, 2026
87491b1
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
612deb6
bundler: keep source order only in a wrapper prefix with no async dep…
robobun Oct 29, 2026
9eda6aa
bundler: shorten the doc comment of leave_source_order
robobun Oct 29, 2026
6d64d37
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
076cd9f
test: a plain bundle in code-from-disk now runs its CommonJS import f…
robobun Oct 29, 2026
e3a598a
bundler: test the wrapper prefix layout for every short list, and the…
robobun Oct 29, 2026
392f790
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
ba6d620
test: say when bun runs the sources of the case with a CommonJS file …
robobun Oct 29, 2026
c9eff65
bundler: one line for the comment of the expected layout in the wrapp…
robobun Oct 29, 2026
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
40 changes: 27 additions & 13 deletions scripts/splitting-fuzz.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
// bun scripts/splitting-fuzz.ts <bun> <graphs> [first seed]
// bun scripts/splitting-fuzz.ts <bun> <graphs> [first seed] [--unsplit [--baseline=<bun>]]
// Runs test/bundler/splitting-fuzz.ts over many seeds against <bun> and prints the seeds where chunk folding made a
// bundle worse than the unfolded one (rerun a seed with `<graphs>` = 1 to look at it).
// bundle worse than the unfolded one (rerun a seed with `<graphs>` = 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 <bun> <graphs> [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 <bun> <graphs> [first seed] [--unsplit [--baseline=<bun>]]");
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 <bun> read foldChunksForTesting (--expose-internals gate)?");
process.exit(1);
}
231 changes: 222 additions & 9 deletions src/bundler/LinkerContext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5109,11 +5109,15 @@ pub struct StmtList {
pub(crate) all_stmts: Vec<Stmt>,
}

/// 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<Stmt>,
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<usize>,
}

impl InsideWrapperPrefix {
Expand All @@ -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(),
}
}

Expand All @@ -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();
}
}

Expand All @@ -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(
Expand Down Expand Up @@ -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<i32>),
}

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<Printed> {
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<Printed> {
let positions = |kinds: &[Kind]| -> Vec<i32> {
(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<Printed> = 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<Vec<Kind>> {
let mut all: Vec<Vec<Kind>> = 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));
}
}
});
}
}
3 changes: 3 additions & 0 deletions src/bundler/linker_context/convertStmtsForChunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down
5 changes: 3 additions & 2 deletions test/aot/code-from-disk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ const cases: Record<string, Case> = {
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":
Expand All @@ -558,7 +558,8 @@ const cases: Record<string, Case> = {
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",
},
Expand Down
6 changes: 3 additions & 3 deletions test/bundler/bundler_bytecode_portable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading
Loading