Skip to content

test(guards): one walk behind the guards that scan source as text - #3914

Merged
max-sixty merged 5 commits into
mainfrom
fix/packaged-assets-empty-scan
Aug 26, 2026
Merged

max-sixty merged 5 commits into
mainfrom
fix/packaged-assets-empty-scan

Conversation

@max-sixty

@max-sixty max-sixty commented Aug 26, 2026 •

Copy link
Copy Markdown
Owner

Six guards read src/, or the snapshot corpus, as text rather than compiling it — no stray println! outside the allowlist, no eprintln! resolving to std's macro instead of anstream's, no bare env!("VERGEN_…"), no include_str! of a path the package won't ship, no host path in a .snap, and nothing but src/testing/mod.rs spawning wt through CARGO_BIN_EXE_wt. Each asserts absence over what its walk handed it, and that polarity is what makes a swallowed read dangerous here in a way it isn't for code that acts on what it finds: a file that drops out of the walk is indistinguishable from a file with nothing wrong in it, so violations stays empty and the test passes green over less than it claims.

Each carried its own copy of the same recursion — read the directory, iterate entries, recurse into subdirectories, read and check files with a matching extension. The copies drifted, and #3911 is the evidence: closing a swallowed read in one left the identical swallow in the others, and the review that caught it had to list remaining sites by hand. tests/common/source_scan.rs owns the walk now and all six call it. The guards keep what is actually theirs: which extension, what counts as a violation, and their own coverage assertion.

Six swallowed reads go with it. output_system_guard.rs had entries.flatten() in both its walks and Err(_) => return in both its file readers; version_build.rs had entries.flatten() and let Ok(..) else { return }. flatten() is the one that hides, dropping a per-entry io::Error without looking like a return at all — a println! in a file whose stat fails went unreported by check_no_unexpected_stdout_writes, and a bare env!("VERGEN_…") in a file that failed to read went unreported by vergen_env_vars_are_read_optionally, the guard standing between that and another #3123.

visit_files returns how many files it visited, because an absence assertion also passes over an empty walk. Three guards had nothing that would fail in that case and now assert the count. The other three already assert something an empty walk cannot satisfy — a non-empty asset set, a per-root snapshot count, an offender list equal to one named file — so a count beside those would be a second mechanism for one guarantee.

The return is #[must_use], which matters more than it looks. Left advisory, the classification of who needs the count would live only in prose, and a seventh caller would inherit none of it — the same shape as the bug being fixed, a per-caller obligation that four of five copies quietly skipped. As a must_use it is a compile error under -D warnings, and "my proof is elsewhere" becomes a let _ = a reviewer can see and check.

The scan label names the calling guard. It previously claimed to and didn't: three call sites passed the same "src/ scan", so a permissions failure read identically whichever guard was running, and only libtest's thread name told them apart. The file-contents panic was likewise worded exactly like the directory-listing one, separated only by whether the path happened to be a file.

One behaviour change worth naming: output_system_guard's allowlist checks now run against a file the walk has already read, rather than short-circuiting before the read. An allowlisted file that can't be read is a file in the tree that can't be read, and now says so instead of being skipped.

tests/CLAUDE.md records the rule, beside "a test's setup is part of its proof" — the same failure in different dress. Nothing wrote the hazard down, which is why each guard was free to rediscover it.

The rule holds outside the source scans too, so two walks in config_show.rs's test_plugin_layout_is_consolidated now count what they assert absence over. One walks plugins/worktrunk/skills asserting no entry is a symlink — if the mirror never generated, it visits nothing and passes; is_dir() runs first, but an existing empty directory is exactly what a failed mirror leaves. The skills/ loop below it has the same shape for SKILL.md. Neither reads file contents, so visit_files isn't a drop-in and they carry their own counts.

Test plan

cargo fmt --check and clippy --all-targets clean; the 15 tests across the guard files pass.

Green proves least here, since a walker that silently visits nothing passes every one of those tests. So each behaviour was reached for directly:

Reach checks

The walk recurses and reads. A println! injected three levels deep in src/trace/profile.rs, a file on no allowlist:

src/trace/profile.rs:1306: println!("probe");

The walk covers roots outside src/. The CARGO_BIN_EXE_wt needle injected into a file under tests/:

left: ["src/testing/mod.rs", "tests/integration_tests/list_layout.rs"]

The coverage assert fires. A scan pointed at a directory that exists, reads fine, and holds no .rs files:

scanned no files under …/.github

The per-root snapshot count fires on a partial move. tests alone — 1,088 of the 1,156 snapshots — swapped for a directory holding none, leaving src's 64 and docs' 4 in place:

.github/ holds no .snap files — the corpus has moved, so these assertions are now passing over whatever is left of it

An aggregate non-empty check survives that case, which is why the count is per root. Every edit was reverted after its run.

This was written by Claude Code on behalf of max-sixty

Six guards read `src/`, or the snapshot corpus, as text rather than compiling
it — no stray `println!` outside the allowlist, no `eprintln!` resolving to
std's macro instead of anstream's, no bare `env!("VERGEN_…")`, no `include_str!`
of a path the package won't ship, no host path in a `.snap`, and nothing but
`src/testing/mod.rs` spawning `wt` through `CARGO_BIN_EXE_wt`. Each asserts
absence, and each carried its own copy of the same recursion: read the
directory, iterate entries, recurse into subdirectories, read and check files
with a matching extension.

The copies drifted. #3911 closed a swallowed read in one of them and the
identical swallow survived in the rest, which is how a review ended up listing
remaining sites by hand. `tests/common/source_scan.rs` owns the walk now and
all six call it; the guards keep what is theirs — which extension, what counts
as a violation, and their own coverage assertion.

Six swallowed reads go with it. `output_system_guard` had `entries.flatten()`
in both its walks and `Err(_) => return` in both its file readers;
`version_build` had `entries.flatten()` and `let Ok(..) else { return }`.
`flatten()` is the one that hides, dropping a per-entry `io::Error` without
looking like a return: a `println!` in a file whose `stat` fails went
unreported, and so did a bare `env!("VERGEN_…")` in a file that failed to read.

`visit_files` returns how many files it visited, because absence assertions
also pass over an empty walk. Three guards had nothing that would fail in that
case and now assert the count. The other three already assert something an
empty walk cannot satisfy — a non-empty asset set, a per-root snapshot count,
an offender list equal to one named file — so a count beside it would be a
second mechanism for one guarantee.

`output_system_guard`'s allowlist checks now run against a file the walk has
already read rather than short-circuiting before the read. An allowlisted file
that cannot be read is a file in the tree that cannot be read, and says so.

`tests/CLAUDE.md` records the rule, beside "a test's setup is part of its
proof" — the same failure in different dress. Nothing wrote it down, which is
why each guard was free to rediscover it.

Verified by reaching rather than by green, since a walk that visits nothing
passes every one of these tests: a `println!` injected three levels deep in
`src/trace/profile.rs` is reported with its line, the spawn-pin needle injected
under `tests/` is reported, and pointing a scan at a readable directory holding
no `.rs` files fires the new coverage assert.
…over

Two walks in `test_plugin_payloads_are_canonical` assert absence and count
nothing, so an empty tree satisfies both over zero entries. The symlink
invariant walks `plugins/worktrunk/skills` asserting no entry is a symlink; if
the mirror never generated, the walk visits nothing and passes. `is_dir()` runs
first, so the directory has to exist, but an existing empty one is exactly the
case a failed mirror leaves behind. The `skills/` loop below has the same shape
for SKILL.md.

Neither reads file contents, so `visit_files` isn't a drop-in — they carry
their own counts instead.

Same rule as the source-scanning guards, applied where it also holds: an
assertion of absence needs separate evidence that there was something to assert
over.
…the scan

`visit_files` returning a count left the obligation in prose: `tests/CLAUDE.md`
said which guards must assert it and why the others need not, and a seventh
caller would inherit none of that. That is the shape of the bug this replaced —
a per-caller duty four of five copies quietly skipped. `#[must_use]` turns
forgetting into a compile error under `-D warnings`, and turns "my proof is
elsewhere" into a `let _ =` a reader can see and check.

The `scan` label claimed to name the guard and didn't: three call sites passed
the same `"src/ scan"`, so a permissions failure read identically whichever was
running, and only libtest's thread name distinguished them. They now name the
guard. The file-contents panic was also worded exactly like the directory-listing
one, separated only by whether the path happened to be a file; it says so now.

With the recursion inside `visit_files`, the `scan_*` adapters were one-line
single-caller wrappers whose second parameter was provably the same value as
their first at every call site — that shape only made sense while they
recursed. Inlined, which drops the wrapper and the duplicate argument together.

`packaged_assets`' comment still credited panics to `scan_file`, which now takes
`contents` and reads nothing, and the coverage asserts said what happened
without saying what it cost.

Verified `#[must_use]` bites: dropping one `let _ =` fails the build with
"unused return value of `visit_files` that must be used".

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.

Nothing on correctness at 2b69fb4: each guard's panic wording survives the extraction, the #[must_use] + let _ = pairing lands on exactly the two guards whose own assertion already fails over an empty walk (assets non-empty, offenders == vec!["src/testing/mod.rs"]), and for_each_snapshot keeps asserting the count per root. cargo check --test integration is clean with no warnings.

Three small things inline — two in tests/CLAUDE.md, one stray doc line in the new module.

Comment thread tests/CLAUDE.md Resolved
Comment thread tests/CLAUDE.md Resolved
Comment thread tests/common/source_scan.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner Author

All three applied at 29af15f63, plus one correction to the last of them.

The inventory undercount is the one that mattered: the section listed five guards where visit_files has six callers, and the missing one was test_wt_spawns_are_pinned — the caller furthest from the rest, in tests/common/ rather than integration_tests/, and so the likeliest to be re-forked by someone reading that paragraph as the inventory. That's the failure this section exists to prevent, in the section itself. Named now.

On the stray //!: removing it as suggested left the module doc butting straight against use std::fs;, and the repo's convention is a blank line there — src/trace/profile.rs, src/trace/emit.rs and tests/common/mod.rs all have one. rustfmt doesn't manage that position, so nothing would have caught it. Restored in 5844ba094.

Current state at 5844ba094: cargo fmt --check and cargo clippy --all-targets -- --deny warnings clean, 37 tests green across the guard files. Required CI was green on 2b69fb477 before these two doc commits.

This was written by Claude Code on behalf of max-sixty

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Confirmed at 5844ba094 — all three resolved, and the inventory correction is right. Counting from the tree: visit_files has six call sites, and the six clauses now map onto them one-to-one.

The stray-//! suggestion was mine to get wrong: the block should have carried the blank line, since a suggestion applies literally and there's nothing downstream that would notice — rustfmt doesn't manage that position, as you found. Worth me writing suggestions that leave the file in the shape the repo wants rather than just deleting the offending line.

Still in draft, so I'll hold the full pass until it's marked ready.

The six call sites
Guard Call site
stray println! output_system_guard.rs:128
std eprintln! output_system_guard.rs:291
bare env!("VERGEN_…") version_build.rs:33
unshipped include_str! packaged_assets.rs:57
host path in .snap snapshot_formatting_guard.rs:186
CARGO_BIN_EXE_wt spawn pin common/mod.rs:1449

@max-sixty
max-sixty marked this pull request as ready for review August 26, 2026 05:18

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Full pass now that it's out of draft. Correctness holds: the six call sites map one-to-one onto visit_files, each guard keeps its own extension, violation predicate, and coverage claim, and the three that discard the count each have an assertion an empty walk genuinely can't satisfy (!assets.is_empty(), the per-root seen > 0, offenders == ["src/testing/mod.rs"]). #[must_use] on the recursive call is satisfied by the visited +=, and &mut f reborrows cleanly across the three snapshot roots.

One thing in the PR description rather than the code: it says "two walks in config_show.rs's test_plugin_payloads_are_canonical", but no such test exists — the mirrored and skill_dirs counters land in test_plugin_layout_is_consolidated. Worth correcting before merge, since the description becomes the squash message and that name is the only handle a future reader has for finding those two walks.

@max-sixty
max-sixty merged commit e4238e8 into main Aug 26, 2026
40 checks passed
@max-sixty
max-sixty deleted the fix/packaged-assets-empty-scan branch August 26, 2026 05:25
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.

2 participants