Skip to content

test(guards): restore the empty-corpus checks the read_dir panics don't cover - #3911

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

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

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

#3878 changed each source-scan walk's fs::read_dir from a silent return into a panic, which is the right call — a directory the walk can't read contributed no violations, so an unreadable or relocated src/ 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.rs lost assert!(!assets.is_empty()). The other way discovery comes back empty is every file reading fine and the include_str! / include_bytes! / #[template] matching in scan_file recognizing none of them. Nothing downstream notices, because the comparison against cargo package --list and the flake.nix filter iterates assets — an empty set checks nothing and the test passes green while the guard standing between a bare include_str! and another #3123 has stopped guarding.

snapshot_formatting_guard.rs lost assert!(seen > 500) from for_each_snapshot. That walk's roots are src, tests and docs, and all three exist, so the read_dir panic in visit_snap_files only 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 .snap files, and all three callers assert absence over nothing. The comment left in visit_snap_files claimed 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. tests holds 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, docs included 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_file still had let Ok(contents) = fs::read_to_string(path) else { return }; — a subset of src/*.rs failing to read silently drops whatever those files embed while the rest keep assets non-empty, so the restored assert wouldn't have caught it. And both walks iterated entries.flatten(), which drops a per-entry io::Error (a file removed mid-walk, a failing stat), so the file it would have led to reached neither panic. Both now surface. visit_snap_files also read each snapshot with a bare unwrap() 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:

exit=101
count-assert fired: True
read_dir panic fired instead: False

The read_dir panic 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 — tests alone swapped for .github, leaving src's 64 and docs' 4 in place:

exit=101
per-root assert fired and named the root: True
.github/ holds no .snap files — the corpus has moved, so these assertions are now passing over whatever is left of it

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 .rs file under src/ 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.

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

…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 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.

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.
@max-sixty max-sixty changed the title test(packaged_assets): restore the empty-scan assert the read_dir panic doesn't cover test(guards): restore the empty-corpus checks the read_dir panics don't cover Aug 25, 2026
@max-sixty

Copy link
Copy Markdown
Owner Author

All three folded in at 7f956f91e — thanks, the snapshot one was the substantive catch.

The corpus check in for_each_snapshot: confirmed and restored. I checked the reasoning rather than taking it on the argument — the roots are src, tests and docs, all three exist, so the read_dir panic can't be what catches a relocated corpus. Then exercised it: flipping the roots to .github (exists, reads fine, holds no snapshots) fires the new assert and does not fire the read_dir panic, which is the distinction the whole change rests on. Restored as seen > 0 rather than the seen > 500 it replaces — the threshold was arbitrary, and non-empty matches the shape of the packaged_assets.rs assert. The comment in visit_snap_files that claimed the panic covered a directory-layout change now says which of the two failures each mechanism is for.

scan_file's read_to_string swallow: closed, on your reasoning — a subset of files failing to read drops their embeds while the rest keep assets non-empty, so the restored assert doesn't backstop it. Worth noting the panic isn't reachable through the non-UTF-8 case you named, since the crate wouldn't compile; it's fail-fast for a permissions change or a file removed mid-walk. The PR body says so rather than implying it's a live guard.

The wording note: right, and closing the scan_file swallow is what makes the narrow framing true — both read failures now panic, so "every file read, and the matching recognizing none of them" is what's left. Rewritten to name both panics.

Title and description updated to match the widened scope. Still a draft; the maintainer will mark it ready.

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

@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.

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.
@max-sixty

Copy link
Copy Markdown
Owner Author

All three folded in at 9334574e8, including the one outside the diff.

Per-root count: you're right and the aggregate check was answering the wrong question — tests holds 1,088 of 1,156, so relocating it leaves 68 and seen > 0 passes over 6% of the corpus. Counting inside the loop now, with the assert naming the root. Exercised the case your argument turns on: swapping tests alone for a directory that exists and holds no snapshots fires it and names the root, where the aggregate version passes. Taking your point on the symmetry argument too — packaged_assets.rs has one root, so non-empty genuinely is the whole question there and it doesn't transfer.

On docs: leaving it in the list. Four files makes it load-bearing, but a root that legitimately stops holding snapshots belongs out of the list rather than carrying an exemption inside it — an exempted root is a root nothing checks, which is the shape this PR exists to remove. The comment says so, so whoever drops the demo fixtures knows the answer is to edit the list.

entries.flatten(): took the resolution rather than the wording fix. It is the same swallow shape, and narrowing the comment would have left the hole while describing it accurately. Closed in both walks, so the comment's claim is now true as written.

visit_snap_files's bare unwrap(): fixed, same one-liner as the other three. Worth the line for the reason you gave — the panic three lines up names the directory and this one printed a bare Os { code: 2, … }.

Description updated. Still a draft; the maintainer will mark it ready.

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

@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.

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.
@max-sixty

Copy link
Copy Markdown
Owner Author

Both suggestions applied verbatim at 7a44ecdbf — the wording you wrote is the right one, and the diagnosis is the part I'd missed: identical text plus identical path means the message can't tell you which of the two you hit, and the per-entry case is the one where checking the directory clears it and sends you back to the start. Each walk's two directory panics now differ; the read_to_string ones stay as they are, distinguished by naming a file, as you noted.

That's every item from the three rounds. Current state: 14 guard tests across the four files #3878 touched pass, cargo fmt --check and clippy --all-targets clean, and required CI was green on 9334574e8 before this commit.

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.

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

@max-sixty
max-sixty marked this pull request as ready for review August 25, 2026 22:02

@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.

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

@max-sixty
max-sixty merged commit 8e40a2b into main Aug 25, 2026
40 checks passed
@max-sixty
max-sixty deleted the fix/packaged-assets-empty-scan branch August 25, 2026 22:11
max-sixty added a commit that referenced this pull request Aug 26, 2026
)

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_
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