bun
Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
by oven-shRust
Commit activity
Last 12 weeks · 1268 commits
Community health
5 of 6 standards met
Recent PRs & issues
Problem With , a file that calls on itself builds with exit 0 and then fails: . () treats a record that names the importing file as an import inside the chunk. The printer then calls a wrapper that no chunk declares. Fix A self in a file with no wrapper now loads the chunk of that file. A file with a wrapper keeps its output. Scan step 1 asked this for lifted CommonJS before the wrappers were final, so that case moves after step 2. skips self records: no chunk layout changes. Verified: (30 cases, 20 fail before), , . Self-reviewed: 12 concerns, 11 addressed (Notes). Background A wrapper (, ) is a function that runs a file on the first call. Under , an target has none: it is an entry point with its own chunk. #36191 and #44828 (by @dylan-conway, whose tests are here) did this for every self . That changes 7 programs that are right today, all in files with a wrapper. Downsides An ES module entry with no wrapper whose self runs while a split loads it: a before, a promise that never settles now, as in unbundled Bun (oven-sh/WebKit#871). A browser build with a self gains the chunk preload helpers: 169 to 1,371 bytes for the repro. Bundler : +1,343 bytes. An entry with no in its name names itself (). A page that loads runs it twice. Notes Repro (1.4.0, 1.4.3 and main): The output has and no chunk declares . and do the same. The file does not have to be the entry point. Where this came from. No user reported it on this tracker. A differential run of bundles against the unbundled program found it. One shape is a regression of 1.4.1: a file that imports itself and that another file s, with . 1.4.0 prints , because the gave the file a wrapper. A split (#40519) does not, and 1.4.1 prints (). Size. Release of the rlib: 2,110,310 bytes on main, 2,111,653 here. The one-file repro minified: 141 to 718 bytes for , 149 to 126 for . esbuild. esbuild has the same clause and prints the same call (evanw/esbuild#3043). Alternative weighed. A forced wrapper on the file: it changes when the file and its imports run, and the build without that does this fails for a self before the first top-level await (#47426). The rule. answers "this call loads a chunk" for a self record only when the record is an and the file has . Every other self record keeps main's answer. So the output of a file with a wrapper is byte-identical to main's. This differs from esbuild's only where the esbuild form throws. Guards. Ten cases assert main's output for files with a wrapper, because the earlier form of this change broke them: a CommonJS entry whose self runs or is pending while a split loads it, an ES module that s itself, with 11 shared exports, , a non-ASCII file name, run-time , and a lifted CommonJS file that a later file wraps. What the call returns. When no other file imports the file statically, the chunk is the file, and the loader returns the namespace of the running module. When another file imports it statically, the code of the file is in a shared chunk and the call loads the small entry chunk that re-exports it (94 to 102 bytes in the layout of ). An of the file from any other file gets that same namespace (). Fuzzer. skipped a self because it threw. It now draws one. Pinned graph 47 has one and fails on main. With the draw on, 28 of 300 graphs crash on main and none on this branch (bundle against unbundled run, seeds 1 to 300, 0 graphs right on main and wrong here). Not in this PR. Other calls of a wrapper that no chunk declares: #39488 (), #38621 (), #46072. A self before the first top-level await in a build without (#47426), and under the dev server (#46770). The entry chunk that re-exports a file does not carry the names of (#47510). The metafile path of the record (#47337). The promise that never settles when an of a module runs while a is loading that module (oven-sh/WebKit#871). It needs no self . A conflict to expect: a branch for the evaluation order of modules shared with a split target also changes . Self-review. Raised: the earlier form broke 7 working programs (reworked to the no-wrapper rule), wrap flags not final in scan step 1 (moved after step 2), the fold read the predicate (self records skipped, layout unchanged: a 4-file probe prints with 5 files on main and on this branch), the read of the column in the cross-chunk walk (the walk now passes the that it holds to ), a dead clause in the arm (removed), missing cases (HTML page, server manifest, lazy double call, fuzzer draw: added), the body (rewritten). Not done: a tracking issue. This account cannot open issues. Question for @dylan-conway. #44828 had this hunk for every self and you closed it after a day. Was there a reason that also applies to this narrower form? Suites run** (debug build, main e67258f3348 and this branch): (239 pass), (33), (154, and times out on main too), , , , , , , , , , , : 0 new failures.
Problem A callback outside a microtask is the first to read . The tick queue then drains behind the next microtask: . Node prints . A listener added after an misses , or an uncaught exits 1. () arms a one-shot drain for every new tick queue. Outside a microtask nothing spends it. Fix arms the drain only inside a checkpoint of the default microtask queue. Correct: the checkpoint after a callback already runs ticks, microtasks, then their ticks. Entry point bodies run inside a microtask. Verified: (six fail on main), two AOT fixtures. Self-reviewed: 17 concerns raised, 14 addressed. Not done: tracking issues, todo rows (Notes). Background The first read of creates a global object's tick queue (per thread, or per test file). JSC calls the hook after each microtask. Bun drains once through it, because Node runs a CommonJS entry point's ticks before its promise jobs. Considered: arm the drain only behind a CommonJS entry point. Nobody built or asked for it (Notes). Downsides Not changed: a tick queue that a microtask creates still drains behind it. pins that. , child stdio, bind and post callbacks lose an accidental first-round pass. They print , as in later rounds. Cost: +18 bytes. One call, compare and branch per tick queue. Notes Repro. No imports, so nothing reads before the timer runs. The second round is the control. Node runs the tick queue when the outermost callback scope closes, after the microtask checkpoint: , . Programs that die. No user reported this. These are programs I wrote. In each, a callback (a timer or an immediate) is the first to load or touch a built-in that reads , and a listener is added after an . All three are in the test file. When it applies. Only when nothing read before the callback. In a callback, a first load of , , , , or reads it, and so does a first use of . , , , , , , and do not. The tick queue belongs to the global object, so has one first round per test file. Mechanism. () creates the queue on the first read and calls . That set the flag and installed as the hook. The hook clears the flag and drains the queue once, after the microtask that ends next. A queue created in a timer left the flag set until some later microtask ended. The gate reads . JSC sets that for the whole of , the loop that calls the hook. What changes. 52 probe scripts on debug builds of main and of this PR, each in a fresh process. Six rows change, all to Node's order: the repro as and , in a global , in a callback, in a forked child, and the script above. As and executables, three repro programs change in the same way and eight other programs do not change (22 rows, 6 change). Two more shapes, measured one by one, also print Node's order in their first round now: a job of a context's own microtask queue that an immediate runs, and a function of a module that the host runs from a timer. What does not change. Every entry point shape: and top level, entries that import or require , or a file that reads , , , top level, and global entry points, forked children. Not changed: a microtask creates the queue. The drain still runs right behind that microtask. Node's output first, then Bun's, before and after this PR: a microtask in a timer reads and queues the tick: / the first promise job of an entry point top level: / a CommonJS module that a microtask requires: / the continuation of a top-level await: / the third program of the table above without its write to : / uncaught , exit 1 a context with its own microtask queue, run from a microtask: a tick and a microtask of the main realm run inside as the first statement of the entry file gives Node's order in each of these rows. (#47942) expects the current order for the first row under , so a change there needs a maintainer's decision. I asked in https://github.com/oven-sh/bun/pull/43366#issuecomment-6858596451. The accidental passes. Bun calls some Node-style callbacks from a promise reaction or from . Node calls them from native code, so a tick that the callback queues runs before its microtasks. On main the leftover one-shot gives that order to the first such callback after a queue creation outside a microtask. With this PR the first round prints what every later round prints. The sites: callbacks: the reactions in ( at , and the other functions that pass through ). The bind callback () and (). of a child's when the read was pending (). The write callback and of and of a piped , for a write that the pipe cannot take at once (, ). For each of these, Node prints in every round. Main prints that in the first round and in the second. This PR prints in both. Example: a timer writes to for the first time and then calls . The callback sets a flag in a tick and reads it after . Node and main read the flag as set. This PR reads it as not set. Main does too once one unrelated microtask ends between the write and the lookup, or when the program loads at startup. The todo in #48713. in . With #48713 merged into main it fails (the program prints nothing). With #48713 and this PR it passes: reports "this test is marked as todo but passes". This PR alone does not make that program pass, because on main the write callback of a stream does not come from a microtask. Whichever of the two lands second removes the . The other design. With the arm on queue creation already spent before the entry point (, where the file is ), main prints Node's order in every row of "Not changed" above. The one exception is the top level of a CommonJS entry point, which #43366 handles after a preload. So an arm behind the entry point could replace the arm on queue creation. Nobody has built that or asked for it, and I did not establish what it needs. Three things I found: #43366 arms only when the queue exists before the body, so its predicate would have to change. would flip. Node does not print one order for the top level of an ES module entry point. v26.3.0, v26.12.1 and v26.13.1 print the promise jobs first. v26.12.0 and v27.0.0-alpha.0.1.0 print the ticks first, as Bun does when the body is the first reader. I did not look at how an ahead-of-time compiled entry point would arm. That design would delete the lines of this PR and keep its tests. A nearer step that nobody built either: keep the arm on queue creation and take it only while a module body is on the stack, which is the mark of #33088 read at the arm. Self-review: what I did not do. No tracking issue for the unchanged half or for the callback sites above. I asked the question in the comment on #43366 instead. No rows for the callback sites. Each row belongs in the test file of its module, with its fix. Cost. Release object of (), main against this PR: 236,082 to 236,100 bytes. grows from 82 to 100 bytes. The function runs once for each tick queue. Outside a checkpoint it now returns before it allocates the 16-byte hook wrapper. Suites.** Debug builds, Linux x64, main against this PR. : 16 pass and 6 fail on main, 22 pass with this PR. The six are new. The seventh new test, for the CommonJS entry point, passes on both and guards the entry order. : passes on both. : 46 pass on both. 319 files of Node's : ticks, microtasks, timers, streams, , workers and all 97 . The same 316 exit 0 on both builds. The same 3 fail on both: , , . These files cannot take the new branch, because loads in the entry body. They show that nothing else moved. This PR adds no passing Node test. AOT: 39 programs of that use , with the two new fixtures, four builds each. 38 pass in every build. failed under in one run. Run alone, it passes with this PR and fails the same way on main. (197 pass, 3 fail), (152, 1), (417, 6), (43, 1), (164, 0): the same tests fail on both builds. They are time and memory limits of a debug build in this container.
Problem Without , a test file that asks for a DOM gets none after an earlier file left (a save and restore does that). Nothing prints. Tests fail: . tests (, ). A key that holds counts as a DOM. Fix Both tests now ask what the key holds: an own data property that is or is no DOM, and the window opens. Every other counts as before: a stub, a registered DOM, an accessor (not called), a non-configurable property, a reset beside the node classes of a hand-registered DOM. The document of a window that Bun keeps, put back by a hook, is no other DOM. Verified: , 31 new tests (16 fail on main), 18 mutants. Self-reviewed: 21 concerns raised, 18 addressed. Background A test environment puts a window's names on . Without all files share that global, and Bun keeps one window per environment. Considered opening the window over a stub too. That changes what a preload's stub does and needs #48777. #44833 left that case. Downsides An no longer hides a failure that main has without it. Four measured suites lose one test each (Notes). A stub that nothing removes still turns the environment off. Builtin JS +914 B. is byte-identical. 5 property lookups when the leftover is met. Notes Repro (no flags, a stand-in package, the same with real happy-dom 17.0.3) Main prints , 2 pass, exit 0. This PR opens the window. The same holds when a DOM file runs ahead of , and when the DOM comes from or and has . With the package missing, main exits 0 where says the file fails. This PR prints the warning for a comment and fails the file for the project's environment again. Earlier discussion. This line was raised in the review of #44833 (https://github.com/oven-sh/bun/pull/44833#discussion_r4323859206) and left with: "Leaving this one. It needs a file that puts a fake on and never removes it, no , and a later file that asks for a DOM with a comment." This PR does not change that case. It changes what the sentence does not cover: a file that removes its fake by assignment (the key stays), and the project's environment after such a file (no comment on the later file). The rule after this PR. With none of Bun's windows left on and a key there, a DOM file gets its window in two cases only: is an own, configurable data property whose value is or , and none of , , , on is a node class (a function whose prototype has ). is an own, configurable data property whose value is the document of a window that Bun keeps. In every other case the file gets no window, as on main. The rule is the same for a comment and for the project's environment. , and the Rust side are not changed. and are not changed. Outcomes that change against main. Release builds of d0f4fdd65fb and of this PR, real happy-dom 17.0.3 and jsdom 25.0.1, pass/fail as main, then this PR. Better: A project with , one file that saves and restores , two React Testing Library files: 1/2, then 3/0. A preload that puts Bun's own document back in (), in a project with an environment and one file: 2/2, then 4/0. Four more suites of this kind: 2/1, then 3/0. With : 2/2, then 4/0. Worse. Each result is what main gives when the leftover is absent: A file with a leftover DOM comment that asserts , after a file that left : 2/0, then 1/1. Alone it fails on main. A commented file that registers its own DOM through a shared helper, after such a file: 4/0, then 3/1. Without the leftover, main fails the same test. The same three files after a preload that sets : 3/0, then 2/1. A non-extensible with left on it: a file with a comment now gets the warning , and a file of the project's environment fails. Main reports the same when the leftover is absent, and nothing with it. A file leaves and a fake , a DOM file follows, a file deletes the fake, and a second DOM file needs the real : 4/0, then 3/1. With the fake alone, main is 3/1. The cause is , which #48777 changes. The same on both builds: the real registered by a test file, a hook or a preload (16/0, 2/0, 5/0, 5/0, 3/0, 3/0), the same after a file reset only (4/0 four times, 13/0 twice, 12/1 twice, 19/0, 14/0, 10/0), a jsdom that a test file registered in part by hand and whose was reset (2/0 twice), a preload's (3/0), a counting getter (4/0 twice), a stub and a fake that a file never removes (4/0, 4/0, 9/0). Not fixed here A stub that a test file never removes still turns the environment off, with no message: in a file of a project with an environment is 2/1 on both builds. The same holds for , which is not configurable. : a name outside the fixed list that a test file left hides the window's name (#48777). reads the same key (). After of the repro it is in a later file with no DOM. The wider change, if it is wanted. The earlier commits of this PR tested what a non-null is ( and a ), so that a stub left behind is no DOM either. Review found what it costs: a preload's stub stops counting unless a record of the preloads is kept (45 lines of Rust) or #48219 lands first, a window opens beside more leftovers (9/0 to 3/6 in one project with a fake , it needs #48777), and the runner calls getters that test files defined (4/0 to 3/1 in one suite). It also reverses the decision in #44833. So it needs a maintainer's answer to one question first: does a stub that nothing removes still turn a later file's environment off? Related PRs. #48219 edits the same two lines for a preload's stubs. This PR keeps main's outcome for every non-null , so its rows hold. #48777 changes . Its docs sentence "A file also gets no window if an earlier test file left a on " needs "that is not " after this PR. #47727 and draft #45415 touch the same function. Measurements. Release builds of d0f4fdd65fb and of this PR from one build directory. Counts are hardware-breakpoint counts. 50 one-test files per fixture. : 124,595,689 on both and byte-identical, unchanged, 2,787,195 to 2,788,109, file size unchanged. 50 files without a DOM: host calls 0 to 0, loads of 0 to 0. 50 happy-dom files in a row: host calls 12,138 to 12,138, descriptor objects 11,942 to 11,942, lookups of 1 to 1. The same with : host calls 23,800 to 23,800. The same 50 files after a file that left : 12,143 host calls and 11,944 descriptor objects on this PR (5 and 2 more than without the leftover, once). Main opens no window there. Not installed here: , , . Tests and mutants. 31 new tests. On the canary of main (1.4.5-canary.1+d0f4fdd65) 16 fail and 15 pass. The 15 pin what must not change: a stub, a non-configurable and an inherited for a comment and for the project's environment, a getter that must not be called, a reset beside each of the four node classes, the real in three forms, and a jsdom registered in part by hand. Mutants of the built JS, each run with the module swapped under the debug binary, and the tests that fail: main's key test at both places 16, main's key test at the second place 2, no 2, no key test 3, a stub is no DOM 3, counts 1, no kept-window check 3, no node classes 6, one name less in the list 1 (four mutants, 2), a class by only 2, no 1, an accessor is read as a value 1, no non-configurable clause 2, an inherited opens the window 2, a class read through a getter 1. A build without the second key test passes every test: leaves at once when a name became non-configurable, so no state that a test can reach differs. It stays, as on main. 144 tests pass on the debug build with (real jsdom loads in about 13 s there) and on the release build of this PR. Self-review. The last review of the previous shape raised 21 concerns. This push answers 18: the smaller gate that it asked for, no Rust record, no getter call, the kept-window check on both real packages, the partial registration, and the stale PR text. Not done: a non-configurable stub still gives no message (as on main), the case above (it needs #48777), and the ruling on stubs. History of this change. A first version decided once, when the preloads finished. It opened Bun's window over a DOM that a test file had registered (16/0 to 12/4 with the real registrator). A second version tested what a non-null is. It changed what a preload's stub does, so a record of the preloads came back. A third version failed the setup for a non-configurable stub and opened the window over a hand-registered DOM whose alone was reset. Review found suites that pass on main and fail there. This version changes only what and do. Build note. A debug build of main d0f4fdd65fb does not compile until #48812 or #48722 lands ( lacks an include). The debug runs above used that one-line include on top of this branch. It is not part of this PR.
Repository: oven-sh/bun. Description: Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one Stars: 96680, Forks: 5281. Primary language: Rust. Languages: Rust (77%), C++ (11.8%), TypeScript (9%), C (1.2%), JavaScript (0.6%). Homepage: https://bun.com Topics: bun, bundler, javascript, javascriptcore, jsx, nodejs, npm, react, rust, rust-lang, transpiler, typescript. Latest release: bun-v1.4.4 (3w ago). Open PRs: 100, open issues: 9853. Last activity: just now. Community health: 100%. Top contributors: Jarred-Sumner, robobun, dylan-conway, nektro, cirospaciari, small-angel, Electroid, alii, colinhacks, pfgithub and others.