From 049109452e71a5322cd369a5ce1e200e422767c4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 20 Jan 2025 11:10:07 -0500 Subject: [PATCH] LibJS: Do not attempt to link modules which have failed to load Linking a module has assertions about the module's state, namely that the state is not "new". The state remains "new" if loading the module has failed. See: https://tc39.es/ecma262/#figure-module-graph-missing In any case, this exception causes a loading failure, which results in A's [[Status]] remaining new. So we must propagate that failure, instead of blindly moving on to the linking steps. --- Libraries/LibJS/Runtime/VM.cpp | 5 ++++- Libraries/LibJS/Tests/modules/basic-modules.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index 5ce09f0d403..0761100a089 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -551,7 +551,10 @@ ThrowCompletionOr VM::link_and_eval_module(Badge, S ThrowCompletionOr VM::link_and_eval_module(CyclicModule& module) { auto filename = module.filename(); - module.load_requested_modules(nullptr); + auto& promise_capability = module.load_requested_modules(nullptr); + + if (auto const& promise = verify_cast(*promise_capability.promise()); promise.state() == Promise::State::Rejected) + return JS::throw_completion(promise.result()); dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] Linking module {}", filename); auto linked_or_error = module.link(*this); diff --git a/Libraries/LibJS/Tests/modules/basic-modules.js b/Libraries/LibJS/Tests/modules/basic-modules.js index 23b61dbdad8..1fa49198889 100644 --- a/Libraries/LibJS/Tests/modules/basic-modules.js +++ b/Libraries/LibJS/Tests/modules/basic-modules.js @@ -219,6 +219,10 @@ describe("in- and exports", () => { expect(result.default).toBeInstanceOf(RegExp); expect(result.default.toString()).toBe(/foo/.toString()); }); + + test("importing a non-existent file results in a SyntaxError", () => { + expectedModuleToThrowSyntaxError("./i-do-no-exist.mjs", "Cannot find/open module"); + }); }); describe("loops", () => {