Skip to content

test(scripts): the comment said column zero, the regex says any indentation - #1173

Closed
lilyshen0722 wants to merge 2 commits into
mainfrom
test/self-executes-comment-matches-regex
Closed

lilyshen0722 wants to merge 2 commits into
mainfrom
test/self-executes-comment-matches-regex

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

@sprint-review (57328) ran the pattern instead of reading it. The comment claimed "at column zero, i.e. at module scope"; the regex is ^\s*, so an indented main() inside a function body matches. Reproduced:

column-zero main()       -> true
indented main() in body  -> true
inside require.main gate -> true
no call at all           -> false

Fixing the comment, not the regex

The over-match is the safe direction for a guard whose job is to be suspicious of scripts that might self-execute on import. And a call indented inside if (require.main === module) is exactly what a correctly guarded script looks like — which the sibling test proves separately, by requiring the sentinel.

Narrowing this pattern to column zero would make it miss a script that self-executes from inside any wrapper, which is the shape it exists to catch. So the code is right and the prose was wrong.

Why it's worth a PR

Their framing: the comment is what the next editor trusts. A doc-vs-code mismatch inside a guard reads as licence to "fix" the code toward the doc — and here that fix would silently narrow the detector. Same class as checklist rule 15, with the twist that the stale sentence is in the guard rather than in the thing guarded.

4 tests pass.

🤖 Generated with Claude Code

lilyshen0722 and others added 2 commits August 22, 2026 23:45
…tation

@sprint-review (57328) ran it rather than reading it: the doc claimed "at
column zero, i.e. at module scope" while the pattern is `^\s*`, so an indented
`main()` inside a function body matches. Reproduced:

    column-zero main()       -> true
    indented main() in body  -> true
    inside require.main gate -> true
    no call at all           -> false

Fixing the COMMENT, not the regex. The over-match is the safe direction for a
guard whose job is to be suspicious of scripts that might self-execute on
import, and a call indented inside `if (require.main === module)` is what a
correctly guarded script looks like — which the sibling test proves separately
by requiring the sentinel. Narrowing to column zero would make this miss a
script that self-executes from inside any wrapper, which is the shape it
exists to catch.

Their framing is why it matters at all: the comment is what the next editor
trusts, so a doc-vs-code mismatch in a guard reads as licence to "fix" the
code toward the doc.

4 tests pass.

Co-Authored-By: Claude Opus 5 <[email protected]>
…ide it

@sprint-review (57329), second note on the same guard. `readdirSync` is not
recursive and the importer pattern captures `[\w.-]+`, which stops at a slash,
so `scripts/sub/foo` is invisible to BOTH halves — neither scanned for
self-execution nor recognised as imported.

Inert today: backend/scripts is flat, verified (no subdirectories).

Recorded rather than fixed, and the reason is the interesting part: both
controls still pass, because each finds the flat things it was built to find.
A green run would report full coverage of a directory it had not fully read.
That is the failure this suite exists to warn about, sitting in the suite.

If a subdirectory ever appears, make the walk recursive and widen the capture
in the same change — the controls will not notice.

Co-Authored-By: Claude Opus 5 <[email protected]>

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PASS at 42b17a0fa8ceb11c209124f94c7374953cec34cb — CLEAN, green, base main, no prior review.

You made the right call on 57328, and I checked it by running both patterns rather than agreeing with the reasoning:

                                    this PR (unchanged regex)   #1140 (tightened)
bare main() at col 0                          YES                    YES
if (require.main === module) { main() }       no                     no
if (!process.env.DRY_RUN) { main() }          YES                    no
try { main() } catch {}                       YES                    no
function f() { main() }                       YES                    no

Rows 3 and 4 are unguarded scripts that execute on import — the exact population this guard exists to find. Keeping ^\s* keeps them. The one false positive it costs (row 5) is the safe error, and your reason for accepting it is the correct one: the !GUARDED filter already excludes the indented shape that is legitimately fine, so the over-match only ever costs a reader one look at a file that turns out clean.

Fixing the comment rather than the code is the right resolution of a comment/code disagreement whenever the code is the better specification, and saying why in the docstring is what stops the next reader from "fixing" it back.

Heads-up you cannot see from here: this conflicts with #1140. Same file, and both are CLEAN against main, which says nothing about each other:

git merge-tree --write-tree 42b17a0fa 56660eea0   → exit 1, 3-stage conflict
git merge-tree --write-tree origin/main 42b17a0fa → exit 0

They also disagree substantively — #1140 takes the opposite decision on SELF_EXECUTES, and I've blocked it there with the table above.

Where #1140 is better than this PR: the recursion gap. You document it and leave it; #1140 fixes it — a recursive scriptFiles, a keyOf that produces sub/foo, [\w./-]+ so the importer capture crosses the slash, and a __probe_sub control that asserts its own cleanup. Your docstring's own argument is why that matters more than it looks: "both controls below still pass", so a green run would report full coverage of a directory it had not fully read. That is a guard that will lie on the day it first matters, and "inert today" is doing a lot of work — backend/scripts being flat is not an invariant anyone is holding.

I'm not blocking on it. Documenting a known gap precisely is a real contribution and strictly better than silence. But if you and #1140's author are reconciling anyway, the merged shape I'd want is your SELF_EXECUTES decision plus their recursion fix — which is also the shape where the two PRs stop touching the same lines.

Merge as-is if you'd rather land the comment fix now and take recursion separately.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Closing in favour of #1140, which edits the same lines and also fixes the recursion and import gap. Only one of the two can land. #1140 gets refreshed onto current main as a board row. Triage 2026-10-23 checked this against main's code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant