Skip to content

AOT: more of bun test --aot, module loading fixes, faster ES5 classes - #47571

Closed
Jarred-Sumner wants to merge 5 commits into
mainfrom
claude/compile-aot-follow-up
Closed

Jarred-Sumner wants to merge 5 commits into
mainfrom
claude/compile-aot-follow-up

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Follow-up to #47316 for bun build --compile --aot and bun test --aot.

Goes with oven-sh/WebKit#967, which is merged. WEBKIT_VERSION is its merge commit, 5869a3b791e68fd8e0e8059ff21e278760bcf4c6.

main is merged in, up to #44833 and #47070.

bun test --aot and #44833

#44833 changed what plain bun test does with module mocks. bun test --aot doesn't match it yet:

  • a name that a mock leaves out is undefined in plain bun test, and still a link error under --aot
  • a factory that failed throws again at each load
  • a name that only a mock has, by name and through export *
  • vi.mock and jest.mock are not hoisted above the imports

Each test of these states what plain bun test does on main and keeps the comparison with --aot behind a TODO(bun), so it fails the day the two agree. The fixes are mostly in the engine and come in the next PR. main's own CI has been failing on 39 tests under test/aot since #44833. This PR fixes 17 of them.

Two things work under bun test --aot on main and don't with this PR. Both are fixed in the next PR:

  • vi.dynamicImportSettled() doesn't wait for an import() of compiled code.
  • bun test --aot ./checks/latest.ts: a test file whose name has no .test or .spec (given by path, or Foo.Test.ts found by the scan) isn't counted as one of the tests' files. A helper beside it that writes a file at run time and calls require() on it fails with "Cannot load ...: it wasn't compiled with the program". Beside usual.test.ts the same helper works.

bun test --aot

  • Tests can replace Math, JSON, Reflect or process on the global object: global.Math = mockMath, globalThis.process = undefined.
  • Tests can pose as another runtime: redefining or assigning process.versions, process.release, process.isBun.
  • swap(Date, { now() { return 5 } }), where a helper in another file copies the members in a for...in loop, no longer fails with "Cannot assign to Date.now".
  • In layered runs, built-ins reached through another file stay writable: export { getPrototypeOf } from "./tools.js", export const above = x => Object.getPrototypeOf(x), a static field, a Map.
  • Names that mock.module() adds or leaves out are seen through export * (as plain bun test did before bun test: Vitest compatibility #44833, see above).
  • A mock.module() factory that throws rejects the import the way bun test does, also for a module that is not on disk.
  • spyOn() inside if (feature("EXTRA")) works with --feature=EXTRA in layered runs.
  • new Module(path)._compile(text, path), as require-from-string and pirates do it, can require("node:zlib") and other built-in modules.
  • A test that rewrites a file, deletes it from require.cache and requires it again can use library code nothing else in the run names.
  • An unhandled rejection in a file loaded by await import() inside a test is reported as an error next to the passing test, as in bun test. It used to fail the test.
  • Under --isolate, an object kept from an earlier test file is no longer treated as an instance of the new file's classes (#v in x, method calls, catch (e) { e instanceof Fault }).
  • p.gained = "there" on an object made in a lower layer is visible to the layers in between.
  • jest.isMockFunction(f) without an import says jest.isMockFunction is not a function, not %test:jest.isMockFunction.
  • A package that only checks typeof jest !== "undefined" (react-dom's development build) no longer counts as mocking.
  • Product code finds only what an executable would find. With exports.load = name => require(dir + name) in src/ or in a package, a test that has it load a file the build didn't see now fails as the executable would. Files of the tests load what they like, native addons load wherever the call stands, and --aot-allow-code-from-disk turns it off.
  • delete require.cache[require.resolve("../src/state.js")] and then a written import("./state.js") in product code runs the module again.
  • --shard=k/n deals the files by what they reach, so that a shard builds less. Every shard computes the same deal from the paths, the sizes, what the files reach and the timings file, whatever its own --parallel says, and prints a hash of the deal. Plain bun test --shard is unchanged.
  • The parts of a run are built slowest first.
  • silence(console, levels) and spyAll(console, wanted), where a helper in another file stores or spies under a computed name, no longer fail with "Cannot assign to console.debug".
  • A spyOn() inside a template string with ${..} for a name was missed: "Cannot spy on 'total'".
  • --files-from=/dev/stdin failed every file in a layered run.
  • One file that let go of its module or require made the run keep every export of every file. In express that was 838 functions and 78 files.
  • --aot-memory-limit says what it bounds: the build, not the tests that run.

Module loading

  • require.cache[key].exports of a file required as a statement is its exports, not undefined. The entry of an ES module shows all of its exports.
  • require.cache[key].children and .parent are filled for files required at the top level of a test file.
  • Imports with nothing to run (export const value = 7) are listed in Object.keys(require.cache).
  • require("./config.json") in a CommonJS file returns the data while an ES module that imports the same file is still loading. It returned the ES namespace.
  • const name = "bun"; (await import(name)).default === Bun answers what Bun answers.
  • require(process.cwd() + "/lib/a.js"), path.resolve(..) and path.join(process.cwd(), ..) find a file that was passed as another entry point.
  • Followed at build time: require(__dirname + path.sep + "a.cjs"), require(path.normalize(__dirname + "/x/../a.cjs")), path.posix.join(__dirname, ..), import(import.meta.dir + sep + "a.mjs").
  • new Worker(new URL(".", import.meta.url).pathname + "job.mjs") is followed.
  • (typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require)("./lib/util") is followed.
  • createRequire(__filename).resolve("hides/hidden.js") throws MODULE_NOT_FOUND when the package's exports hide the file.
  • Followed at build time: require(__filename.replace("main.cjs", "first.cjs")), replaceAll, __filename.slice(0, -8) + "third.cjs", import(import.meta.url.replace(/module\.mjs$/, "ninth.mjs")).
  • new Worker(file) in a file loaded from disk by an executable built with --aot-allow-code-from-disk died on globalThis.console = ...
  • An import with a query follows main's rule: raw among the & parameters is the file's text, url is the file loader.
  • (await import("../../helpers/data.json")).default failed with "Maximum call stack size exceeded" when the project was at certain absolute paths.

Crashes

  • let b = null, c = null; if (n) c = { k: 1.5 }; while (b !== b) b = [b]; if (y != b) { [b, y] = [y, b]; return c === y ? c : 1; } ended with SIGSEGV, and ten lines of the same kind aborted the compiler. The fix is in the WebKit PR, the programs are here.
  • try { null.x } catch (e) { e.constructor.list = { length: 3 } }, where TypeError.list was an array before, ended with a signal.
  • class Holder { held = stream[Symbol.for("nodejs.stream.readable")]; } with a ReadableStream aborted. In a function the read gave undefined.
  • mock.module("./below.ts", () => { throw .. }) followed by imports of two files above it aborted in builds with assertions.
  • When the source text an error message quotes failed its own check, the build stopped with an internal compiler error. Now that one file loses the quote.
  • function kind(x) { switch (x) { case 3: .. } } kind([2]) aborted with "FrameCellEscaped" in builds with validation on.
  • 10,000 functions that call one another in a ring, each handing on what it got: bun test --aot overflowed the compiler's stack. 100,000 build now.
  • var holder = { other: 1 }; const kept = holder.link; holder.link = kept; holder.link(Child, Base): the build never ended.
  • --aot-check=verify-each-stage reported GuardNotAvailable for correct code: an async function with more than 16 awaits, and a typed function that restarts in its generic version.

Wrong answers

  • function pick(i) { return i % 2 ? null : { a: 1 } } function read(o) { return o.a }: read(null) returned 1 instead of throwing.
  • ES5 classes that share a method: after delete A.prototype.twice, new A(8).twice() returned 16 instead of throwing.
  • try { null.x } catch (e) { e.constructor.extra = 41 }: TypeError.extra was undefined.
  • class Tags extends Set { constructor(values) { super(values) } } calls an overridden add. Same for Map, WeakMap, WeakSet.
  • Array.from(new Map(..)) after the class's iterator was deleted treats the object as array-like.
  • Object.assign({}, source) kept the wrong key order for a source with a hidden index property and a symbol key.
  • a[300_000_000] = 1; a.toSorted() threw "Out of memory".
  • A parameter passed on the stack and then set to null kept its object alive across Bun.gc(true).
  • Whether an --external package's globalThis.Date = .. was refused, and which fields { delay: 50, ...options } got, depended on the order the analysis ran in.
  • A function expression passed to .catch() or .then() was typed from the checker's first, incomplete look at it.
  • Error messages quote the source as bun run does: o.nothing({ a }), tagged templates, undefined.x, minified text, o["字"](), new o["字"].
  • A file that does not parse reports its syntax errors only, not the type errors that follow from them.
  • String(f) in a .ts file: () => <Foo>{} read () => {} and return<number>a read returna.

Faster

  • Classes that Babel compiled to ES5 are compiled as classes: _createClass, _defineProperties, _classCallCheck, _inherits and _typeof, written into the file or imported from @babel/runtime, Babel 7.18 through 7.24.
  • var p = Object.create(A.prototype); p.constructor = B; B.prototype = p: 254 to 25 instructions per iteration.
  • util.inherits = function (ctor, superCtor) {..} in another file: 712 to 19.
  • const f = table[key]; for (..) t += f(..) checks once, before the loop, which function it got.
  • out[key] = value that adds a property is cached per site. holder[key] = value checks the field's type once per kind of value.
  • for (const [key, value] of [["a", 1]]) and const [text, count] = ["abc", n]: each position has its own type.
  • list.map(f) no longer re-reads the new list's length. list.push(x) in a counted loop allocates once.
  • [...set], [x, ...set], f(...set); list.concat(other) with doubles and other values.
  • found ? 1 : 0 is the comparison's result. parsed[i].radius || 0 tests numbers inline.
  • left[i] === right[j] and to[k] = from[k] on typed arrays.
  • toUpperCase() and toLowerCase() on Latin-1 strings, 16 characters at a time.
  • A private or public field of a class that extends Array is looked up once before a loop.
  • Object.defineProperty(object, "name", { value }) on a literal or an instance gets a field in the layout.
  • Closures returned as an object (return { add(by) {..}, get() {..} }) are created when first read.
  • import * as pkg from "a-commonjs-package"; pkg.member across layers is cached.
  • require.cache lookups and the first require() of a file cost the same in a large program. Modules above an evicted file are checked once.
  • The first error thrown at each place in a 1.5 MB one-line bundle took 108 M instructions. Only the statements around it are scanned now.

Smaller

  • The names of captured variables are written once for closures that capture the same ones.
  • The registers around a slow-path call are saved by one shared stub. A caller that reads no floating-point register after a call saves none.
  • catch (error) { console.log(error.message) } no longer adds an initialization check in front of every built-in. A class the realm has not created costs no lookup at startup.

Build time and memory

  • let picked = s0; if (n > 1) picked = s1; .. with 2,000 or more branches.
  • A linked package reached through a computed import() no longer causes a second build.
  • The scan of a test run's files reads each file once. File stamps are taken on threads and compared without allocating a path string.
  • A long-running checker task no longer keeps the trees of retired modules alive.
  • The layers of a test run are planned while the packages are being built.
  • all.push({..}) 20,000 times in one function: the checker walked, for every reference to a symbol, all call statements that mention it.
  • The front end keeps a tree that it will be asked for again instead of parsing the file anew.

Tests and CI

  • A test builds each stored program under several orders of the analysis rules, thread counts and checker passes, and fails if the generated code differs.
  • Nearly all instruction counts in test/aot/perf are exact, with no tolerance.
  • Two of those counts rose when main was merged: a gzip round trip makes 2 more property accesses by name per turn, and Map.groupBy takes 3 more branches. Both come from library text that changed on main: zlib.ts now takes isArrayBufferView from node:util/types, which the compiler has no entry for (ArrayBuffer.isView it knows), and declare var $Map: MapConstructor in builtins.d.ts gives groupBy's map a type, which adds a type test per element. Both fixes are in the compiler and come in the next PR. Both pins stay where they were. Each row carries an "owed" mark with the count it is held to: the test fails if the count rises further, and fails again once the pin is met, so the mark cannot be forgotten.
  • o.nothing(String.raw`é${x}`): since bun test: Vitest compatibility #44833 the TypeError of plain Bun quotes its own lowering of the template (__rawTemplate_..), and compiled code quotes the source. A todo in test/aot/parallel/errors/raw-template-quote.js.
  • About 1,800 new files under test/aot, most of them stored programs compared with plain Bun.
  • test/aot/a-text-compared-in-a-cold-loop-asks-for-no-atom.test.ts is removed with the behavior it tested. In cold functions, kind === "circle" used to skip the check for the same atom to save code size. That made the comparison slower, so AOT: crashes and wrong answers found since #942, and faster builds of large functions WebKit#967 puts the check back.

Not AOT-specific

  • bun test --isolate: a process.nextTick() queued while the second test file's imports load runs after that file's body again, as before Compile TypeScript to machine code ahead of time: bun build --compile --aot #45631 and as in 1.4.4.
  • bun test --parallel: a worker waiting for its next file sleeps instead of polling.
  • bun test: the memory reservation while tests run is removed (581 lines).
  • bun test --isolate: a FileSink closes its descriptor before close returns instead of later on the work pool.
  • require.cache is the function it was before the first AOT PR. The traps for compiled programs are a separate function.
  • bun check: a hash set past 16 type parameters, the narrowing index is kept instead of dropped whole, the regular form of a literal is computed once, a discarded task explains no error.
  • bun check and bun lint --type-aware, five more changes, none of which moves an answer: the same errors on seven folders of TypeScript, eight
    folders under bun lint --type-aware and four written probes, on CI's builds of main and of this branch. Whether what is being computed may be
    cached is answered from a memo keyed by the frames' serial numbers, not by a scan of the stack of queries (check/context.rs).
    are_call_statements_idle() remembers how many statements in front are idle (check/flow.rs). exporting_modules() works in chunks of
    1,024, and a task that is discarded leaves off where the next begins (check/errors_declaration_emit.rs, task.rs). hir::Fixed is a
    pointer, a u32 and a tag in 16 bytes where it was an enum of a box, a &mut and a &, with unsafe in Deref, DerefMut, Drop, Send
    and Sync (hir.rs). Under bun lint --type-aware, a function expression with a contextual signature and no written return type gets its first
    look in the task of its own file even where a return type is stored already (check/expr.rs).
  • path.posix.dirname, basename and extname of Latin-1 strings use SIMD kernels.
  • bun check and builds on POSIX: a thread that runs out of stack says what it was compiling (a 512 KB signal stack per thread).
  • Release link: on Windows the bitcode is no longer compiled twice for a weak alias that only native objects name.
  • Compiled into plain Bun and not reached there: two stubs that answer false (one asked in ImportMetaObject.cpp before a .node file is refused), an early return inside if (hasCompiledModules()) where the module loader fetches, the lock on process.release (it does nothing without compiled code), and the wording of this._write is not a function for the bare streams that exist only where a built-in was trimmed away.

How did you verify your code works?

  • Every fix has a test in test/aot that fails without it.
  • Differential fuzzing against plain Bun on Linux x64 and arm64.
  • The stored programs also run under ASAN and the GC verifier.

Follow-up to #47316 for `bun build --compile --aot` and `bun test --aot`.
Goes with oven-sh/WebKit#967; WEBKIT_VERSION is pinned to its preview build until it merges.

bun test --aot:
- Tests can replace `Math`, `JSON`, `Reflect` or `process` on the global object: `global.Math = mockMath`, `globalThis.process = undefined`.
- Tests can pose as another runtime: redefining or assigning `process.versions`, `process.release`, `process.isBun`.
- `swap(Date, { now() { return 5 } })`, where a helper in another file copies the members in a `for...in` loop, no longer fails with "Cannot assign to Date.now".
- In layered runs, built-ins reached through another file stay writable: `export { getPrototypeOf } from "./tools.js"`, `export const above = x => Object.getPrototypeOf(x)`, a static field, a `Map`.
- Names that `mock.module()` adds or leaves out are seen through `export *`.
- A `mock.module()` factory that throws rejects the import the way `bun test` does, also for a module that is not on disk.

Module loading:
- `require.cache[key].exports` of a file required as a statement is its exports, not `undefined`. The entry of an ES module shows all of its exports.
- `require.cache[key].children` and `.parent` are filled for files required at the top level of a test file.
- Imports with nothing to run (`export const value = 7`) are listed in `Object.keys(require.cache)`.
- `require("./config.json")` in a CommonJS file returns the data while an ES module that imports the same file is still loading. It returned the ES namespace.
- `const name = "bun"; (await import(name)).default === Bun` answers what Bun answers.

Crashes:
- `mock.module("./below.ts", () => { throw .. })` followed by imports of two files above it aborted in builds with assertions.
- When the source text an error message quotes failed its own check, the build stopped with an internal compiler error. Now that one file loses the quote.
- `function kind(x) { switch (x) { case 3: .. } } kind([2])` aborted with "FrameCellEscaped" in builds with validation on.

Wrong answers:
- `function pick(i) { return i % 2 ? null : { a: 1 } } function read(o) { return o.a }`: `read(null)` returned 1 instead of throwing.
- ES5 classes that share a method: after `delete A.prototype.twice`, `new A(8).twice()` returned 16 instead of throwing.
- `try { null.x } catch (e) { e.constructor.extra = 41 }`: `TypeError.extra` was `undefined`.
- `Object.getPrototypeOf(crypto.subtle).extra = 41`: `SubtleCrypto.prototype.extra` was `undefined`. Same for `AbortSignal.prototype` and `Bun.S3Client.prototype`.
- `class Tags extends Set { constructor(values) { super(values) } }` calls an overridden `add`. Same for `Map`, `WeakMap`, `WeakSet`.
- `Array.from(new Map(..))` after the class's iterator was deleted treats the object as array-like.
- `Object.assign({}, source)` kept the wrong key order for a source with a hidden index property and a symbol key.

Faster:
- Classes that Babel compiled to ES5 are compiled as classes: `_createClass`, `_defineProperties`, `_classCallCheck`, `_inherits` and `_typeof`, written into the file or imported from `@babel/runtime`, Babel 7.18 through 7.24.
- `var p = Object.create(A.prototype); p.constructor = B; B.prototype = p`: 254 to 25 instructions per iteration.
- `util.inherits = function (ctor, superCtor) {..}` in another file: 712 to 19.
- `const f = table[key]; for (..) t += f(..)` checks once, before the loop, which function it got.
- `out[key] = value` that adds a property is cached per site. `holder[key] = value` checks the field's type once per kind of value.
- `for (const [key, value] of [["a", 1]])` and `const [text, count] = ["abc", n]`: each position has its own type.

Smaller:
- The names of captured variables are written once for closures that capture the same ones.
- The registers around a slow-path call are saved by one shared stub. A caller that reads no floating-point register after a call saves none.

Build time and memory:
- `let picked = s0; if (n > 1) picked = s1; ..` with 2,000 or more branches.
- A linked package reached through a computed `import()` no longer causes a second build.

Tests and CI:
- A test builds each stored program under several orders of the analysis rules, thread counts and checker passes, and fails if the generated code differs.
- The instruction counts in `test/aot/perf` are exact, with no tolerance.

Not AOT-specific:
- `bun test --isolate`: a `process.nextTick()` queued while the second test file's imports load runs after that file's body again, as before #47316.
- `bun test --parallel`: a worker waiting for its next file sleeps instead of polling.
- `bun test`: the memory reservation while tests run is removed (581 lines).
- `bun test --isolate`: a `FileSink` closes its descriptor before `close` returns instead of later on the work pool.
- `require.cache` is the function it was before the first AOT PR. The traps for compiled programs are a separate function.
- `bun check`: a hash set past 16 type parameters, the narrowing index is kept instead of dropped whole, the regular form of a literal is computed once, a discarded task explains no error.
- `path.posix.dirname`, `basename` and `extname` of Latin-1 strings use SIMD kernels.
@Jarred-Sumner
Jarred-Sumner requested a review from alii as a code owner October 31, 2026 20:11
@robobun

robobun commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 9:05 PM PT - Oct 31st, 2026

@Jarred-Sumner, your commit 79841cf is building: #136290

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread scripts/build/deps/webkit.ts Resolved
Comment thread src/aot/load/replaced_builtins.rs Resolved
Comment thread src/runtime/cli/test_command.rs Resolved
Jarred-Sumner added a commit to oven-sh/WebKit that referenced this pull request Oct 31, 2026
…ith a long chain of classes

Two fixes for two red tests of oven-sh/bun#47571.

The first error at a place costs the same in a longer file

Forty places inside one `switch` of 12,000 cases each throw their first
TypeError ("undefined is not an object (evaluating 'a.b')"). To quote the source
in such a message the run time decodes the entries that say what was printed in
place of the original text. It decoded every entry of the kept piece of source
at each first error, and a long statement or function is one piece. The entries
of a piece are now decoded once and kept, however many places in it throw. A
compiled test run decoded every entry of the module each time: now once.

The long file decodes 2,162 entries where it decoded about 357,000. Forty places
in one statement: 2.3 > 1.5 ms. In a test run: 4.05 > 0.59 ms. With
AddressSanitizer it was 130 to 141 ms against 2 to 3 ms in the short file, which
is what failed the-first-error-at-a-place-costs-the-same-in-a-longer-file on the
x64 ASAN lane. No message changes. The counts file has a new line,
`decoded:TextPrintedInPlace`.

A build keeps the partition of a call's receivers as words

`function ask_i(x) { return x.weigh() + ask_(i-1)(x) }`, 800 in a chain, each
given an instance of one class more. No two calls of `weigh` see the same
classes, so the cache kept one partition per call and key, each with an arm per
class: 320,000 arms of 112 bytes. The cache now keeps a partition packed: 24
bytes for an arm with one method and one layout. And "may this call enter a
table look-up" built and kept a whole partition to answer one bool: it now asks
class by class, stops at the first no and keeps nothing.

800 classes: 189 > 166 MB, 20.14 > 20.01 G instructions. The generated code is
identical on 1.16 million functions. build-scaling-4 failed on macOS arm64 with
"twice as many classes cost 2.58 times the peakBytes; allowed: 2.5". On Linux
that ratio goes from 2.29 to 1.85. Not measured on macOS.
Pins oven-sh/WebKit#967 at 43ccc98c356a5d1ff47392dde1ced58b51c5d20c.

The build

- `var holder = { other: 1 }; const kept = holder.link; holder.link = kept;
  holder.link(Child, Base)`: `bun build --compile --aot` never ended. So with
  `const original = A.make; .. A.make = original`. The walk from a name to what
  it stands for forgot the names it had visited when it went on to a stored
  member. It now remembers each store it has followed. The previous commit
  brought this.
- The exported variables of a build's module records are looked up on the
  calling thread again. The previous commit put that on threads, which gained
  nothing on a large application.
- `bun_aot` is built at opt-level "s": Bun is 475 KB smaller.

`bun test --aot`

- Product code finds only what an executable would find. With
  `exports.load = name => require(dir + name)` in `src/` or in a package, a test
  that has it load a file the build didn't see now fails as the executable
  would. Files of the tests load what they like, native addons load wherever
  the call stands, and `--aot-allow-code-from-disk` turns it off.
- `delete require.cache[require.resolve("../src/state.js")]` and then a written
  `import("./state.js")` in product code runs the module again.
- `--shard=k/n` deals the files by what they reach, so that a shard builds
  less. Every shard computes the same deal on any machine. Plain
  `bun test --shard` is unchanged.
- The parts of a run are built slowest first.
- `silence(console, levels)` and `spyAll(console, wanted)`, where a helper in
  another file stores or spies under a computed name, no longer fail with
  "Cannot assign to console.debug".
- `--files-from=/dev/stdin` failed every file in a layered run.
- One file that let go of its `module` or `require` made the run keep every
  export of every file. In express that was 838 functions and 78 files.
- `--aot-memory-limit` says what it bounds: the build, not the tests that run.

Module loading

- Followed at build time: `require(__filename.replace("main.cjs", "first.cjs"))`,
  `replaceAll`, `__filename.slice(0, -8) + "third.cjs"`,
  `import(import.meta.url.replace(/module\.mjs$/, "ninth.mjs"))`.
- `new Worker(file)` in a file loaded from disk by an executable built with
  `--aot-allow-code-from-disk` died on `globalThis.console = ..`.

Wrong answers

- `String(f)` in a `.ts` file: `() => <Foo>{}` read `() => {}` and
  `return<number>a` read `returna`.

From the review of the previous commit

- A `spyOn()` inside a template string with `${..}` for a name was missed:
  "Cannot spy on 'total'".
- The memory of a worker's children was counted in pages of 4 KB.
- A failed write into the program below was ignored. Three of our own tests
  opened that file read-only, so their writes had been failing silently.
- A preload that a test file imports back was scanned twice.
- A test that can't make its folder says skipped.

Tests

- The stage-log test used `O_DIRECT` by number, which is `O_DIRECTORY` on
  arm64. It listens on a datagram socket now.
- A helper asked `.todo` of `test.skipIf(true)`, which failed eight files on
  the ASAN lane.
- A stack-frames program cut its path at the first forward slash, which failed
  on Windows.
- Five `gc/` programs are marked todo on Windows x64.
- The thread pool's unit tests ran dozens of builds on a pool sized for one.
- The test of how a run's parts are cut asked for the order in which three
  builds end, which is a race. It asks for the cut.
- 113 new files, most of them stored programs compared with plain Bun.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still open from earlier reviews (1):

  • Unresolved: 1 blocking on lines changed since (possibly already fixed).

If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.

Comment thread src/runtime/cli/test_command.rs Resolved
Jarred-Sumner added a commit to oven-sh/WebKit that referenced this pull request Oct 31, 2026
… large functions (#967)

* AOT: crashes and wrong answers found since #942, and faster builds of large functions

Its tests that need Bun are in the Bun PR that goes with this one.

Crashes of compiled programs:

    let b = null, c = null;
    if (n) c = { k: 1.5 };
    while (b !== b) b = [b];
    if (y != b) { [b, y] = [y, b]; return c === y ? c : 1; }

ended by SIGSEGV, and ten lines of the same kind aborted the compiler. SinkAllocations analyses a function,
counts and then rewrites it, and all three must decide alike. A helper read constants out of the function as
it stood, so the rewriting saw constants that the analysis had not seen. #942 mended one of its five callers.
Now the question is answered from a table that is filled before anything is rewritten, and that mend is gone.

    try { null.x } catch (e) { e.constructor.list = { length: 3 } }    // TypeError.list was an array before

ended by a signal, gave `undefined`, or called the function that had been replaced. Flow took the stores that
it could follow for all the stores there are. A store through an object that nobody knows is now looked for on
every built-in class, and on the prototypes above crypto.subtle, Bun.s3 and a request's signal.

    class Holder { held = stream[Symbol.for("nodejs.stream.readable")]; }    // a ReadableStream

aborted, and in a function the read gave `undefined`. A symbol out of Symbol.for() was treated like one out of
Symbol(), which the host cannot reach.

Wrong answers:

  - After `delete A.prototype.twice`, `new A(8).twice()` still answered. With a setter on B.prototype alone,
    `const o = k === 0 ? new A(k) : new B(k); o.y = 5` in a loop ran B's setter for the first A. B3 moved the
    load of a class's guard bits across the allocation that makes the class.
  - `require("./config.json")` in a .cjs file that runs while the graph loads answered the ES namespace.
  - `Object.assign({}, source)` stored names before symbols for a source with room for elements.
  - Closures of one function that call each other in tail position: a stack frame had the name of the
    function object that was called, not of the one that ran.
  - With one optional pass off, `o.a` was folded where `o` may be null, and another pass aborted the build.
  - Two runs of one executable made different caching decisions: a table was hashed by an address.

Memory: the slow path of a stub has a frame as large as what it saves. Objects were kept alive longer than in
plain Bun. A store of "" or of a string of one Latin-1 character needs no write barrier.

Build time. A generated function of 1,000 statements builds in 7,891 M instructions, from 12,473 M, with the
same output: liveness gave up only after a fixed number of visits and then started over, a dataflow kept a
counter for each condition in each block, and the guards' analysis asked each write about each proof, where
4,304 writes had 28 different answers.

Speed: `a[i] === b[j]`, case conversion sixteen characters at a step, a read by name of a slot that holds a
raw double, and 226 stepped benchmark rows through the stubs' new form.

Checks: the proof checks compare a function's identity and are no longer skipped in forwarding code.
`--aot-check=ClaimsOfFlow` tests every claim of flow where it is made. Under the verifying flags
SinkAllocations checks that its three steps decided alike.

* AOT: built-ins reached by computed names, RegExp fixes, a vectorizer crash

Crashes

- `Bun.gzipSync.list = [1, 2]; Bun[JSON.parse('"gzipSync"')].list = { length: 3 }`
  and then `Bun.gzipSync.list.length` segfaulted. So did the same through
  `Event.prototype[name]`, names out of `Object.keys(Bun)`, and
  `Bun.Glob.prototype` reached by three computed names.
  `Object.setPrototypeOf(Object.getPrototypeOf(Bun.s3), { .. })` was not seen.
  A read under a computed key, or of `prototype`, off an unknown object in the
  program's own code now reaches the host prototype objects. This replaces the
  wider rule of the previous commit, which also counted such reads in library
  code and cost `await request.json()` its typed parse.
- The compiler died with "Floating point error" on
  `while (a == 65536 && i0 < 9) { i0++; c = c > 0 ? d : c; }`.
  B3VectorizeLoops divided by the number of memory accesses of a loop that has
  none.
- A loop counter beyond int32, read from a property and used to index a typed
  array, trapped.
- `RegExp::matchWithImageCode` dereferenced a null image record when jsc runs
  its tests with regular expressions compiled ahead of time (six JSTests).

Wrong answers

- `/^(?=(.*))\1$/` said "no match" for a subject with characters outside
  Latin-1. The pattern was taken to consume 8-bit characters only: what a
  lookahead captures counts once a back reference can consume it.
- `new RegExp("\\B" + text, "u")`: `\B` matched inside a surrogate pair.
- `function make(k) { return k === 0 ? new A(0) : new B(1) }` with a getter on
  `B.prototype` alone: `make(0).size` ran `B`'s getter when the first `A` was
  made behind a test of both classes.
- `x.f += n` where `f` starts on the prototype: the arm that reads `f` by name
  may run a getter, and came back into code that relied on a guard from before
  the read.
- `Interface.prototype[Symbol.asyncIterator]` read in a function was a
  different function object than `Reflect.get()` returned (node:readline).
- `await import("sass")` in an executable threw "Cannot assign to
  Function.prototype.call$0". Programs out of dart2js add such names.
- `function twice(f, n) { return f(f(n)) } twice({}, 1)` quoted `f` instead of
  `f(n)` in the error message.
- `import * as ns`; `Reflect.setPrototypeOf(ns, ..)`, `Object.freeze(ns)`,
  `"added" in ns`: a guard for compiled objects read a module namespace.

Windows

- `hasChanged |= std::exchange(member, copyOfClass(member)) != member` compares
  the old value with itself where the right operand is evaluated first, as on
  both Windows targets. Copied classes and closure factories kept the types of
  the original there: correct, but less precise.

Memory

- `for (let i = 0; i < n; i++) a.push(f(i))` reserved the whole array up front
  even where `f` may throw and end the loop.

Faster

- `let current = first; while (current) current = current.run(this)` with
  `first` typed by an interface: objects that have the interface's members but
  none of its layouts stay in the typed loop. Effect's run loop: -20%
  instructions per turn.

Smaller

- The compiler's rarely run files are built for size: -600 KB.

Tools

- The map and `--aot-report` say "proven" or "missed optimization" where the
  final IR knows the receiver's class, instead of "unknown".
- False alarms under `--aot-validate` and `--aot-verify-each-pass`: a field
  that holds its own object, what an `await` holds, `Object.keys()` of an
  object with a computed key.

* AOT: FlowSiteLevel stays within 24 bytes on Windows

The previous commit added a bool at the end of FlowSiteLevel. With the
Microsoft ABI the two bit-fields at its start take a unit of their own, so the
struct was already 24 bytes there and grew to 28, and the static_assert failed
every Windows build. The bool now stands behind the two uint8_t members, in
what was padding on every target.

* AOT: a build aborted where a generic copy rejoins at a block entered by a test's own edge

`bun test --aot` on NestJS's suite stopped with "Stopped while compiling
printMessages .. in PlaceTypeTests". A block's parameters are its explicit
arguments followed by the results of the edges that enter it, such as the
narrowed value of a type test. Two places indexed an edge's arguments with a
parameter's index, which is out of bounds for an edge result. Both now ask
incomingValueOf(). An edge's own result counts as proven when a rejoin is
planned, and as unknown when the first values of loop variables are looked for.
The second commit of this pull request brought this.

* AOT: the first error in a long statement, and the memory of a build with a long chain of classes

Two fixes for two red tests of oven-sh/bun#47571.

The first error at a place costs the same in a longer file

Forty places inside one `switch` of 12,000 cases each throw their first
TypeError ("undefined is not an object (evaluating 'a.b')"). To quote the source
in such a message the run time decodes the entries that say what was printed in
place of the original text. It decoded every entry of the kept piece of source
at each first error, and a long statement or function is one piece. The entries
of a piece are now decoded once and kept, however many places in it throw. A
compiled test run decoded every entry of the module each time: now once.

The long file decodes 2,162 entries where it decoded about 357,000. Forty places
in one statement: 2.3 > 1.5 ms. In a test run: 4.05 > 0.59 ms. With
AddressSanitizer it was 130 to 141 ms against 2 to 3 ms in the short file, which
is what failed the-first-error-at-a-place-costs-the-same-in-a-longer-file on the
x64 ASAN lane. No message changes. The counts file has a new line,
`decoded:TextPrintedInPlace`.

A build keeps the partition of a call's receivers as words

`function ask_i(x) { return x.weigh() + ask_(i-1)(x) }`, 800 in a chain, each
given an instance of one class more. No two calls of `weigh` see the same
classes, so the cache kept one partition per call and key, each with an arm per
class: 320,000 arms of 112 bytes. The cache now keeps a partition packed: 24
bytes for an arm with one method and one layout. And "may this call enter a
table look-up" built and kept a whole partition to answer one bool: it now asks
class by class, stops at the first no and keeps nothing.

800 classes: 189 > 166 MB, 20.14 > 20.01 G instructions. The generated code is
identical on 1.16 million functions. build-scaling-4 failed on macOS arm64 with
"twice as many classes cost 2.58 times the peakBytes; allowed: 2.5". On Linux
that ratio goes from 2.29 to 1.85. Not measured on macOS.
Pins WebKit at 5869a3b791e68fd8e0e8059ff21e278760bcf4c6 (oven-sh/WebKit#967,
merged) in place of the preview build.

Five files conflicted with #44833 and #47070. In all five, main's lines stand
as written.

Fixes for what this branch brought

- `bun test --aot --shard=k/n`: each shard dealt the test files by, among other
  things, its own `--parallel`. Two runners started with different `--parallel`
  computed different deals. With `--parallel=1` on one and `--parallel=16` on
  the other, 21 of 40 test files ran on neither, and both runs reported
  success. Found in review of this PR. Every shard now computes the deal from
  what all of them share: the paths, the sizes, what the files reach, and the
  timings file. How many files run at once is read from the timings file
  (`"workers"`, written by a compiled `--parallel` run under
  `--update-timings`). `--parallel` has no say. Each shard prints one dim line
  with a hash of the whole deal, so a mismatch can be seen.
- 10,000 functions that call one another in a ring, each handing on what it
  got: `bun build --compile --aot` overflowed the stack under
  AddressSanitizer, and `bun test --aot` did so in release builds. The walk
  that finds which values a name may stand for recurses once per function of
  the ring. Where the stack runs low it now goes on in a thread with a fresh
  stack. 100,000 functions build.

Build time

- The checker walked, for every reference to a symbol, all call statements that
  mention it. `all.push({..})` 20,000 times in one function takes 2.6% fewer
  instructions to build. No answer of the checker changes. `bun check` shares
  the file.
- `bun_aot` is built at its own optimization level again, not at "s". The
  smaller Bun cost up to 0.85% more instructions a build.
- The front end keeps a tree that it will be asked for again instead of parsing
  the file anew. The output is identical.

The first two turn `build-scaling-6` green on Alpine x64.

Following #44833 (`bun test`: Vitest compatibility)

- An import with a query follows main's rule: `raw` among the `&` parameters is
  the file's text, `url` is the file loader.
- Which file is the entry point of a test run is asked of the runner, not
  compared with the main module's path.
- Tests that state what plain `bun test` does now state what it does on main: a
  name that a mock leaves out is `undefined`, not a SyntaxError; a factory that
  failed throws again at each load; `spyOn(namespace, "absent")` throws a
  TypeError.
- `bun test --aot` doesn't match plain `bun test` on module mocks yet. Each such
  case says so behind a `TODO(bun)` and stays a comparison with plain, so it
  goes red the day the two agree: a name that only a mock has, a name that a
  mock leaves out, the order of two link errors where a file may be mocked, the
  default of a mock that has none, a load above or behind a mock whose factory
  failed.
- `compile-trim-capabilities`: its reader of machine code took every `e8` and
  four bytes for a call, also in the middle of another instruction
  (`80 bb e8 96 00 00 01` is `cmpb $1, 0x96e8(%rbx)`). It now asks that an
  instruction begins there.
- The test that compares the functions of a test run with those of the
  executable leaves out the map's "outlined" rows: `bun:test` now brings its
  fake timers into every test run.
- Two tests of the engine's own suites are listed as failing: they count the
  entries of a table that has three more since #44833. Their fix is in the
  engine and comes with its next commit.

Tests

- `a-line-of-the-stage-log-goes-out-in-one-write` is a todo where the running
  glibc is 2.37 or newer. The engine's long log lines still go out in pieces
  there. The fix is in the engine and comes with its next commit.
The source lint for byte searches asks for it.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟡 test/aot/a-text-compared-in-a-cold-loop-asks-for-no-atom.test.ts:1 — nit: maintainers lose the only regression test for the cold-loop string-comparison codegen, and the diff gives no reason or replacement. test/aot/a-text-compared-in-a-cold-loop-asks-for-no-atom.test.ts is deleted outright (72 lines) while nothing under src/aot changes the ValueEqualsLiteral lowering it guarded and no other test reads the O/F rows of BUN_JSC_aotMapFilePath for countKinds. Fix: either restore the test (adjusting the 140-byte bound if the WebKit pin changed codegen size) or state in the PR why the property no longer needs a test and add a replacement that asserts cold loops compare by characters alone.

    Why this was flagged

    The commit 2cc14c8 removes test/aot/a-text-compared-in-a-cold-loop-asks-for-no-atom.test.ts; its message lists many test changes but never mentions this file or the property it checked (a kind === "circle" comparison in a cold loop emits no atom arm, asserted by band being "c" and bytes / 2 below 140 on x64). A grep of the diff of src/aot and src/sema finds no change to that lowering, and the only remaining mentions of ValueEqualsLiteral in test/ are unrelated engine .ir checks and impostor rows. On the base branch a regression that reintroduces the atom arm in cold loops (the 137 KB the file's header describes) fails this test; after the merge it passes unnoticed. The repository review rules state every test deletion needs a stated reason or a replacement.

    Verification: nit. Trigger: unconditional — the file is gone on merge. git show 2cc14c83e --name-status lists it as D test/aot/a-text-compared-in-a-cold-loop-asks-for-no-atom.test.ts. No replacement exists: a Grep of test/ for countKinds/cold-comparisons finds nothing. A regression of the cold-loop string-comparison code size would now go undetected; nothing breaks at runtime, hence nit.

Binary size

- `bun_aot` is built at opt-level "s" again. The merge commit had taken that
  out, and bun-linux-x64-musl came out 516 KB larger than main's build, 4 KB
  over what the binary-size check allows. At "s" Bun is 475 KB smaller and a
  build takes 0.15 to 0.4% more instructions. `build-scaling-6` keeps its room
  from the checker fix in the merge commit: its shape takes 13,882 M
  instructions against a limit of 14,164 M.

Tests

- `heavy-test-files-run-apart` listed the keys of the timings file that a
  compiled run writes. Since the shard fix in the merge commit, a `--parallel`
  run also writes `"workers"`. The test expects the key, and that it is 2 under
  `--parallel=2`.
- `counted-idioms-2` and `-3`: since main was merged, a gzip round trip makes 2
  more property accesses by name per turn (108 against a pin of 106), and
  `Map.groupBy` takes 3 more branches (85 against 82). Typed and untyped alike,
  on every platform; a build without the merge does not. The cause is not known
  yet. The pins stay where they are. Each of the two rows carries an `"owed"`
  mark with the count it is held to: the test fails if the count rises further,
  and it fails once the pin is met again, so the mark cannot be forgotten.
- ``o.nothing(String.raw`a\nb字`)``: since #44833 the TypeError of plain Bun
  quotes its own lowering of a tagged template that holds letters outside ASCII
  (`String.raw(__rawTemplate_..`). Compiled code quotes the source. The two
  lines move out of the test of error texts into a program of their own, marked
  as a todo.
- `what-require-answered-behind-a-mock-of-a-built-in-module-..` states what
  plain `bun test` does on main: where nothing is loaded when the mock is made,
  `require()` returns the object that the factory made, or its `default` where
  the object says `__esModule`.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

Jarred-Sumner added a commit that referenced this pull request Nov 1, 2026
…ince #47571

The engine's half is oven-sh/WebKit#972. The pull request's description has what is still wrong,
what may be red in CI, the pins that went up and every file outside src/aot, test/aot and docs.

Crashes:

- `TypeError.list = [1, 2]; JSON.parse("0", () => Reflect.set)(e.constructor, "list", { length: 3 })` with `e` a caught error, then `TypeError.list.length` in a function: the compiled program segfaulted. The same happened with a hidden `Object.assign` or `Object.defineProperty`.
- `Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))` with a `key` object whose `toString()` returns "a" and then "b": plain prints 3, compiled code segfaulted.
- Every `--aot` executable aborted before its first statement on a kernel with 64 KB pages (RHEL 8 aarch64, Oracle's UEK, the 64k kernels for NVIDIA Grace).
- `vi.doUnmock("./k.js")` while a plugin's async `onLoad` is still loading another import segfaulted under `bun test --aot`. The crash is fixed; the import still gets the first mock's values where plain gives the file's, and twelve `test.failing` cases record that.
- `bun test --aot` aborted under `validateExceptionChecks`, which CI's ASAN lane sets, wherever a mock was installed: `vi.mock("./mod", () => ({ value: fake }))`, and in a layered run `vi.doMock("./m08.js", async () => { await sleep(80); return { value: "mocked" }; }); import("./m08.js")`.

Miscompilations:

- The code that creates, fills and stores a module variable's value is deferred until the variable is first read, and an assignment to an object created by that initializer was deferred with it. After `Object.freeze(Object.prototype)` an assignment to an inherited name fails: a TypeError in strict code, silently dropped in sloppy code. Plain did the store before the freeze and compiled code did it after. main has this bug too.
- `Object.freeze(Object.prototype); enum Kind { toString, valueOf, plain }`: `bun run` fills an enum by assignments, which fail for a name that a frozen `Object.prototype` has. Compiled code defined the members instead.
- A loop's stores to a variable were delayed to the loop's exit edges, even across an operation that throws only when memory runs out. That error can be caught, and then the loop is left through none of those edges.
- A Map or Set lookup with the same key on every iteration was hoisted out of the loop, even when a `try` covered the lookup but not the loop's preheader, and even when the loop might not run: `for (let i = 0; i < 0; i++) map.has(rope)` threw a RangeError.
- A loop that does `const list = []`, pushes, reads `this.state.type`, reads `list[0]` and stores the list into a node: a value read back out of an open object was taken for this iteration's list.

Hangs and failed builds:

- esbuild's `transformSync()` and `buildSync()` never returned in a compiled program.
  `var threads; try { threads = require("worker_threads") } catch {}`, then
  `var start = given => new given.Worker(__filename, ..)` and `start(threads)`: the
  search for the files that Workers start from knew the module behind an import, a
  direct `require()` and a `const`, not behind a variable that is assigned or a
  parameter. The Worker could not be loaded, and the caller slept in `Atomics.wait()`.
- A package that says `new Worker(__filename)` got `this executable can't load a module
  from a dynamic path`, in an executable and in a test run built as one program.
- `mock.module("node:os", ..)` in a.test.js, `await import("node:os")` in b.test.js: the
  promise never settled and the test timed out. Since #44833 plain `bun test` takes a
  file's mocks away when the file ends, and nobody told the engine.
- `internal compiler error (LinkAndEmit): UndefinedLayout`, in two ways. `child.ts` has
  `import "unbuilt"`, which can't be resolved, and `export class Child extends Base`, and
  another file says `await import("./child")`: in a test run no test file ran. Or a package
  with `"sideEffects": false` has `export class Unused extends Base {}` beside
  `export const used = 1`, and main.ts imports `used` and, with `import type`, `Unused`.
  The layout of a class whose statement is not compiled named a parent that nothing defines.
- A build that never ended in the checker: a conditional type such as
  `T extends infer R ? R : never` was weighed by how it is written, not by what `T`
  stands for.
- `import.meta.glob()` was not expanded under `--aot`. It is now, by Bun's own parser:
  lazy, eager, `import: "name"`, `?raw`, `Object.keys()` of it, in a CommonJS file.

Wrong answers of compiled programs:

- `bottom[name]`, with `class Top { [name]() {} }`, `class Middle extends Top` and
  `class Bottom extends Middle`, had its type if `Bottom` was asked about first and no
  type if `Middle` was. Which came first went by the order of the files.
- `Object.getOwnPropertyDescriptor(require("node:_http_agent"), "globalAgent")` showed a
  getter and a setter. It is a value made at load, as in plain Bun.
- `export { default as t } from "./outside.txt" with { type: "toml" }`, for a file that
  is not in the program, lost its type, while `import` kept it.
- In a `.mjs` or `.mts` file `this` at the top is `undefined`. The name alone decides: a
  `.cjs` file in a package with `"type": "module"` still exports through `this`.
- A stack trace taken while `then` is read by `await thenable` listed the async function,
  which has suspended by then. A class whose decorated members need a constructor was
  shown as `new Task (unknown:1:11)`, not at its `class`.
- `for (3 times) try { require("./t.cjs") } catch {}` where t.cjs throws: the body ran once and the first error was thrown three times. It now runs again each time, as in node and `bun run`.
- main.mjs imports `./m2.mjs` and `./a.cjs`; m2.mjs imports `./b.cjs`; b.cjs requires `./a.cjs` between two logs; a.cjs uses nothing of CommonJS. a.cjs ran first instead of when b.cjs required it.
- `require("./m3.mjs"); Object.keys(require("./d.json"))` where m3.mjs imports d.json: plain `a,default`, compiled `a`.
- `Object.isExtensible(require("./e3.mjs"))` after an import of the same file, in a layered run and for a module left out by `--external`: plain true, compiled false.
- `package.json` says `"sideEffects": false` and a file says `import "./setup"`: a test run built as one program dropped setup and said nothing, while a layer ran what `bun build --compile` drops. Both now do what plain does.
- `const o = Object.create(Blob.prototype, {}); o.constructor = { get name() { return "Stored" } }; o.text()`: plain throws ERR_INVALID_THIS; compiled code threw "'name' was left out of the executable". The same for `Buffer.concat(v)` when `v` isn't an array.
- piscina's `new Worker(join(__dirname, "runner.js"), { workerData: filename })` with `require(workerData).default` in the worker didn't find the project's file. threads.js's `await spawn(new Worker("./job.cjs"))` failed with "this executable can't load a module from a dynamic path".
- A Worker file that nothing reachable can start is no longer put into the executable.

bun test --aot:

- `vi.mock()` and `jest.mock()` are hoisted above the imports, as #44833 does for plain
  `bun test`. With `import { captured } from "./side"; vi.mock("./mod", ..)`, where
  side.ts imports mod.ts, mod.ts ran and `captured` was the real value.
- `require()` of a mocked module answers the object that the factory gave, for `node:`
  names too.
- `mock.module("node:dns", f1); await import("node:dns"); mock.module("node:dns", f2);
  await import("node:dns")` answered the first mock again where a run is built in layers.
  Five mocks in a row gave 1, 1, 3, 4, 5.
- A compiled run of one empty test held 166 functions of the test runner's own code. It
  holds 2 again. `process.mainModule` follows the test file.
- A file of the project that writes on a built-in (`Buffer.isEncoding = ..`) now throws in
  the test run as it does in the executable. A green run said nothing about that line.
- A file that only Workers load has no `expect` or `test`, as in plain Bun.
- `// require("./x.cjs")` in a comment no longer decides whether x.cjs runs as CommonJS.
- A test that writes state.js and imports it afterwards gets the new text in more forms:
  a path from `process.cwd()`, and a helper in another file that takes the path or joins
  a name to its own folder.
- A type imported from a module in a layer below no longer costs the importing file its
  types.
- `--shard` with `--timings` spreads the files that are held apart for their memory over
  the shards, and the walk ahead of the deal asks for each name of a folder once.
- `/** @vitest-environment jsdom */` (also happy-dom, `@jest-environment`, `--environment=`, bunfig's `environment`): the file didn't run: "Cannot load '(internal)': it isn't in this executable". With jsdom 25 and happy-dom 17 it now gets its window, under `--isolate` too. Both packages evaluate text, so a run with such a file is built as an open program: about 5.5 s more per run.
- Fake timers replace the globals. Compiled code had bound `setTimeout` and friends to what the process started with, so `useFakeTimers()`'s own functions were silently refused and the real ones consulted the fake clock: a socket's own timeout never fired, and `advanceTimersByTime()` fired the library's timers.
- `jest.doMock("./lib"); lib.f()` called the real function, and `import sum from "./sum"; jest.mock("./sum"); jest.mocked(sum)` at the top of a test file got the real function: a mock without a factory arrived one step late.
- `export const a = 0` of a mocked module read as 0 in every importer, whatever the factory returned.
- Two `export *` barrels, one of them mocked. Without hoisting (`mock.module()` in a test, then `await import("./first.js")`) main prints `real mocked` where plain prints `mocked mocked`. main has this bug too.
- `mock.module("./other.ts", () => ({ v: "mocked v" })); (await import("./by-name.ts")).renamed` with `export { v as renamed } from "./other.ts"`: "Cannot access 'renamed' before initialization".
- A `require()` that a mock had answered kept that answer for the rest of the run, even after `mock.restore()` and `vi.doUnmock()`, so a later test passed or failed depending on what an earlier file had mocked.
- A CommonJS file behind a mocked ES module still ran.
- `jest.clearAllMocks()` and friends also end the mocks that are still waiting in the engine.
- `import("./user.js").then(..); await vi.dynamicImportSettled()` didn't wait for the import.
- `vi.resetModules()` keeps packages and `jest.resetModules()` doesn't, as in plain.
- A mock of a built-in module installed before the module loads takes effect.
- `import {} from "./m"` and `import "./m"` are hoisted below `vi.mock()` like every other import.
- A file in a `__mocks__` folder is compiled with the program, and `require()` finds a module mocked by a CommonJS file there.
- `vi.importActual()` and `jest.requireActual()`: the original next to its mock loads in a compiled run, may import files of the program, and works with a name computed at run time.
- A test file named on the command line that doesn't match `*.test.*` (`bun test --aot ./tests/latest.ts`), and a helper that writes a file and then `require()`s it.
- What only the preloads import is treated as the tests' own, in a layered run too. A preload that can't be compiled no longer ends the run silently.
- `--isolate=modules` runs compiled. In a layered run the files share one global object, as in plain.
- `--globals=vitest` in a run built in parts; `globalThis.describe`; `vi === vitest`; `vitest.mock()` is hoisted.
- vitest's `assert` and `should` are the project's own chai.
- `test.extend()`: fixtures, JSX in the body, its errors.
- A file that both a Worker and a test file load reads `expect`, `test` and the like from the realm it runs in.
- A test file that can't load (`const broken = ;`) is written to the JUnit report as a failed case, as plain does on main now.
- `typeof console.timeStamp` is `"undefined"` in a test run, as in `bun test`, Jest and Vitest. React's development build checks for it.
- The runner's own filters and the DOM formatting of `toMatchSnapshot()` accept an object of compiled code.
- A plain run keeps its workers: `--aot --shard` had changed how files are dealt to them.

Build time and memory:

- What a file uses of the library's functions was kept in four sets of 131,131 bits, each
  cleared and walked for every file. They are two sorted lists now. The two blocks of a
  module that is set aside are compressed in one go, not one by one.
- `class C1 extends C0 { m1() {} }`, 800 in a row: every inherited method was compared
  with itself in the class above, 380 of 540 MB.
- A build that is not a test run walked every expression of every file for writes on
  built-ins and threw the result away.
- `map.delete(k); map.set(k, v)` took a new slot in the table's index each time and never freed the old one, so every lookup of the key read every slot it had left behind: an iteration cost the size of the table. A deletion now frees the slot. It costs 5 more instructions on a set and on a delete; see "Numbers that went up".
- `new Worker(new URL("./w.js", import.meta.url))` inside a package, the way piscina, tinypool, workerpool and threads.js start theirs: under `bun test --aot` the file was compiled into no layer and ran in the interpreter. A loop of 3e8 iterations took 10,812 ms against plain's 320 ms.
- `class Below extends mix(Base)`: the whole caller was walked for every such call. At 3,200 classes that step took 8,306.6 M instructions and now takes 119.7 M. The generated code is byte-identical.
- Three more build steps that grew with the square of the program: choosing representations around a web of values, finding who reads a parameter that is passed on, and loads by a computed name, which now cost by the package.

Speed:

- A sloppy function that only reads `arguments` and has a closure over a parameter
  (Babel's `_objectSpread`) no longer makes the arguments object at every call.
- `Array.from(map)` and `Array.from(map.entries())`: 672 instructions an entry, now 132.
- 65 host functions have a signature that says what they answer. Without one, a call
  counted as making an instance of every host class.

bun check:

- `function f({ a: { a: .. b } }) {}` nested N deep took time quadratic in N: 20,000 deep took 23.6 s of processor time. Each element of the pattern asked whether the pattern is ambient, which walks up through all its parents. It now asks the parameter that was just found (`symbols.rs`). Same answers: nothing between a pattern and its parameter can be ambient. `test/cli/check/nested-parameter.test.ts`.
- `value.someName` on a `FromSchema<Schema>` (json-schema-to-ts) with a type parameter for `Schema`: `bun check` never finished. It now reports TS2589 and TS2590 where tsc does, in about 5 s (tsc 6.0.2: 13.5 s). `test/cli/check/schema-types-limit.test.ts`.
- A `this` parameter without an annotation keeps what it was assigned, and one that nothing gives a type is still reported. `test/cli/check/unannotated-this.test.ts`.
Jarred-Sumner added a commit that referenced this pull request Nov 1, 2026
…ever changes in it

    while (c === b && fuel-- > 0) { .. }     // neither c nor b changes in the loop; the test narrows c

`bun build --compile --aot` stopped with "Stopped while compiling f in SimplifyControlFlow", or in
PlaceTypeTests or RemoveTrivialParameters, whichever asked first. One form of it builds on main and aborts
since #47571; another aborts on main too.

A branch passes on its test's operand, narrowed, and what it may pass on is read off its condition. Hoisting
lifted the comparison out of the loop and left the branch behind. After a later copy of part of the loop the
condition was a block parameter, which proves nothing. The test of such a branch now stays in its block.

The fix is in oven-sh/WebKit#972. hoisted-test.test.ts goes from 0 of 4 to 4 of 4. Two generated listings
under engine-tests are regenerated.
Jarred-Sumner added a commit that referenced this pull request Nov 1, 2026
    // tsconfig.json: { "compilerOptions": { "jsx": "react" } }
    import React from "./tiny-react.js";
    import { label } from "./label.js";
    jest.mock("./label.js", () => ({ label: "mocked" }));
    test("a paragraph", () => { const made = <p>{label}</p>; });

Under `bun test --aot`: "ReferenceError: React is not defined". Plain passes, and so do main's `--aot` and
#47571. The same with a `jsxFactory`, with `vi.mock()`, and for a class that decorator metadata names, where
`design:paramtypes` held Object.

To hoist a mock above the imports, an import that is used is rewritten where each use is spelled. The factory
and the fragment of classic JSX, and a class named only by decorator metadata, are spelled nowhere, so there
was nothing to rewrite. An import with such a use now stays in place.

mock-beside-jsx.test.ts goes from 1 of 8 to 8 of 8.
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator Author

Closing: #47942 rewrites the front end of the ahead-of-time compiler and its tests for the engine's new compiler (oven-sh/WebKit#997, merged), so this no longer applies and would conflict with it. If the problem still shows once that is in, please open a new pull request against main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants