Commit 20a93ab52b6 for nodejs

commit 20a93ab52b6bb3fdc50bb8f66c84681e36aa86c0
Author: Moshe Atlow <moshe@atlow.co.il>
Date:   Mon Nov 16 11:26:48 2026 +0200

    test_runner: scope rerun ids to the entry file

    With process isolation, the `--test-rerun-failures` state file is
    written by the parent process, which numbered tests sharing a location
    across all test files, while each child process numbers them within
    its own file. A test defined in a helper used by several test files
    was therefore looked up in a slot belonging to another file, and a
    failing test could be replayed as passed without running.

    Prefix the identifier of a test defined outside of its entry file with
    the entry file when tests run in per-file child processes, and build
    the identifier with the same helper on both sides.

    Signed-off-by: Moshe Atlow <moshe@atlow.co.il>
    PR-URL: https://github.com/nodejs/node/pull/67512
    Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
    Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>

diff --git a/doc/api/test.md b/doc/api/test.md
index d3badba70bd..dd007b3590d 100644
--- a/doc/api/test.md
+++ b/doc/api/test.md
@@ -135,6 +135,9 @@ create it.
 the state file is a JSON file that contains an array of run attempts.
 Each run attempt is an object mapping successful tests to the attempt they have passed in.
 The key identifying a test in this map is the test file path, with the line and column where the test is defined.
+When tests run with process isolation and a test is defined outside of the test file being run,
+for example in a helper shared by several test files,
+the key is prefixed with the path of that test file.
 in a case where a test defined in a specific location is run multiple times,
 for example within a function or a loop,
 a counter will be appended to the key, to disambiguate the test runs.
diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js
index 53f6771a091..b143ac28dfd 100644
--- a/lib/internal/test_runner/harness.js
+++ b/lib/internal/test_runner/harness.js
@@ -68,6 +68,7 @@ function createTestTree(rootTestOptions, globalOptions) {
     teardown: null,
     snapshotManager: null,
     previousRuns: null,
+    rerunEntryFile: null,
     isFilteringByName,
     isFilteringByOnly,
     isFilteringByTags,
@@ -219,6 +220,10 @@ function setupFailureStateFile(rootTest, globalOptions) {
   if (!process.env.NODE_TEST_CONTEXT) {
     const reporter = reportReruns(rootTest.harness.previousRuns, globalOptions);
     compose(rootTest.reporter, reporter).pipe(new PassThrough());
+  } else {
+    // The state is written by the parent process, which scopes the tests of a
+    // child process running a single file to that file.
+    rootTest.harness.rerunEntryFile = rootTest.entryFile;
   }
 }

diff --git a/lib/internal/test_runner/reporter/rerun.js b/lib/internal/test_runner/reporter/rerun.js
index ba2a1fe3914..fcb497a04ce 100644
--- a/lib/internal/test_runner/reporter/rerun.js
+++ b/lib/internal/test_runner/reporter/rerun.js
@@ -5,8 +5,8 @@ const {
   ArrayPrototypePush,
   JSONStringify,
 } = primordials;
-const { relative } = require('path');
 const { writeFileSync } = require('fs');
+const { getRerunTestId } = require('internal/test_runner/utils');

 function reportReruns(previousRuns, globalOptions) {
   return async function reporter(source) {
@@ -16,7 +16,7 @@ function reportReruns(previousRuns, globalOptions) {
     const roots = [];

     function getTestId(data) {
-      return `${relative(globalOptions.cwd, data.file)}:${data.line}:${data.column}`;
+      return getRerunTestId(globalOptions.cwd, data, data.entryFile);
     }

     function startTest(data) {
diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js
index b0db6c3bc7e..f4557d23091 100644
--- a/lib/internal/test_runner/test.js
+++ b/lib/internal/test_runner/test.js
@@ -70,6 +70,7 @@ const {
   createDeferredCallback,
   createSeededGenerator,
   countCompletedTest,
+  getRerunTestId,
   isTestFailureError,
   reporterScope,
   testChannel,
@@ -97,7 +98,6 @@ const {
 } = require('timers');
 const { TIMEOUT_MAX } = require('internal/timers');
 const { fileURLToPath } = require('internal/url');
-const { relative } = require('path');
 const { availableParallelism } = require('os');
 const { innerOk } = require('internal/assert/utils');
 const { bigint: hrtime } = process.hrtime;
@@ -837,7 +837,7 @@ class Test extends AsyncResource {
     }

     if (this.loc != null && this.root.harness.previousRuns != null) {
-      const baseIdentifier = `${relative(this.config.cwd, this.loc.file)}:${this.loc.line}:${this.loc.column}`;
+      const baseIdentifier = getRerunTestId(this.config.cwd, this.loc, this.root.harness.rerunEntryFile);
       let testIdentifier = baseIdentifier;
       const disambiguator = this.root.testDisambiguator.get(baseIdentifier);
       if (disambiguator !== undefined) {
diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js
index 4f3728fe092..1624a357b4a 100644
--- a/lib/internal/test_runner/utils.js
+++ b/lib/internal/test_runner/utils.js
@@ -197,6 +197,16 @@ function parsePreviousRuns(rerunFailuresFilePath) {
   return JSONParse(data);
 }

+// A location outside of the entry file is shared by every test file that
+// reaches it, so it is scoped to the entry file when there is one.
+function getRerunTestId(cwd, { file, line, column }, entryFile) {
+  const id = `${relative(cwd, file)}:${line}:${column}`;
+  if (entryFile == null || entryFile === file) {
+    return id;
+  }
+  return `${relative(cwd, entryFile)}::${id}`;
+}
+
 async function getReportersMap(reporters, destinations) {
   return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
     const destination = kBuiltinDestinations.get(destinations[i]) ??
@@ -741,6 +751,7 @@ module.exports = {
   testStorage,
   shouldColorizeTestFiles,
   getCoverageReport,
+  getRerunTestId,
   setupGlobalSetupTeardownFunctions,
   parsePreviousRuns,
   testChannel,
diff --git a/test/fixtures/test-runner/rerun-shared-helper-across-files/a.mjs b/test/fixtures/test-runner/rerun-shared-helper-across-files/a.mjs
new file mode 100644
index 00000000000..2f45f6bb262
--- /dev/null
+++ b/test/fixtures/test-runner/rerun-shared-helper-across-files/a.mjs
@@ -0,0 +1,4 @@
+import { describeEngine } from './helper.mjs';
+
+describeEngine('a-1');
+describeEngine('a-2');
diff --git a/test/fixtures/test-runner/rerun-shared-helper-across-files/b.mjs b/test/fixtures/test-runner/rerun-shared-helper-across-files/b.mjs
new file mode 100644
index 00000000000..13d3dc93658
--- /dev/null
+++ b/test/fixtures/test-runner/rerun-shared-helper-across-files/b.mjs
@@ -0,0 +1,6 @@
+import { describeEngine } from './helper.mjs';
+
+describeEngine('b-1');
+describeEngine('b-2');
+describeEngine('b-3');
+describeEngine('b-4', { fails: true });
diff --git a/test/fixtures/test-runner/rerun-shared-helper-across-files/helper.mjs b/test/fixtures/test-runner/rerun-shared-helper-across-files/helper.mjs
new file mode 100644
index 00000000000..3c4b0d8abd4
--- /dev/null
+++ b/test/fixtures/test-runner/rerun-shared-helper-across-files/helper.mjs
@@ -0,0 +1,13 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert';
+
+// Every test file calling this registers its suites and tests at the same
+// source location, which is outside of the test file itself.
+export function describeEngine(name, { fails = false } = {}) {
+  describe(name, () => {
+    it('works', ({ attempt }) => {
+      assert.ok(!fails, `${name} always fails`);
+      assert.strictEqual(attempt, 0, `${name} already passed and must not run again`);
+    });
+  });
+}
diff --git a/test/parallel/test-runner-test-rerun-failures.js b/test/parallel/test-runner-test-rerun-failures.js
index 585406ec919..cdaef0f1d6d 100644
--- a/test/parallel/test-runner-test-rerun-failures.js
+++ b/test/parallel/test-runner-test-rerun-failures.js
@@ -232,6 +232,32 @@ test('failing test is not swallowed when siblings share its source location', as
   assertAttempt(state, 1);
 });

+{
+  // Both fixtures register all of their suites and tests from the same
+  // location in a shared helper. In each mode, the suite that fails must keep
+  // failing on the next attempt, and the ones that passed must be replayed.
+  const a = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'a.mjs');
+  const b = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'b.mjs');
+  const modes = [
+    { name: 'process isolation', args: ['--test', a, b], pass: 5 },
+    { name: 'no isolation', args: ['--test', '--test-isolation=none', a, b], pass: 5 },
+    { name: 'a file run without --test', args: [b], pass: 3 },
+  ];
+
+  for (const { name, args, pass } of modes) {
+    test(`failing test in a helper shared by test files is not swallowed with ${name}`, async () => {
+      for (let attempt = 0; attempt < 2; attempt++) {
+        const { code, signal, stdout } = await common.spawnPromisified(
+          process.execPath, ['--test-rerun-failures', stateFile, ...args]);
+        assert.strictEqual(signal, null);
+        assert.match(stdout, new RegExp(`^ℹ pass ${pass}$`, 'm'), `attempt ${attempt}`);
+        assert.match(stdout, /^ℹ fail 1$/m, `attempt ${attempt}`);
+        assert.strictEqual(code, 1, `attempt ${attempt}`);
+      }
+    });
+  }
+}
+
 test('rerun preserves the original duration on the replayed pass', async () => {
   const durationFixture = fixtures.path('test-runner', 'rerun-duration.js');
   const args = ['--test-rerun-failures', stateFile, durationFixture];