Review: exclude packages from the test shards by Go pattern, not regex (PR #931, head 9041e96dc)
From mnema-talk v3.3.13, rendered on this site.
Reviewer: an agent that did not write the change. Read-only; worked in the PR worktree without changing it, scratch files only outside the repo.
Commit reviewed: 9041e96dc (one commit on top of origin/main).
Task given to the author: fix CodeQL alert js/regex-injection in scripts/test-shard.mjs (new RegExp(exclude) from argv) properly, not by dismissing it.
CODEOWNERS paths touched: .github/workflows/ci.yml. Also changed: scripts/test-shard.mjs, scripts/test-shard.test.mjs.
Verdict
MERGE.
- The regex built from argv is gone. The exclude arguments are now Go package patterns that
go list --resolves, and packages are dropped by exact import path (without, aSetlookup). - rest-a and rest-b select exactly the same packages as on origin/main, byte for byte.
- Every package of the module lands in exactly one of rest-a, rest-b or the server/realtime shards.
- Tests and actionlint pass.
What I checked
1. The alert's cause
scripts/test-shard.mjsno longer hasnew RegExp. The remaining regex uses are fixed literals:testName(line 28/46), the spec parsers at lines 121 and 144. No argv value reaches a regex.- The
runmode still builds a-runregex from test names (runRegex), but those names come fromgo test -listoutput filtered by^(Test|Fuzz|Example), not from argv, and it is passed togo test, not compiled in JS. Unchanged by the PR and not what the alert flagged. goListusesexecFileSyncwith an argument array (no shell) and puts--before the patterns. A pattern with a leading dash is rejected by go itself:pkgs 1/2 -toolexec=evil->malformed import path "-toolexec=evil": leading dash, exit 2.
2. Same packages as origin/main
Old script and test-shard-weights.json saved from origin/main into the scratch dir (weights unchanged by the PR: git diff --quiet origin/main...HEAD -- scripts/test-shard-weights.json).
old pkgs n/2 '/internal/(server|bot)$|/internal/chat(/|$)'vsnew pkgs n/2 ./internal/server ./internal/bot ./internal/chat/...for n = 1, 2:cmpidentical (39 and 40 packages).check-pkgs 2: both print79 packages, one shard each (39 + 40).- Partition:
go list ./...gives 99 packages;go list ./internal/server ./internal/bot ./internal/chat/...gives 20; rest-a + rest-b + those 20 sorted equals the full list, no duplicates (uniq -dempty)../internal/chat/...includesinternal/chatitself, matching the old/internal/chat(/|$).
3. Bash in ci.yml (lines 415-419)
rest=(./internal/server ./internal/bot ./internal/chat/...)is a bash array, expanded as"${rest[@]}", so each pattern is one argument and...is not glob-expanded (it is a literal in an array assignment; no glob characters). Works underset -euo pipefail: the array is non-empty, so-udoes not trip.- The default
runshell on ubuntu is bash, so arrays are available.$pkgsstays a space-separated string, asmake GO_PKGS=expects, same as before. actionlint .github/workflows/ci.yml(with shellcheck if installed): exit 0, no output.
4. Failure behaviour
- Mistyped directory:
pkgs 1/2 ./internal/srever->directory not found, exit 2. With/...:no such file or directory, exit 2. A typo fails the CI step instead of silently excluding nothing (the old regex would have excluded nothing silently). - Regex text as a pattern:
'(a+)+$'->malformed import path, exit 2. - Bad spec
x-> usage, exit 2;3/2-> out of range, exit 2. - No patterns:
check-pkgs 2now succeeds over all 99 packages (old script required the argument). CI always passes patterns, and the effect would be duplicated tests, never untested packages. Acceptable. - A pattern outside the module (
fmt) resolves and excludes nothing, silently. Harmless: it can only make the rest shards larger, never drop a package.
5. Tests and repo rules
node --test scripts/test-shard.test.mjs: 8 tests, 8 pass. The new test checks the observable behaviour (exact match, a shared prefix such asserver/serverutilis not excluded, empty exclusion list) and is not named after coverage.node scripts/check-english-only.mjs: exit 0. Comments and usage text updated to the new argument form; no stale "regex" wording left for the exclude argument (only the-run regexofrunmode, which is still accurate).- The code is small: one exported pure helper and one
go listwrapper. No dead code. - Callers: only ci.yml lines 409-419 call the script;
Makefile:110runs its unit tests;doc/development.md:147describes the split without naming the argument form, so it needs no update.
What I did not check
- I did not run the CI job itself or the integration tests; only the package selection the job feeds to
make coverage-go-profile. - I did not confirm on GitHub that CodeQL closes the alert on this head; the reasoning above is from the source.
- Pre-existing and unchanged: the package list comes from
go list ./...without-tags=integration, so packages that exist only under that tag (internal/testutil/...,internal/mailer/mailtest) are in no shard. They are test helpers; this is the same on origin/main and outside the PR's scope.
Findings
None.