fix(runner): distinguish worker fixtures by import URL (ENG-1553)

MI
15h ago
+1097-88

What

On Node.js, worker fixtures distinguish user-written ES module queries and
fragments. When the runner reloads the same import for a retry or hook
registration, the worker reuses that fixture value. Reuse holds when another
resolver appends a fragment or query parameter, rewrites or percent-encodes the
query, appends a literal ? query onto the URL's query, or short-circuits a
test entry started by importFreshModule. Fresh config graphs (0.18.0
through 0.25.0) and registrar collections (0.25.0) started by an older copy
of e2e in the same process stay fresh when that entry reaches this loader's
resolve hook with the accepted parameter shape.

Why

Worker fixture identity previously keyed on file path alone, which merged
imports such as ./db.ts?a and ./db.ts?b into one instance. Keying on raw
import.meta.url instead would break retries, because each fresh runner load
appends a new ?e2e=<key> query parameter and a second setup of an exclusive
port or file lock fails. Reading any e2e or e2e-graph query parameter from
the URL as loader metadata also mistook user-written parameters under those
names for the runner's own.

How it works

flowchart LR
  A[importFreshModule] --> S[keys + graphs sets]
  B[legacyEntry in resolve] --> S
  B --> C[legacyCollectorLoads WeakMap]
  U[module URL] --> W[writtenIn]
  S --> W
  W -->|key + graph| R[resolve + load hooks]
  C -->|active 0.25.0 collection| R
  W -->|original URL| F[fixtureIdentities]
  1. Shared parameter sets. FreshnessState on freshnessSlot
    (packages/e2e/src/config/esm-hooks.ts:87, :111) shares atWork, the
    load sequence, and decoded keys and graphs sets across copies of e2e
    in one process. importFresh (packages/e2e/src/config/load.ts:137)
    prefixes new keys as fresh-<cacheKey>-<n>, and importFreshModule
    (packages/e2e/src/config/esm-hooks.ts:205) records key and graph
    before importing the entry.
  2. One URL reader. loaderParameter and writtenIn
    (packages/e2e/src/config/esm-hooks.ts:434, :452) inspect the query
    before #, preserve any leading ? inside a parameter name, match decoded
    e2e and e2e-graph values against keys and graphs (including
    percent-encoded names or values and a naive resolver appending ?x=1
    directly onto a loader parameter), and rebuild original from the remaining
    raw parameters, their literal ? or & separators, and the fragment.
    resolve (packages/e2e/src/config/esm-hooks.ts:483), load
    (packages/e2e/src/config/esm-hooks.ts:501), and originalModuleURLSlot
    (packages/e2e/src/config/esm-hooks.ts:568) all read writtenIn.
    appendQuery (packages/e2e/src/config/esm-hooks.ts:177) appends loader
    parameters without re-encoding user query text or fragments.
  3. Older copies in the same process. Published versions 0.18.0 through
    0.25.0 write ?e2e=<key> and optional &e2e-graph=<name> on the entry
    imported from dist/config/load.js, and hold any active collector on
    e2e.activeCollector.v1. isConfigLoader and legacyEntry
    (packages/e2e/src/config/esm-hooks.ts:367, :402) resolve the importer
    path with fileURLToPath (ignoring any query or fragment added to
    config/load) and read the written specifier query even if an earlier
    resolver modifies resolution.url. While collection is active,
    legacyEntry binds the key to the collector in legacyCollectorLoads
    (WeakMap, packages/e2e/src/config/esm-hooks.ts:121, :123, :171). On
    0.25.0, which reports registrar modules via e2e.registrar-module.v1,
    transitive plain helpers re-evaluate registrars during active collection and
    stop inheriting that load once collection ends.
  4. Fixture identity and older-first fallback. fixtureIdentities
    (packages/e2e/src/collect/fixture-identity.ts:25) keys worker fixtures by
    originalModuleURL(location) ?? location when the slot is present, and
    falls back to moduleFile(location)
    (packages/e2e/src/collect/fixture-identity.ts:32) when an older copy
    registered the module loader first and installed no lookup.
  5. Documentation and release note. docs/fixtures.mdx,
    docs/reference/test.mdx, skills/e2e/references/writing-tests.md,
    plugins/e2e/skills/e2e/references/writing-tests.md, and
    .changeset/silent-fixture-urls.md describe synchronous registration on
    Node.js, ES module URL query and fragment identity, the file-path fallback
    when an older version owns the loader, and Bun test-file reload behavior.

Tradeoffs and limits

  • If user code writes an import query whose decoded e2e or e2e-graph
    parameter equals a live key in keys or graph in graphs (or starts
    with one followed by a literal ?, such as ?e2e-graph=<live>?x=1),
    writtenIn reads that prefix as the loader's, matching how main treats
    every e2e and e2e-graph parameter. The same user-written URL therefore
    reads differently in one process once that load key or graph name becomes
    live.
  • inGraph (packages/e2e/src/config/esm-hooks.ts:315) skips the fresh-graph
    tag when a resolved URL already has any e2e-graph parameter, and
    inFreshLoad (packages/e2e/src/config/esm-hooks.ts:162) skips the load key
    when it already has any e2e parameter. A config dependency imported with a
    user-written ?e2e-graph=app stays cached across fresh graphs, as on main.
  • A resolver that mutates a URL's fragment text differently after the loader
    adds ?e2e=<key> (for example appending ?x=1 versus &x=1 after #frag),
    or that concatenates an unescaped ?x=1 onto the query and then re-encodes
    it into %3Fx%3D1 via searchParams.sort(), produces distinct module
    identities across reloads.
  • keys and graphs retain one short string per fresh load key and graph name
    created in the process, and legacyConfigParents caches one boolean per
    distinct config/load file path; legacyCollectorLoads is a WeakMap and
    retains nothing after a collection finishes. Failed or symlinked imports add
    no per-module entry.
  • Recognizing an older copy's entry requires its import from dist/config/load.js
    or src/config/load.ts in a package named e2e to reach this loader's
    resolve hook with the accepted parameter shape (?e2e=<key> and optional
    &e2e-graph=<name>, nothing else). A bundled or relocated older copy, or a
    resolver registered after this loader that short-circuits the entry or adds
    extra query parameters to specifier before resolve runs, is not matched.
  • When an older copy registered the module loader first, its resolve hook
    owns the process: worker fixtures use file-path identity, and two-install
    runner.list() calls reaching a shared registrar through plain helpers keep
    0.25.0's own two-install behavior (a, then b, then a and b) because
    0.25.0 stores atWork in module-private AsyncLocalStorage. Releases
    0.18.0 through 0.24.0 predate e2e.registrar-module.v1 and do not mark
    imported helpers as registrars.

Issue

ENG-1553

Verified

Ran it locally: yes

Head d14129e7d adds one test-only commit on top of c77e61689 (passing
'junction' to symlinkSync in open-host-loader.test.ts); that file passed
11 of 11 tests on Node.js v22.23.5 and v26.13.1, and pnpm check passed.
The probes, mutation run, and bench-ab table below were recorded on
c77e61689.

Ran the built CLI and two-install runner.js / config/load.js probes on
Node.js v22.23.5 and v26.13.1, pairing this head against a physical
e2e@0.25.0 / main (8bad3fc5b) install:

Casemain (8bad3fc5b / 0.25.0)This PR (c77e61689)
Distinct user queries and fragments (./support.ts?a=1#one vs #two, ?a=a%20b vs ?a=a+b, ??e2e-graph=...)Shared one worker fixture by file pathSeparate worker fixtures
Forwarded import (./forward.mjs?e2e=blue -> ./support.mjs?variant=base) beside ./support.mjs?variant=base&e2e=blueForwarded query treated as loader keySeparate worker fixtures matching Node.js module identity
Retry on entry or queried helper (./support.mjs?variant=blue) when a later resolver appends &x=1, appends literal ?x=1 before #, percent-encodes %652e, or short-circuits retry.e2e.tsOne setup, retry passedOne setup, retry passed on Node.js 22 and 26
Newer loader first, 0.25.0 runner.list() called 3 times on direct registrars, plus helper-wrapped registrarsDirect: a,b each call; helper-wrapped: a, then b, then a,bDirect and helper-wrapped: a,b on all 3 calls
Newer loader first, dynamic import() of a registrar from a plain helper after 0.25.0 runner.list() returnsReturned cached moduleReturned cached module (no COLLECTION_ERROR)
Newer loader first, older openHost() / loadConfigModule({ graph }) across edits, including #instrumented on config/load.js, &x=1 from an earlier resolver, and + rewritten to %20Reloaded edited target each timeReloaded edited target each time
Older loader registered first (originalModuleURLSlot absent) with a fixed-port worker fixture on retryOne setup and one teardownOne setup and one teardown
$ node outside.mjs <head> <0.25.0-build> newer-first
{"listed":["test A","test B"],"result":""}

$ node p2-retry.mjs <head> cand literal '?variant=blue' out.json
{"node":"v26.13.1","exit":0,"status":"flaky","setups":1,"events":["setup ?variant=blue?x=1","teardown ?variant=blue?x=1"]}

$ node graph.mjs <head> <0.25.0-build> before append host
["first","second","third","fourth"]

Mutation testing in an isolated detached worktree (mutant.sh --vitest across
tests/unit/esm-hooks.test.ts, tests/integration/open-host-loader.test.ts,
and tests/integration/worker-fixtures.test.ts): 25 mutants, 25 killed,
0 survived, 0 unconfirmed, 0 did not build, with all 30 added tests killed by
at least one mutant (new-test-kills.mjs exit 0).

No rendered docs screenshot is attached: the docs edits in docs/fixtures.mdx
and docs/reference/test.mdx are prose bullets covering synchronous Node.js
registration (no top-level await), ES module URL query and fragment identity,
the older-loader file-path fallback, and Bun test-file reloads, validated with
pnpm docs:check, and no local Mintlify preview server is configured in this
CLI session. Not measured locally: long-session heap retention and Bun runtime
execution.

bench-ab: 8bad3fc5 (merge base with 8bad3fc5b56e9a9d6e8f273ad2205eb5b5b8db0b) vs c77e6168

apps/testbed: e2e run --workers 1 tests/forms.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 6.2.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)1.73 s (1.71 s-1.74 s)1.71 s (1.71 s-1.72 s)-1.2% (-1.5% to +0.2%)same
suite (sum of attempts)831 ms (824 ms-837 ms)829 ms (826 ms-835 ms)-0.7% (-1.5% to +0.8%)same, under the floor
action (events)363 ms (362 ms-363 ms)364 ms (362 ms-364 ms)+0.3% (-0.4% to +1.0%)same, under the floor
step app.open188 ms (186 ms-191 ms)187 ms (186 ms-188 ms)-0.8% (-3.6% to +1.9%)same, under the floor
step locator.fill157 ms (156 ms-157 ms)157 ms (156 ms-158 ms)+0.3% (-0.6% to +1.6%)same, under the floor
step locator.tap90 ms (88 ms-90 ms)90 ms (90 ms-91 ms)+0.6% (0.0% to +4.4%)same, under the floor
step locator.check44 ms (42 ms-45 ms)43 ms (42 ms-44 ms)-1.1% (-4.3% to +5.0%)same, under the floor
step locator.pressSequentially41 ms (41 ms-42 ms)42 ms (42 ms-42 ms)+2.4% (0.0% to +2.4%)same, under the floor
step locator.uncheck32 ms (32 ms-33 ms)32 ms (32 ms-33 ms)0.0% (-3.0% to +3.1%)same
step expect.toBeVisible18 ms (18 ms-18 ms)17 ms (17 ms-18 ms)-5.6% (-5.6% to -2.7%)same, under the floor
step locator.selectOption17 ms (17 ms-18 ms)16 ms (16 ms-17 ms)-5.9% (-13.1% to 0.0%)same, under the floor
step locator.clear17 ms (16 ms-17 ms)17 ms (17 ms-17 ms)+2.9% (-2.8% to +9.7%)same, under the floor
step expect.toHaveValue7 ms (6 ms-9 ms)7 ms (6 ms-7 ms)-5.7% (-18.4% to +18.3%)same, under the floor
step expect.toHaveCount5 ms (5 ms-6 ms)6 ms (5 ms-6 ms)+9.5% (-8.7% to +34.2%)same, under the floor
step expect.toHaveText4 ms (3 ms-4 ms)4 ms (4 ms-5 ms)+29.1% (-13.4% to +41.4%)same, under the floor
step expect.toBeChecked2 ms (2 ms-2 ms)2 ms (2 ms-2 ms)0.0% (-18.4% to 0.0%)same
step expect.not.toBeChecked2 ms (1 ms-2 ms)2 ms (2 ms-2 ms)0.0% (-29.3% to +100.0%)same

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 640

With 640 tests compared at 95%, about 32.0 could land outside by chance.

Summary by CodeRabbit

  • Bug Fixes

    • Improved worker-fixture identity across module reloads and imports with different URL queries or fragments, so fixtures are separated or reused consistently during retries.
    • Improved fresh module graph and configuration reload behavior across supported loader scenarios.
  • Documentation

    • Clarified worker-fixture behavior in Node.js and Bun, including how top-level await and test-file reloads affect fixture setup.
    • Added guidance on declaring shared worker fixtures, handling asynchronous setup, and how URL differences can affect fixture instances.