Repository navigation
test(guards): restore the empty-corpus checks the read_dir panics don't cover - #3911
Conversation
…ic doesn't cover `scan_directory` now panics when a directory can't be read, which catches a missing or relocated `src/`. It does not catch the other way discovery comes back empty: every file read, and the `include_str!` / `include_bytes!` / `#[template]` matching in `scan_file` recognizing none of them. The comparison that follows iterates `assets`, so an empty set checks nothing and the test passes. Two failures, two mechanisms. The comment records which one this assert is for, so the next reader doesn't take it for a duplicate of the panic.
worktrunk-bot
left a comment
There was a problem hiding this comment.
Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.
The restored assert is right, and the reasoning in the description holds: scan_file's matching returning nothing is a distinct failure from read_dir erroring, and the for asset in &assets loop below turns it into a green vacuous pass.
#3878 dropped the same guard from snapshot_formatting_guard.rs, and the same argument applies there. It replaced for_each_snapshot's assert!(seen > 500, …) with the read_dir panic in visit_snap_files — but the panic only fires when one of src / tests / docs can't be read. If a layout change relocates the snapshot corpus while those three directories still exist and are readable, the walk succeeds, yields zero .snap files, and all three callers (test_no_host_specific_paths_in_snapshots plus the two at the top of the file) assert absence over an empty corpus and pass. That's precisely the case this PR is restoring for packaged_assets.rs, and the comment left in visit_snap_files — "a walk that reads nothing passes vacuously … Swallowing the error is what makes a directory-layout change look like a clean corpus" — names a failure mode the panic doesn't actually cover. Restoring a corpus-size check in for_each_snapshot alongside this change would keep the two guards consistent; happy to push it if you want it in this PR.
Smaller, same file: scan_file still opens with let Ok(contents) = fs::read_to_string(path) else { return }; — the swallow shape #3878 set out to remove. The new assert backstops the all-files-fail case, but a subset of src/*.rs failing to read still silently drops whatever those files embed while the rest keep assets non-empty. A .rs file that isn't valid UTF-8 is broken anyway, so an expect there seems defensible, but it's your call whether it's worth the line.
One wording note on the new comment: it scopes the assert's case to "every file read, and the … matching in scan_file recognizing none of them", but the assert equally catches the case where the files didn't read. Not wrong, just narrower than what it covers.
I didn't run the suite — the assert is a verbatim restore of code that was green before #3878, so I'd expect it to pass.
…d close scan_file's swallow `for_each_snapshot` walks `src`, `tests` and `docs`, and all three exist. The `read_dir` panic in `visit_snap_files` therefore only fires when one of them can't be read — it says nothing about a corpus that moved out from under them while the roots still read fine. In that case the walk succeeds, yields zero `.snap` files, and all three callers assert absence over nothing and pass. The comment in `visit_snap_files` claimed the panic covered exactly that; it didn't. Counting the corpus and asserting it is non-empty covers it, and the comment now says which of the two failures each mechanism is for. `seen > 0` rather than the `seen > 500` this replaces: the threshold was arbitrary, and a non-empty check is the same shape as the assert in `packaged_assets.rs`. `scan_file` kept the swallow shape too. A subset of `src/*.rs` failing to read silently dropped whatever those files embed while the rest kept `assets` non-empty, so the empty-scan assert wouldn't catch it either. Verified by flipping the walk's roots to a directory that exists and holds no snapshots: the corpus assert fires and the `read_dir` panic does not, which is the distinction the change rests on.
|
All three folded in at The corpus check in
The wording note: right, and closing the Title and description updated to match the widened scope. Still a draft; the maintainer will mark it ready.
|
worktrunk-bot
left a comment
There was a problem hiding this comment.
Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.
All three earlier points are folded in, and the corpus restore is right where it counts: the .github bug-back check in the description is the evidence I'd have asked for, and it does distinguish the two mechanisms. I ran the seven tests across both files — all pass.
Two notes inline: the seen > 0 threshold doesn't reach the most likely shape of the failure it's written for, and the new comment in packaged_assets.rs claims a bit more panic coverage than the walk actually has.
One more, outside the diff — visit_snap_files reads each snapshot with a bare fs::read_to_string(&path).unwrap(). It does panic, so it isn't the swallow shape this PR is closing, but it's the one read in either file whose failure message doesn't name the file: the read_dir panic three lines above it prints {dir} unreadable during the snapshot scan, while this one prints Os { code: 2, … } and leaves you grepping 1,156 files for which one it was. Happy to push the unwrap_or_else if you want it in this PR — it's the same one-liner as the other three.
…alk errors Counting the corpus in aggregate answered the wrong question. `tests` holds 1,088 of today's 1,156 snapshots, so relocating just that root leaves 68 behind and a non-empty check passes while the callers assert absence over 6% of the corpus. A layout change moves one root and leaves the others, which is exactly the case the aggregate check couldn't see. Counting inside the loop and asserting per root has no threshold in it and names the root that came back empty. Each root is load-bearing as a result: one that legitimately stops holding snapshots belongs out of the list rather than exempted from it. Both walks also iterated `entries.flatten()`, which drops a per-entry `io::Error` — a file removed mid-walk, a failing `stat`. The file it would have led to never reaches either panic, and in `packaged_assets.rs` it takes whatever it embeds with it while the rest keep `assets` non-empty, so the restored assert doesn't catch it either. That is the same swallow shape this change set out to close, so both walks now surface it. `visit_snap_files` read each snapshot with a bare `unwrap()` whose message named no file; it names the path now, like the other three. Verified by swapping `tests` — the root holding the large majority — for one that exists and holds no snapshots: the per-root assert fires and names it, where the aggregate check it replaces would have passed.
|
All three folded in at Per-root count: you're right and the aggregate check was answering the wrong question — On
Description updated. Still a draft; the maintainer will mark it ready.
|
worktrunk-bot
left a comment
There was a problem hiding this comment.
Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.
Both points are folded in, and per-root is the right granularity: src / tests / docs are the roots test source lives under and insta anchors every snapshot to its test file, so a snapshot can't drift out of the three without a deliberate relocation — the list is a stopping point with a reason behind it, not an arbitrary one. Today's split is 64 / 1,088 / 4, so tests is the root whose loss the aggregate check would have survived. I ran the seven tests across both files; all pass, and cargo fmt --check is clean on both.
One note inline, in the same family as the bare unwrap() this commit fixed: each new per-entry panic reuses its read_dir panic's message verbatim. "This directory can't be read" and "this directory reads fine but one entry in it can't" now print the identical line naming the identical path, so the message tells you nothing about which of the two you hit — and the second is the one where the obvious next step (open the directory, it's fine) sends you the wrong way. Suggestions on both walks.
…at can't be read The per-entry panics reused their `read_dir` panic's message verbatim, so "this directory can't be read" and "this directory reads fine but one entry in it can't" printed the identical line naming the identical path. The second is the one where the obvious next step — open the directory, find it fine — sends you the wrong way. Each walk's two directory panics now differ; the file reads already differed by naming a file.
|
Both suggestions applied verbatim at That's every item from the three rounds. Current state: 14 guard tests across the four files #3878 touched pass, Still a draft, and the scope has grown well past the single-assert restore it opened as — two guard files, the per-root corpus check, and four read paths. Every step came from review and is the same defect class, but that call belongs to the maintainer along with marking it ready.
|
worktrunk-bot
left a comment
There was a problem hiding this comment.
Ran the seven tests across both files on the merged tree — all pass, cargo fmt --check clean on both. Per-root is the right granularity, and the two directory panics in each walk now say which read failed, so everything from the draft rounds is closed.
One thing outside the diff, since the PR's own frame is the four guard files #3878 touched: the other two still carry both swallow shapes this PR closes.
In output_system_guard.rs, scan_directory and scan_stderr_macros both iterate entries.flatten(), and check_file and check_stderr_macros_in_file both open with let contents = match fs::read_to_string(path) { Ok(c) => c, Err(_) => return };. In version_build.rs, scan_directory iterates entries.flatten() and check_file opens with let Ok(contents) = fs::read_to_string(path) else { return; };.
All three of those guards assert violations.is_empty(), so a file that drops out of the walk takes its own coverage with it and the test still passes green — the same shape as scan_file's swallow one file over, and the reason the read_dir panic #3878 added doesn't backstop it: src/ reads fine, one entry under it doesn't. A println! in a file whose stat fails mid-walk goes unreported by check_no_unexpected_stdout_writes, and a bare env!("VERGEN_…") in a file that fails to read goes unreported by vergen_env_vars_are_read_optionally — which is the guard standing between that and another #3123.
The empty-scan assert doesn't transfer as cleanly there, worth saying: both walk src/, and the crate compiled from src/ to run the test at all, so "reads everything, finds zero .rs files" isn't reachable the way a relocated snapshot corpus is. It's the per-file swallows that carry the same weight in those two.
Happy to push it — four sites, the same unwrap_or_else one-liner this PR already uses. Four hand-rolled walks across the guard suite now carry the same eight lines of panic boilerplate, so a shared walker in tests/common is the other shape it could take; that's a bigger call than the swallows themselves. Here or as a follow-up, whichever you prefer — approving on the two files in the diff either way.
Sites
output_system_guard.rs:149and:320—entries.flatten()output_system_guard.rs:172and:339—Err(_) => returnversion_build.rs:52—entries.flatten()version_build.rs:63—let Ok(contents) = … else { return; }
) 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: <details><summary>Reach checks</summary> **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. </details> > _This was written by Claude Code on behalf of max-sixty_
#3878 changed each source-scan walk's
fs::read_dirfrom a silentreturninto a panic, which is the right call — a directory the walk can't read contributed no violations, so an unreadable or relocatedsrc/read as a clean crate. On the strength of that it also dropped two guards as duplicates of what the panic now gives. Neither is one: the panic catches a directory that can't be read, and both of them catch a walk that read everything and found nothing.packaged_assets.rslostassert!(!assets.is_empty()). The other way discovery comes back empty is every file reading fine and theinclude_str!/include_bytes!/#[template]matching inscan_filerecognizing none of them. Nothing downstream notices, because the comparison againstcargo package --listand theflake.nixfilter iteratesassets— an empty set checks nothing and the test passes green while the guard standing between a bareinclude_str!and another #3123 has stopped guarding.snapshot_formatting_guard.rslostassert!(seen > 500)fromfor_each_snapshot. That walk's roots aresrc,testsanddocs, and all three exist, so theread_dirpanic invisit_snap_filesonly fires when one of them can't be read. If the corpus moves out from under those roots while they still read fine, the walk succeeds, yields zero.snapfiles, and all three callers assert absence over nothing. The comment left invisit_snap_filesclaimed the panic covered exactly that case; it didn't, and now says which of the two failures each mechanism is for.The count is restored per root rather than in aggregate.
testsholds 1,088 of today's 1,156 snapshots, so relocating just that root leaves 68 behind — a layout change moves one root and leaves the others, which is the realistic shape and the one an aggregate non-empty check survives. Counting inside the loop has no threshold in it and names the root that came back empty. That makes each root load-bearing,docsincluded at 4 files: a root that legitimately stops holding snapshots belongs out of the list rather than exempted from it.Two swallows survived in both files.
scan_filestill hadlet Ok(contents) = fs::read_to_string(path) else { return };— a subset ofsrc/*.rsfailing to read silently drops whatever those files embed while the rest keepassetsnon-empty, so the restored assert wouldn't have caught it. And both walks iteratedentries.flatten(), which drops a per-entryio::Error(a file removed mid-walk, a failingstat), so the file it would have led to reached neither panic. Both now surface.visit_snap_filesalso read each snapshot with a bareunwrap()whose message named no file, leaving you to guess which of 1,156 it was; it names the path now, like the others.Each walk's two directory panics say which read failed. "This directory can't be read" and "this directory reads fine but one entry in it can't" would otherwise print the same line naming the same path, and the second is the one where the obvious next step — open the directory, find it fine — sends you back to the start.
Test plan
The 14 tests across the four guard files #3878 touched all pass.
The corpus assert is a tripwire for a state the tree isn't in, so it was exercised directly, twice:
Bug-back checks on the corpus assert
Whole corpus gone — roots flipped from
["src", "tests", "docs"]to[".github"], which exists, reads fine, and holds no snapshots:The
read_dirpanic not firing is the point: it confirms the two mechanisms cover different failures rather than one being a duplicate of the other.One root relocated —
testsalone swapped for.github, leavingsrc's 64 anddocs' 4 in place:68 snapshots still walk here, so an aggregate non-empty check passes and this is the case it can't see.
Both edits were reverted after the runs.
The panics on the read paths aren't exercised. A
.rsfile undersrc/that isn't readable UTF-8 wouldn't compile, so the crate never reaches the test; they're fail-fast for the reachable causes — a permissions change, a file removed mid-walk — rather than live guards.