Repository navigation
Conversation
These cases pass today. They cover inputs that a change to the turns of module mocks can break: a factory that calls require() for another mocked module, an async onLoad() callback that declines beside a mock, a mock that is removed or replaced while its load waits for its own onLoad() callback, and a module behind an async onLoad() callback that a factory imports while another load fetches it.
…oad that can give it A mock that waited for its turn had a place in one load only, the first load that reached its module. While a factory of that load was pending, the mock waited for it, also when another load asked for the mock. So a load that an onLoad() callback started never finished when it imported a mocked module of the load that the callback belonged to. A waiting mock now has a place in every load that reaches it. It takes its turn in the first of those loads in which no other factory has the turn. Two loads that share only a mocked module are no longer fetched together. When another load comes to import a mock that waits behind a turn, the mocks are ranked again.
…oad is fetching When a running factory imported a module that another load was still fetching, the runtime could not tell whether that module imports the mocked module, and gave the factory a private copy at once. So a module behind an async onLoad() callback ran twice, and the callback was called twice for one file, unless the two loads happened to share a module. The import(), vi.importActual() and vi.importMock() calls of a running factory are now held until the loader has that module and has asked for its imports. Then the call is resolved again: it shares the module, or gets a copy if the module turns out to wait for the mock. require() and static imports cannot wait, and keep the copy.
…iffer under --aot The test of Bun.plugin.clearAll() while an import() of a factory waits is a test of its own. Compiled runs the files themselves there, so the two lists of the --aot suites name it. The tests of the wait itself pass compiled.
… for its mock A mock that two loads import took its turn in one of them, and the factory held the turn in that load only. A later mock that both loads import after it then started in the other load, beside the pending factory that comes before it in both. Two pages loaded with Promise.all(), which import an async mock and then a mock that is built on it, passed before the mocks had a place per load and failed after. The factory now has the turn in every load in which its mock has a place. A mock still does not wait for the pending factory of a load that alone imports that factory's module. The import() of a factory that waits for a fetch in flight no longer leaves its target in the load roots before it is resolved again, no longer lets what an onResolve() callback resolves wait in its place, and is one import() for vi.dynamicImportSettled(). The tests add the shape without a gate, bun-plugin-svelte with a $state module, and the price of the wait: a test file that needs the mock before it lets the other load's callback answer never finishes loading.
Collaborator
Author
|
Updated 10:01 AM PT - Nov 15th, 2026
❌ @robobun, your commit 4af1051 has 2 failures in
🧪 To try this PR locally: bunx bun-pr 49027That installs a local version of the PR into your bun-49027 --bun |
Collaborator
Author
|
Status: draft. It waits for an answer to the question in the description. How I reproduced it
CI at 4af1051 (build 141899, finished)
PR: #49027 |
…same in every run under --aot The next file looked at the events after one read of a file. A compiled import() of the factory is not dropped with its test file, and on the lane with a sanitizer it went on before that look in some runs and after it in others. The next file now loads the module itself and then reads one more file, so a compiled import() has always gone on by then. The test is named in runner-suites.known.txt. Its list of runs is now the same plain and compiled, so its line leaves runner-suites.runs.txt. The two tests of two import() calls in one tick get lines there: which importer runs first is not fixed in a plain run.
Collaborator
Author
|
Closing as stale: this PR has had no review and no new commit since 2026-11-15. The branch stays, so the work is not lost. To pick it up again, update the branch from main and reopen this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Question for @Jarred-Sumner: c7e4231 (#44833) gives a mock factory a private copy of a module that another load is fetching. This PR makes its
import()wait for that fetch, as Bun 1.4.4 does. Copy or wait?Problem
bun-plugin-svelte, avi.mock()factory imports a$statemodule while two components load. The module runs twice. Bun 1.4.4 passes 12 of 12 runs, main 1 of 12.isWaitingForModule()(src/jsc/bindings/BunPlugin.cpp:8057) copies a module in flight for the factory.rankWaitingModuleMocks(),:2623).Fix
import(),vi.importActual()andvi.importMock()wait until the loader has the module, then share it.test/js/bun/test/mock/mock-module.test.ts(33 new tests, 19 fail on main). Other suites: Notes.Background
import()call or one test file's static imports. Its factories run one at a time.Downsides
TypeError: require() async module. Main passes.import()call costs 9 more instructions. Release.textgrows 9,728 bytes.Notes
The repro with the first-party plugin (
bun test --preload ./preload.ts ./state.test.js,node_moduleswithsvelte5.20.4 andbun-plugin-svelte). It is the new testa $state module of bun-plugin-svelte runs once for a factory and a component.Runs that pass: Bun 1.4.4 12 of 12, main (ed43633) 1 of 12, this PR 12 of 12. The same shape with a hand-written
asynconLoad()and no gate is in the tests..., by loads that share no module.What the author of the rule wrote. c7e4231, 2026-10-18: "A mock that is reached through a load's imports now waits until no load is reading a file, so the question always has an answer. It doesn't wait for a plugin's onLoad() callback in another load, which may depend on the test's progress." The start of a factory still does not wait for such a callback. The factory's own
import()of that module now does. After the wait the call is resolved again: the factory shares the module, or gets the private copy if the module imports the mocked one.Both answers, measured (release builds: Bun 1.4.4, main ed43633, this PR 03355f286):
import()s a module behind an asynconLoad()of another load (7 test rows)never finished loading... never settles)never finished loadingPromise.all(), an async mock and a mock built on itAddressed from the self-review:
onResolve()callback resolves does not wait in the place of the import.vi.dynamicImportSettled().rankWaitingModuleMocks()says what waits now.bun-plugin-svelte, two tests without a plugin, and the cost of the wait.Open items. This PR stays a draft until each is fixed or decided.
onResolve()callback is asked once more for each wait. An alias that a plugin resolved is lost ifBun.plugin.clearAll()ran meanwhile.onLoad(), in flight for another load: 399 rankings against 1 on main at 200 modules, and 1.3 s against 0.11 s at 3,000 modules.import()has no registry entry until its fetch settles, so the wait does not see it. The callback is still called twice there, with one instance.vi.dynamicImportSettled()waits for a waiting import. Three fixtures that call it before they let the callback answer go from pass to a 5 s timeout. It is the first downside again.jsFunctionResumeImportsAfterFetches()has no throw scope around its loop.The second part is not opened. The hang that started this work needs a change to the scheduler: a fetch that a plugin callback holds must hold back only the loads that import its module (
error: "b.test.js" never finished loadingafter an earlier file leftimport("./slow.js")behind a callback that never answers). A build of that part (branchrobobun/10e3cf25/mock-turn-after-unfinished-fetch) passes 73 of 74 fixture rows where main passes 23. It also gives a module behind a fresh helper's static import a second instance where main gives one, so it is not ready.Measured on release builds, lto off, main ed43633 against this PR (03355f286).
stepi):moduleLoaderResolve()256 and 256,moduleLoaderFetch()198 and 198,moduleLoaderImportModule()270 and 279 (53 and 55 conditional branches).size.text: 150,971,049 and 150,980,777 (+9,728 B).BunPlugin::OnLoadgrows 40 bytes, a waiting mock 16 bytes. One host function is added.onLoad()52 and 52, 200import()roots 30 and 30. One waiting import adds 2 rankings and saves the second callback call, transpile and evaluation.perf,valgrindandhyperfineare not installed on the machine.Suites run on the debug build of this PR:
mock-module.test.ts350 pass,mock-hoist.test.ts134,vitest-flavor.test.ts191,vitest-apis.test.ts151,vi-utils.test.ts193,isolation.test.ts154,rerun-each.test.ts10,plugins.test.ts278 pass and 1 fail (the same GC test fails on main's debug build). On the release build:test/aot/runner-suites/mock-module-{1..6}andmock-module-runs-{1..6}, 12 of 12.Under
--aot. A compiledimport()does not passmoduleLoaderImportModule()and does not wait. Four tests behave differently compiled and are named intest/aot/runner-suites.known.txtorrunner-suites.runs.txt, each with its reason. Main's compiled run gives the same result for the two that pass on main.Open PRs whose merge with this branch conflicts in a file that this PR changes: #48795 (
runner-suites.runs.txt), #48910 (docs/test/mocks.mdx), #48967 (BunPlugin.cpp,BunPlugin.h), #48503 (ZigGlobalObject.cpp,runner-suites.known.txt).no test proof · iteration 2 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/test/mock/mock-module.test.ts