scripts(runner): a dirty worktree is refused, and a failing suite is named #2468

Merged
jaylouisw merged 1 commit from fix/runner-honest-failure into main 2026-11-23 21:34:14 +00:00
Owner

What was wrong

Two halves of one problem: scripts/run-tests.js lied about failure.

  1. A dirty worktree made the next run lie. test/canonical-urls.test.js edits README.md and restores it. An interrupted run left M README.md behind, and the run after it reported 6 failures that were that one file - git checkout -- . and the same tree ran 52 passed, 0 failed. Three tool calls went into that, one of them spent believing the wrong suite.
  2. A run could exit 1 naming no suite at all. A local full run printed stdin is not a tty and nothing else, so the failure could not be attributed: no suite to re-run, nothing to fix.

The guard

git status --porcelain before the first suite. Non-empty and no --allow-dirty: print the paths and the two commands that clean them, exit 2 - "I would not start" is not "a suite said so", and reading the second as the first is how the wrong thing gets debugged.

$ node scripts/run-tests.js
Refusing to start: the worktree is dirty (1 path).

   M README.md

  A suite that edits a tracked file and is interrupted leaves the file behind, so the
  next run fails on the leftover rather than on the tree - six failures that were one
  dirty file, every one of them about a commit that was fine. Clean up first, so the
  number after it means something:

    git checkout -- .      # discard what a suite left in a tracked file
    git clean -fd          # ... and remove what a suite created and never removed

  Running mid-edit on purpose? node scripts/run-tests.js --allow-dirty

--allow-dirty proceeds with one line, because a worker is sometimes genuinely mid-edit:

Warning: the worktree is dirty (1 path:  M scripts/run-tests.js) and --allow-dirty was given, so this run goes ahead. If a suite blames a file nobody touched in this change, that is why.

Nothing ran in the refused case - no banner, no suite line, no summary - so a refusal cannot be read as a green run. --list skips the guard (a listing runs nothing), and git being unable to answer (tarball install, container without git) means unknown, not dirty: run as before. CI is unaffected: the test job checks out with git checkout -q -f --detach FETCH_HEAD and git clean -qfdx (ci.yml:847-851), so the tree is clean and the guard costs one git status.

The failure block

Every non-zero suite is named with how it exited and the last 20 lines of its own output, from a real fixture where one suite fails with output and one dies silently:

3 suite(s) in 0.4 s - 4 assertions passed, 2 failed

2 of 3 suite(s) failed - this is what each one said:

  x exit 1 - test/b-bad.test.js
      2 passed, 1 failed - 91 ms
      last 20 lines of its output:
      |   line 22 of a long suite
      ...
      |   not ok - the refusal names the way forward
      | 2 passed, 1 failed

  x exit 7 - test/c-silent.test.js
      no summary line - the exit code is the whole report - 75 ms
      (printed nothing on stdout or stderr)
  • The suite path stays at the end of its line: CI's flake retry reads a failing log with grep -oE 'test/[A-Za-z0-9._/-]+\.test\.js$'. Moving it into the middle of a sentence would have quietly stopped that working, so the format is asserted against that regex.
  • The summary path no longer calls process.exit(), which drops writes still queued on a pipe - a lost summary reads exactly like a run that named nothing. It sets process.exitCode and lets stdout drain.
  • If the run dies before the summary (uncaught throw, unhandled rejection, SIGTERM on Linux), the known failures and the suites that never reported are printed with a synchronous fs.writeSync, and an exit hook says [runner] exited N without printing a summary if even that was missed. (Windows kill is a TerminateProcess and runs no handler, so the signal path is the Linux one CI takes.)
  • A run that dirties the tree now says so at the end of that run (This run left the worktree dirty ... M README.md), so the next run's failure is already explained instead of costing three tool calls.
  • --bail adds N suite(s) never ran (--bail).; --filter, --verbose, --concurrency and --list behave as before.

Evidence

test/runner-honest-failure.test.js (37 assertions, 0 failed). There was no existing runner suite - grep -rln run-tests test/ returned nothing - so this is a new one, and it tests the runner the only honest way: it builds a real repository in a temp directory (a copy of scripts/run-tests.js, four suites whose behaviour is known, a committed README.md), runs the copy with real child processes, and asserts on what came back. The copy runs against the fixture because the runner derives its root from path.resolve(__dirname, '..') - no --root flag, no environment hook to keep in sync.

It proves the three things asked for, on behaviour rather than on source:

  • (a) a dirty tree is refused and names a path - exit 2 (not 1), Refusing to start, test/alpha-pass.test.js named, git checkout -- . printed, --allow-dirty advertised, and no banner/summary in the output, which is what proves no suite ran;
  • (b) --allow-dirty proceeds - exit 0, one Warning: line naming the flag and the path, and the run finishes green;
  • (c) a failing suite is named with its exit code and output tail - exit 1 - test/bravo-fail.test.js and exit 3 - test/charlie-silent.test.js both present, the fixture's own BRAVO-SENTINEL line present (so the tail was really captured), printed nothing on stdout or stderr for the silent one, the passing suites not blamed, and the CI grep shape asserted.

Plus: a run that leaves a tracked file modified is named at the end of that run.

Mutation check - the same suite against the pre-change runner from main:

21 passed, 16 failed

Gates: node scripts/syntax-check.js (546 files, all OK), node scripts/check-node-surface.js (no API newer than Node 18), and node scripts/run-tests.js --filter=runner|canonical|ci-disk|doc all green (58/58 canonical, 6 doc suites, 135 assertions).

Deliberately left out

  • test/canonical-urls.test.js is untouched: the guard is the fix for its leftovers, not a rewrite of how it edits README.md.
  • No CHANGELOG.md entry and no docs/ edit - both are hot spots for the parallel worktrees this was written beside; say the word and I will add the line.
## What was wrong Two halves of one problem: `scripts/run-tests.js` lied about failure. 1. **A dirty worktree made the next run lie.** `test/canonical-urls.test.js` edits `README.md` and restores it. An interrupted run left `M README.md` behind, and the run after it reported 6 failures that were that one file - `git checkout -- .` and the same tree ran 52 passed, 0 failed. Three tool calls went into that, one of them spent believing the wrong suite. 2. **A run could exit 1 naming no suite at all.** A local full run printed `stdin is not a tty` and nothing else, so the failure could not be attributed: no suite to re-run, nothing to fix. ## The guard `git status --porcelain` before the first suite. Non-empty and no `--allow-dirty`: print the paths and the two commands that clean them, exit **2** - "I would not start" is not "a suite said so", and reading the second as the first is how the wrong thing gets debugged. ``` $ node scripts/run-tests.js Refusing to start: the worktree is dirty (1 path). M README.md A suite that edits a tracked file and is interrupted leaves the file behind, so the next run fails on the leftover rather than on the tree - six failures that were one dirty file, every one of them about a commit that was fine. Clean up first, so the number after it means something: git checkout -- . # discard what a suite left in a tracked file git clean -fd # ... and remove what a suite created and never removed Running mid-edit on purpose? node scripts/run-tests.js --allow-dirty ``` `--allow-dirty` proceeds with one line, because a worker is sometimes genuinely mid-edit: ``` Warning: the worktree is dirty (1 path: M scripts/run-tests.js) and --allow-dirty was given, so this run goes ahead. If a suite blames a file nobody touched in this change, that is why. ``` Nothing ran in the refused case - no banner, no suite line, no summary - so a refusal cannot be read as a green run. `--list` skips the guard (a listing runs nothing), and git being unable to answer (tarball install, container without git) means *unknown*, not dirty: run as before. **CI is unaffected**: the test job checks out with `git checkout -q -f --detach FETCH_HEAD` and `git clean -qfdx` (`ci.yml:847-851`), so the tree is clean and the guard costs one `git status`. ## The failure block Every non-zero suite is named with how it exited and the last 20 lines of its own output, from a real fixture where one suite fails with output and one dies silently: ``` 3 suite(s) in 0.4 s - 4 assertions passed, 2 failed 2 of 3 suite(s) failed - this is what each one said: x exit 1 - test/b-bad.test.js 2 passed, 1 failed - 91 ms last 20 lines of its output: | line 22 of a long suite ... | not ok - the refusal names the way forward | 2 passed, 1 failed x exit 7 - test/c-silent.test.js no summary line - the exit code is the whole report - 75 ms (printed nothing on stdout or stderr) ``` * The suite path stays at the **end** of its line: CI's flake retry reads a failing log with `grep -oE 'test/[A-Za-z0-9._/-]+\.test\.js$'`. Moving it into the middle of a sentence would have quietly stopped that working, so the format is asserted against that regex. * The summary path no longer calls `process.exit()`, which drops writes still queued on a pipe - a lost summary reads exactly like a run that named nothing. It sets `process.exitCode` and lets stdout drain. * If the run dies before the summary (uncaught throw, unhandled rejection, SIGTERM on Linux), the known failures and the suites that never reported are printed with a synchronous `fs.writeSync`, and an `exit` hook says `[runner] exited N without printing a summary` if even that was missed. (Windows `kill` is a TerminateProcess and runs no handler, so the signal path is the Linux one CI takes.) * A run that dirties the tree now says so at the end of *that* run (`This run left the worktree dirty ... M README.md`), so the next run's failure is already explained instead of costing three tool calls. * `--bail` adds `N suite(s) never ran (--bail).`; `--filter`, `--verbose`, `--concurrency` and `--list` behave as before. ## Evidence `test/runner-honest-failure.test.js` (37 assertions, 0 failed). There was no existing runner suite - `grep -rln run-tests test/` returned nothing - so this is a new one, and it tests the runner the only honest way: it builds a real repository in a temp directory (a copy of `scripts/run-tests.js`, four suites whose behaviour is known, a committed `README.md`), runs the copy with real child processes, and asserts on what came back. The copy runs against the fixture because the runner derives its root from `path.resolve(__dirname, '..')` - no `--root` flag, no environment hook to keep in sync. It proves the three things asked for, on behaviour rather than on source: * **(a) a dirty tree is refused and names a path** - exit 2 (not 1), `Refusing to start`, `test/alpha-pass.test.js` named, `git checkout -- .` printed, `--allow-dirty` advertised, and no banner/summary in the output, which is what proves no suite ran; * **(b) `--allow-dirty` proceeds** - exit 0, one `Warning:` line naming the flag and the path, and the run finishes green; * **(c) a failing suite is named with its exit code and output tail** - `exit 1 - test/bravo-fail.test.js` and `exit 3 - test/charlie-silent.test.js` both present, the fixture's own `BRAVO-SENTINEL` line present (so the tail was really captured), `printed nothing on stdout or stderr` for the silent one, the passing suites not blamed, and the CI `grep` shape asserted. Plus: a run that leaves a tracked file modified is named at the end of that run. Mutation check - the same suite against the pre-change runner from `main`: ``` 21 passed, 16 failed ``` Gates: `node scripts/syntax-check.js` (546 files, all OK), `node scripts/check-node-surface.js` (no API newer than Node 18), and `node scripts/run-tests.js --filter=runner|canonical|ci-disk|doc` all green (58/58 canonical, 6 doc suites, 135 assertions). ## Deliberately left out * `test/canonical-urls.test.js` is untouched: the guard is the fix for its leftovers, not a rewrite of how it edits `README.md`. * No `CHANGELOG.md` entry and no `docs/` edit - both are hot spots for the parallel worktrees this was written beside; say the word and I will add the line.
scripts(runner): a dirty worktree is refused, and a failing suite is named
All checks were successful
CI / Server smoke and Docker build (pull_request) Successful in 36s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 6s
CI / Test (Node 20) (pull_request) Successful in 1m9s
CI / Test (Node 22) (pull_request) Successful in 1m9s
CI / Test (Node 18) (pull_request) Successful in 1m10s
CI / Arm auto-merge (pull_request) Successful in 3s
2198e3f683
Two ways `scripts/run-tests.js` blamed the wrong thing, both of which cost real
time before they were written down:

A suite that edits a tracked file and is interrupted leaves it modified, and the
run after it reports failures that belong to the leftover. `canonical-urls` edits
README.md and restores it; one interrupted run left `M README.md` behind and the
next reported 6 failures - `git checkout -- .` turned them into 52 passed, 0
failed. The runner now refuses to start on a dirty worktree: it prints the paths
and the two commands that clean them, and exits 2, because "I would not start" is
not "a suite said so". `--allow-dirty` proceeds with one warning line, since a
worker is sometimes mid-edit. CI checks out with `git checkout -f` and `git clean
-fdx`, so the guard is a no-op there.

A run could also exit 1 having named no suite - one local run printed `stdin is
not a tty` and nothing else - so there was nothing to re-run and nothing to fix.
Every non-zero suite is now named in the summary with how it exited and the last
20 lines of its own output; a suite that printed nothing says so; `--bail` says
how many never ran. The summary path no longer calls `process.exit()`, which drops
writes still queued on a pipe, and an exit hook, an uncaught-throw handler and a
signal handler print what is known if the run dies before the summary. A red run
is readable without re-running it.

The failing-suite line keeps the suite path at the end of the line, because CI's
flake retry reads it with `grep -oE 'test/[A-Za-z0-9._/-]+\.test\.js$'`.

Tested end-to-end in `test/runner-honest-failure.test.js`, which builds a real
fixture repository in a temp directory (a copy of the runner plus four suites:
pass, fail loudly, fail silently, and one that edits a tracked file), runs it with
real child processes, and asserts on what came back: a dirty tree is refused and
names the path, `--allow-dirty` runs and warns, and a failing suite is named with
its exit code and its own output in the log. Reverted against the old runner, 16
of its 37 assertions fail.
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-23 21:33:06 +00:00
forgejo-actions canceled auto merging this pull request when all checks succeed 2026-11-23 21:33:54 +00:00
Sign in to join this conversation.
No description provided.