From 426ce77702fa513653fa52ed17c37121916f60d4 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:03:30 +0000 Subject: [PATCH 1/2] [JSC] Module loader: replay a failed fetch with the error the host rejected with ModuleRegistryEntry::error() and JSModuleLoader::hostLoadImportedModule() copied every ErrorInstance a fetch was rejected with through duplicateError(), which keeps the type and the message only. They now call maybeDuplicateFetchError(), as moduleGraphLoadingError and moduleLoadTopRejected do: an error the host marked with moduleFetchFailureKind is still copied, any other error is replayed as the object the host rejected with. --- ...-loader-fetch-failed-replays-host-error.js | 38 +++++++++++++++++++ .../module-fetch-failed-replay/importer-a.js | 2 + .../module-fetch-failed-replay/importer-b.js | 2 + .../JavaScriptCore/runtime/JSModuleLoader.cpp | 6 ++- .../runtime/ModuleRegistryEntry.cpp | 3 +- 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 JSTests/stress/module-loader-fetch-failed-replays-host-error.js create mode 100644 JSTests/stress/resources/module-fetch-failed-replay/importer-a.js create mode 100644 JSTests/stress/resources/module-fetch-failed-replay/importer-b.js diff --git a/JSTests/stress/module-loader-fetch-failed-replays-host-error.js b/JSTests/stress/module-loader-fetch-failed-replays-host-error.js new file mode 100644 index 0000000000000..f944237bdb4ee --- /dev/null +++ b/JSTests/stress/module-loader-fetch-failed-replays-host-error.js @@ -0,0 +1,38 @@ +//@ runDefault + +// A module whose fetch was rejected while another module imported it keeps a +// FetchFailed registry entry, and the load of every later importer is settled +// from that entry by JSModuleLoader::hostLoadImportedModule(). It used to hand +// out JSModuleLoader::duplicateError()'s copy of the stored error, which keeps +// only its type and message. That copy exists for errors the host marked as +// fetch failures (moduleFetchFailureKind); an error the host rejected with +// itself, like the one this shell creates for a missing file, must be replayed +// unchanged, as the other replay sites (maybeDuplicateFetchError) already do. + +var abort = $vm.abort; + +async function shouldReject(promise) { + try { + await promise; + } catch (e) { + return e; + } + throw new Error("did not reject"); +} + +(async function () { + const first = await shouldReject(import("./resources/module-fetch-failed-replay/importer-a.js")); + if (!String(first).includes("does-not-exist")) + throw new Error("importer-a: unexpected error: " + first); + first.seenByFirstImporter = true; + + const second = await shouldReject(import("./resources/module-fetch-failed-replay/importer-b.js")); + if (second.message !== first.message) + throw new Error("importer-b: unexpected error: " + second); + if (second !== first || !second.seenByFirstImporter) + throw new Error("importer-b: rejected with a copy of the stored fetch error instead of the error itself"); +}()).catch((error) => { + print(String(error)); + print(error.stack); + abort(); +}); diff --git a/JSTests/stress/resources/module-fetch-failed-replay/importer-a.js b/JSTests/stress/resources/module-fetch-failed-replay/importer-a.js new file mode 100644 index 0000000000000..394f05657e28c --- /dev/null +++ b/JSTests/stress/resources/module-fetch-failed-replay/importer-a.js @@ -0,0 +1,2 @@ +import "./does-not-exist.js"; +export const a = "a"; diff --git a/JSTests/stress/resources/module-fetch-failed-replay/importer-b.js b/JSTests/stress/resources/module-fetch-failed-replay/importer-b.js new file mode 100644 index 0000000000000..da15dce97cc15 --- /dev/null +++ b/JSTests/stress/resources/module-fetch-failed-replay/importer-b.js @@ -0,0 +1,2 @@ +import "./does-not-exist.js"; +export const b = "b"; diff --git a/Source/JavaScriptCore/runtime/JSModuleLoader.cpp b/Source/JavaScriptCore/runtime/JSModuleLoader.cpp index 9c4ab3d0902a0..5d59c7ced172f 100644 --- a/Source/JavaScriptCore/runtime/JSModuleLoader.cpp +++ b/Source/JavaScriptCore/runtime/JSModuleLoader.cpp @@ -887,7 +887,11 @@ JSPromise* JSModuleLoader::hostLoadImportedModule(JSGlobalObject* globalObject, // To avoid a race condition, instantiation errors need to be checked later, not here. if (JSValue fetchError = mapEntry->fetchError()) { if (auto* errorInstance = dynamicDowncast(fetchError)) { - fetchError = JSModuleLoader::duplicateError(globalObject, errorInstance); + // Like moduleGraphLoadingError and moduleLoadTopRejected, only copy the errors the host marked as + // fetch failures (the WPT requirement duplicateError exists for). Anything else the host rejected + // with is replayed as-is: duplicateError keeps only the type and message, which drops own properties + // such as AggregateError's errors. + fetchError = JSModuleLoader::maybeDuplicateFetchError(globalObject, errorInstance); RETURN_IF_EXCEPTION(scope, nullptr); } diff --git a/Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp b/Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp index 1949159617173..7aa24844a97db 100644 --- a/Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp +++ b/Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp @@ -157,8 +157,9 @@ JSValue ModuleRegistryEntry::error(JSGlobalObject* globalObject) const auto scope = DECLARE_THROW_SCOPE(vm); if (JSValue error = m_error.get()) { if (m_status == Status::FetchFailed) { + // See JSModuleLoader::hostLoadImportedModule: only host-marked fetch failures are copied. if (auto* errorInstance = dynamicDowncast(error)) - RELEASE_AND_RETURN(scope, JSModuleLoader::duplicateError(globalObject, errorInstance)); + RELEASE_AND_RETURN(scope, JSModuleLoader::maybeDuplicateFetchError(globalObject, errorInstance)); } RELEASE_AND_RETURN(scope, error); } From 5ba62c846c03971270db0e50bb47278595aae848 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 7 Nov 2026 03:38:32 +0000 Subject: [PATCH 2/2] [JSC] A rejected fetch of a load by name registers nothing When the fetch of a load by name rejected with an error that the loader had tagged as the error of another module, moduleLoadTopSettled registered the key with that error. In Bun that is an onLoad hook or a module factory that awaits an import() which throws. The entry has no record and no promises. The next import() of the key is answered from it without a fetch, and a static import of the key never settles: hostLoadImportedModule() waits for a fetch promise that nothing settles. With the additions on, a rejected fetch registers nothing, whatever the error is of. No module of the key ran, and the next load fetches again, as it already does after a fetch that rejected with an untagged error. --- Source/JavaScriptCore/runtime/JSMicrotask.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/runtime/JSMicrotask.cpp b/Source/JavaScriptCore/runtime/JSMicrotask.cpp index 3ac43b8d9f568..c40d7030437a6 100644 --- a/Source/JavaScriptCore/runtime/JSMicrotask.cpp +++ b/Source/JavaScriptCore/runtime/JSMicrotask.cpp @@ -1379,9 +1379,13 @@ static void moduleLoadTopSettled(JSGlobalObject* globalObject, VM& vm, ThrowScop intermediatePromise->pipeFrom(vm, combinedPromise); } else { // onFetchRejected logic + JSValue errorValue = arguments[1]; + // Bun: nothing was fetched, so no module of the key ran. The key is not registered with the error, whatever + // module the error is of: the next load of the key fetches again. An entry made here has no record and no + // promises, and a static import of its key would wait for a fetch promise that nothing settles. +#if !USE(BUN_JSC_ADDITIONS) const Identifier& specifier = context->moduleRequest().m_specifier; auto type = context->moduleRequest().type(); - JSValue errorValue = arguments[1]; if (auto* error = dynamicDowncast(errorValue)) { auto failure = JSModuleLoader::getErrorInfo(globalObject, error); // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-module-script step 13.1 @@ -1395,6 +1399,7 @@ static void moduleLoadTopSettled(JSGlobalObject* globalObject, VM& vm, ThrowScop entry->setEvaluationError(globalObject, error); } } +#endif intermediatePromise->reject(vm, errorValue); } }