ECMA-262 in Lean

Spec issues · defect of the text · docs/spec-issues/02-gather-available-ancestors-empty-cycle-root.md

02. GatherAvailableAncestors reads the [[CycleRoot]] of a module whose evaluation threw

Location
§16.2.1.6.1.3.3 GatherAvailableAncestors (#sec-gather-available-ancestors), step 1.a; §16.2.1.6.1.3.1 InnerModuleEvaluation (#sec-innermoduleevaluation), steps 11.c.v.2 and 16.b.viii.
Kind
failing read (a field of empty).
Model erratum
GatherAvailableAncestorsEmptyCycleRoot, default reading literal; its readings: literal, proposed. The literal reading stops the run at step 1.a; the proposed reading is the step 1.a of Proposed wording.
Upstream
Checked 2026-11-19: issue #3766 (open, 2026-03), "m.[[CycleRoot]] can be null in GatherAvailableAncestors", reports it; PR #3769 (open, 2026-03), "Normative: Do not read from empty [[CycleRoot]] in GatherAvailableAncestors", fixes it (step 1.a tests m.[[EvaluationError]] instead of m.[[CycleRoot]].[[EvaluationError]]) and is not merged.
On this site
GatherAvailableAncestorsEmptyCycleRoot on the Errata page (defect of the text; its default: the literal text)

Text

1. For each Cyclic Module Record ancestorModule of module.[[AsyncParentModules]], do

a. If execList does not contain ancestorModule and ancestorModule.[[CycleRoot]].[[EvaluationError]] is empty, then

Problem

InnerModuleEvaluation (step 11.c.v.2) adds a module to the [[AsyncParentModules]] of an asynchronous dependency before it evaluates the module's later dependencies. When a later dependency throws, InnerModuleEvaluation returns the throw completion before step 16.b.viii sets the module's [[CycleRoot]], so [[CycleRoot]] stays empty. When the asynchronous dependency later fulfils, GatherAvailableAncestors step 1.a reads [[EvaluationError]] of empty. (Evaluate step 9.a has meanwhile set the module's own [[EvaluationError]], which the proposed step 1.a reads instead.)

Examples

examples/02-gather-available-ancestors/ (outputs: the recorded runs): a.mjs imports b.mjs (top-level await) and then c.mjs (throws); main.mjs imports a.mjs, then b.mjs.

// b.mjs
print("b starts"); await 0; print("b ends");
// c.mjs
print("c throws"); throw "c-error";
// main.mjs
import("./a.mjs").then(() => print("a fulfilled"), e => print("a rejected: " + e))
  .then(() => import("./b.mjs")).then(() => print("b fulfilled"), e => print("b rejected: " + e));
output
literal text b starts, c throws, b ends; then b's fulfilment reaches step 1.a, which reads a field of empty
V8, SpiderMonkey b starts, c throws, b ends, a rejected: c-error, b fulfilled
JavaScriptCore b starts, b ends, c throws, a rejected: c-error, b fulfilled
formal model, literal reading (default) stops at step 1.a (stop: erratum GatherAvailableAncestorsEmptyCycleRoot sec-gather-available-ancestors#0 1.a)
formal model, proposed reading b starts, c throws, b ends, a rejected: c-error, b fulfilled

JavaScriptCore evaluates c only after b resumes, a separate deviation.

Proposed wording

Proposed: step 1.a tests the module's own [[EvaluationError]], and reads [[CycleRoot]] only after asserting that it is set:

--- a/spec.html
+++ b/spec.html
@@ -27647,15 +27647,16 @@
               </dl>
               <emu-alg>
                 1. For each Cyclic Module Record _ancestorModule_ of _module_.[[AsyncParentModules]], do
-                  1. If _execList_ does not contain _ancestorModule_ and _ancestorModule_.[[CycleRoot]].[[EvaluationError]] is <span class="const">empty</span>, then
+                  1. If _execList_ does not contain _ancestorModule_ and _ancestorModule_.[[EvaluationError]] is <span class="const">empty</span>, then
                     1. Assert: _ancestorModule_.[[Status]] is <span class="const">evaluating-async</span>.
-                    1. Assert: _ancestorModule_.[[EvaluationError]] is <span class="const">empty</span>.
-                    1. Assert: _ancestorModule_.[[AsyncEvaluationOrder]] is an integer.
-                    1. Assert: _ancestorModule_.[[PendingAsyncDependencies]] > 0.
-                    1. Set _ancestorModule_.[[PendingAsyncDependencies]] to _ancestorModule_.[[PendingAsyncDependencies]] - 1.
-                    1. If _ancestorModule_.[[PendingAsyncDependencies]] = 0, then
-                      1. Append _ancestorModule_ to _execList_.
-                      1. If _ancestorModule_.[[HasTLA]] is *false*, perform GatherAvailableAncestors(_ancestorModule_, _execList_).
+                    1. Assert: _ancestorModule_.[[CycleRoot]] is not <span class="const">empty</span>.
+                    1. If _ancestorModule_.[[CycleRoot]].[[EvaluationError]] is <span class="const">empty</span>, then
+                      1. Assert: _ancestorModule_.[[AsyncEvaluationOrder]] is an integer.
+                      1. Assert: _ancestorModule_.[[PendingAsyncDependencies]] > 0.
+                      1. Set _ancestorModule_.[[PendingAsyncDependencies]] to _ancestorModule_.[[PendingAsyncDependencies]] - 1.
+                      1. If _ancestorModule_.[[PendingAsyncDependencies]] = 0, then
+                        1. Append _ancestorModule_ to _execList_.
+                        1. If _ancestorModule_.[[HasTLA]] is *false*, perform GatherAvailableAncestors(_ancestorModule_, _execList_).
                 1. Return <span class="const">unused</span>.
               </emu-alg>
               <emu-note>

test262

None found: no test in language/module-code/top-level-await/ imports a module with top-level await followed by a module that throws.

The program

The example is a Module graph (its main.mjs runs as a Module). Its runs on the engines and on the model: the examples' runs.

Open in the debugger: The specification Open in the debugger: The specification, with GatherAvailableAncestorsEmptyCycleRoot read as proposed reading

Source examples/02-gather-available-ancestors/a.mjs
import "./b.mjs";
import "./c.mjs";
globalThis.print("a runs");
Source examples/02-gather-available-ancestors/b.mjs
if (typeof print === "undefined") globalThis.print = console.log;
print("b starts");
await 0;
print("b ends");
Source examples/02-gather-available-ancestors/c.mjs
if (typeof print === "undefined") globalThis.print = console.log;
print("c throws");
throw "c-error";
Source examples/02-gather-available-ancestors/main.mjs
// GatherAvailableAncestors 1.a reads a.[[CycleRoot]] of a module a whose
// synchronous evaluation threw (Evaluate 9.a never sets [[CycleRoot]]).
// a.mjs imports b.mjs (top-level await) and then c.mjs (throws).
if (typeof print === "undefined") globalThis.print = console.log;
import("./a.mjs").then(
  function () { print("a fulfilled"); },
  function (e) { print("a rejected: " + e); })
  .then(function () { return import("./b.mjs"); })
  .then(function () { print("b fulfilled"); }, function (e) { print("b rejected: " + e); });