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/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); } } 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); }