Repository navigation
dev: skip the pre-push cargo steps that already passed on the same tree - #924
Conversation
The three cargo pre-push hooks (clippy, the e2e harness clippy and cargo test) ran over the whole workspace whatever the push contained, so a one-file change in a leaf crate cost a full workspace build and test run. They now run through `cargo xtask pre-push-check`, which takes the range prek passes to a pre-push hook and applies the selection CI already uses (`cargo xtask affected`): the changed crates plus every crate that depends on them, and the whole workspace for the lockfile, the root manifest, the toolchain file, workflow files, a doc an xtask test reads or a path no crate owns. The directory-based selection cannot see a crate's tests reading a file from outside the crate, so the hook widens it: any Rust file also selects xtask (its contract tests scan every source file), README.md, docs/ and skills/ also select rocm and xtask, and the e2e PowerShell fixture also selects rocm-core, which includes it. A unit test scans the workspace for include_str!, include_bytes! and manifest-relative reads that leave their crate and fails when one is not covered, so a new cross-crate read cannot be missed silently. Whenever the affected crates cannot be determined (the base is not fetched, `cargo metadata` fails) a step runs over the whole workspace and prints why. A step is skipped only when the selection is known and has nothing for it to check, and it says so. The e2e harness clippy runs when e2e-cucumber is affected, and the test step never includes it, as before. `--dry-run` prints the scope and the cargo command without compiling. The step's range, `--base` override, `--dry-run` and cargo's exit status are tested through an injectable cargo runner. `affected` now lists both paths of a moved file (`--no-renames`), so a file moved out of one crate selects the crate that lost it as well as the one that gained it. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…ross-crate read guard The drift guard that keeps the pre-push selection in step with crate tests reading files outside their crate only recognised plain `"..."` literals, so `include_str!(r#"../../../README.md"#)` passed it even though a README edit would not select the reader crate. It now reads raw literals too. It also walked every directory, including nested checkouts such as `.claude/worktrees/...`, so a rocm-core fixture include in another branch's sources was reported as an uncovered read of this tree and could fail `cargo test -p xtask`. Like the other xtask source scans, it now skips every dot-directory. Each change has a test that fails when it is reverted. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…ata file
The test added for raw-string includes spelled out `include_str!("../../../README.md")` in its source, which the ASCII output contract counts as a data file compiled into a binary, so `every_embedded_data_file_is_ascii` failed on the README's non-ASCII lines in the Linux, Windows and coverage jobs. The test now builds those arguments with `format!`, as the neighbouring scanner test does.
Signed-off-by: Jussi Elo <jussi.elo@amd.com>
git exports an absolute `GIT_DIR` to a pre-push hook run from a linked worktree. The throwaway-repository helper and the directory-taking `diff_name_only` ran `git -C <tmp>` without clearing it, so in that setting they acted on the outer repository: the rename test could fail, and the helper's commit and staging landed on the developer's branch. Both now clear the variables git uses to select a repository, through one shared `scrub_git_env`, and a test checks that every one is cleared. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
The pre-push hook had no memory, so a push retried after a dropped connection, a manual run before the push, or an amend that only rewords the message re-ran clippy and the tests on content it had just checked. After a step's cargo command exits 0, `cargo xtask pre-push-check` now records a stamp, and the next run of that step looks for it first. The stamp is keyed on the step and its exact cargo arguments (so a pass for a narrow selection never lets a wider one skip), the git tree hash of HEAD (so an amend with identical content still hits), the toolchain, and a digest of the CARGO_* and RUST* environment. It is only used and written when the working tree is clean, because cargo checks the working tree rather than the pushed commit, and nothing is recorded if the run changed the tree. Stamps live under the git common directory, so every worktree shares them, expire after seven days and are capped at 64; scratch files from a write that died are cleaned up. Any error reading, writing or verifying a stamp means the step runs, and git is run with GIT_DIR and the other repository-selecting variables removed when it is pointed at a directory. A skip prints what matched, what it does not cover (user-level cargo configuration, installed tools, files git ignores, flaky tests), and how to force a run (--rerun or ROCM_PRE_PUSH_RERUN=1). CONTRIBUTING.md, docs/testing.md and the hook comment describe the skip and say it shows this tree passed earlier, not that the checks ran during this push. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
There was a problem hiding this comment.
🟡 Changes recommended
The hook can incorrectly skip checks after a failed rerun or on an empty diff, while non-Unicode environment variables can abort it before Cargo runs.
3 open issues
What changed in this PR
Builds on #918’s affected-crate selection to avoid repeating pre-push Cargo checks that already passed on identical content, addressing #896 without changing the rocm binary.
Changes:
- Adds per-step pass stamps keyed by tree, Cargo arguments, toolchain and environment, with clean-tree checks, expiry and bounded storage.
- Adds forced reruns and skip diagnostics while leaving commit-signature verification uncached.
- Adds regression coverage and documents cache behavior, limitations and overrides.
Verification used supplied code and targeted source inspection; tests were not run in this review environment.
| File | Description |
|---|---|
xtask/src/prepush.rs |
Integrates stamp lookup and recording into pre-push execution. |
xtask/src/prepush_stamp.rs |
Implements stamp keys, validation, storage, pruning and tests. |
xtask/src/main.rs |
Wires the command and tests --rerun parsing. |
xtask/src/affected.rs |
Shares range-selection and Git environment helpers. |
docs/testing.md |
Documents hook scope, stamps and verification limits. |
CONTRIBUTING.md |
Explains cached checks and how contributors force reruns. |
.pre-commit-config.yaml |
Routes Cargo hooks through the helper with verbose output. |
Review effort: Balanced
| let resolved = changed_files() | ||
| .context("could not list the changed files") | ||
| .and_then(|files| { | ||
| let graph = load_graph().context("could not read the workspace dependency graph")?; |
There was a problem hiding this comment.
Checked on the current head and it no longer applies: an empty changed-file list goes through resolve_selection, and pre-push-check test --base HEAD --dry-run prints "the pushed range lists no changed files; checking the whole workspace" and plans cargo test --workspace --all-targets --exclude e2e-cucumber. The stamped path was the part nothing exercised, so 2bde696 adds a test: after a narrow pass, an empty list runs the whole-workspace command, records its own pass, is skipped only on its own next run, and the lint step falls back the same way. It fails when the empty-range check is removed.
| return Ok(()); | ||
| } | ||
|
|
||
| run_cargo(&args)?; |
There was a problem hiding this comment.
Fixed in 7886647. A rerun that is going to run now drops exactly this step's matching stamp first, through Stamps::invalidate, so a failed rerun leaves nothing for the next ordinary run to skip on, and a successful rerun records a fresh pass. Other steps' and other selections' stamps are separate files and stay, and --dry-run touches nothing. Tests cover pass, failed rerun and ordinary retry (which must run the step), sibling stamps surviving, a dry run changing nothing, and a passing rerun replacing the pass.
| } | ||
|
|
||
| fn env_digest(&self) -> String { | ||
| env_digest(std::env::vars()) |
There was a problem hiding this comment.
Fixed in 7934447. The probe reads std::env::vars_os() and env_digest takes OsStr-like pairs, matching names as text (every prefix is ASCII) and hashing values as bytes, so an unrelated non-Unicode variable cannot abort the hook and two different relevant ones still differ. Unicode values hash to the same digest as before, so recorded stamps stay valid. A child-process test runs the real probe under non-Unicode variables.
The base pull request merged, so this branch now carries only its own change on top of main. main's version of the narrowing change is kept everywhere; the one overlap was the paragraph in CONTRIBUTING.md that describes the pre-push hooks, which keeps main's wording (including the empty-range fallback) and adds the sentence about skipping steps that already passed on the same tree. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…pace on the stamped path A pushed range that lists no changed files means the range is wrong, not that nothing changed. resolve_selection already turns that into the whole workspace, and the stamped path goes through it, but nothing exercised the two together, so a change that let an empty list through to the "no Rust crate" skip would also bypass the pass stamps. The new test records a narrow pass, then runs with an empty list and expects the whole-workspace test command to run, to be recorded as its own pass and to be skipped only on its own next run, and expects the lint step to fall back the same way. It fails when the empty-range check in resolve_selection is removed. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
… cannot leave it behind --rerun exists for the cases the pass key does not cover, such as a change to user-level cargo configuration or an installed tool. Before, a rerun ignored the recorded pass but left it in place, so when the rerun failed the next ordinary run still skipped on the pass the rerun was meant to double-check. A rerun that is going to run now removes exactly this step's matching stamp first, through a new Stamps::invalidate, and a successful rerun records a fresh pass as before. Stamps of other steps and other selections are different files and stay, and --dry-run still touches nothing. New tests cover pass, failed rerun and ordinary retry (which must run the step), a failed rerun leaving sibling stamps alone, a dry-run rerun leaving every stamp in place, and a passing rerun replacing the pass; each fails when its part of the change is removed. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
juhovainio
left a comment
There was a problem hiding this comment.
I read through the diff and found nothing blocking. The stamp key covers the exact cargo arguments, the tree hash, the toolchain and the environment digest. A stamp is only used or written on a clean tree, and every error path falls back to running the step. Docs, hook comment, --help and CONTRIBUTING agree with each other. This is a diff-only read, not a CI result or a local run.
Small things, none blocking:
- The path from
main.rsthroughprepush::run()to the stamp store has no test, as the PR body notes. A regression that always passesrerun=falsewould not be caught. - The PR body says a write that dies leaves no scratch file behind. The docs and code say a killed process leaves one until a later write prunes it after an hour. The docs are right, so the body could be tightened.
- Two lines in
docs/testing.mdare over-long.
One design note for the author: the PR keeps one stamp per step, where #896 asked for a single stamp after all three hooks pass. I think per-step is a reasonable tradeoff, since a retry after a failed test still reuses clippy.
…ble cannot abort the pre-push hook The environment part of the pass key came from std::env::vars(), which panics as soon as any variable holds bytes that are not valid Unicode, before the digest could filter out the unrelated ones. On Linux a single stray variable was enough to make `pre-push-check` abort even with --rerun, for a feature that is optional. The probe now reads std::env::vars_os(), and env_digest takes OsStr-like pairs: names are matched as text, which is enough because every prefix and exempt name is ASCII, and values are hashed as the bytes they are, so a relevant non-Unicode value still changes the digest and two different ones do not collapse into one. For Unicode values the hashed bytes are the same as before, so recorded stamps stay valid, and a test pins that. The two other environment lookups in the hook (the rerun variable and the git refs) went through std::env::var(..).ok(), which does not panic but reads a non-Unicode value as unset, so a non-Unicode ROCM_PRE_PUSH_RERUN was ignored; they now go through var_os. New tests cover unrelated and relevant non-Unicode bytes in names and values, and a child-process test that runs the real probe under non-Unicode variables; each fails when its part is reverted. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…erun Clippy's collapsible_if lint, with warnings denied as in CI, flagged the nested `if` in `execute` that drops this step's earlier pass before a `--rerun`; it is now one condition with a let chain, and behaves exactly as before. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…through the real wiring The tests of the stamp logic built Stamps by hand, so nothing proved that the flag parsed in main.rs, and the variable read by run(), actually arrive at it: a regression that always passed rerun=false would not have been caught. run() is now a thin wrapper over run_in(), which takes the stamp store, the probe, the clock and the environment lookup as a World and holds the wiring from the flag and the variable into Stamps; the pre-push-check arm of main.rs is a function that takes run as a parameter, so a test sees what the dispatch hands over. New tests parse real command lines and check that every flag arrives unchanged, and run run_in over a fake store with a recorded pass: without the flag or the variable the step is skipped, with --rerun or with the variable set it runs, and 0 or empty means off. Each fails when its part is broken: hard-coding rerun=false in the dispatch fails the dispatch test, hard-coding it in run_in fails the flag test, and dropping the variable lookup fails the variable test. The thin real-world run() wrapper, which builds the real git store, is not covered by a unit test. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
…stamp docs to the file's width A write goes through a scratch file and a rename and removes the scratch file itself when the rename fails, but a process killed mid-write leaves its scratch file behind until a later write prunes it, once it is more than an hour old; a younger one may belong to a concurrent writer. docs/testing.md and the comment on the age limit now say that, and nothing in the docs or comments claims otherwise. The paragraphs this pull request added to docs/testing.md that ran well past the rest of the file are wrapped to its width; no other text is touched. Signed-off-by: Jussi Elo <jussi.elo@amd.com>
|
Thanks for the review. On the notes in the approving comment, all addressed in ac19326 and the commits before it:
On the design note: per-step stamps are kept on purpose, since a retry after a failed |

Summary
The pre-push hook had no memory, so a push retried after a dropped connection, a manual run before the push, or an amend that only rewords the message re-ran clippy and the tests on content it had just checked. After a step's cargo command exits 0,
cargo xtask pre-push-checknow records a stamp, and the next run of that step looks for it first. The stamp is keyed on the step and its exact cargo arguments, so a pass for a narrow selection never lets a wider one skip, the git tree hash ofHEAD, so an amend with identical content still hits, the toolchain, and a digest of theCARGO_*andRUST*environment (values are never stored).It is only used and written when the working tree is clean, because cargo checks the working tree rather than the pushed commit and an untracked file can hide a missing
git add; the tree is probed again after the run and nothing is recorded if cargo changed it orHEADmoved. Stamps live under the git common directory, so every worktree shares them, expire after seven days and are capped at 64, and a write that fails removes its scratch file, while one left by a killed process is pruned by a later write once it is an hour old. Any error reading, writing or verifying a stamp means the step runs. A skip prints what matched, what it does not cover (user-level cargo configuration, installed tools, files git ignores, flaky tests) and how to force a run:--rerunorROCM_PRE_PUSH_RERUN=1.SKIP=<hook id>still skips a hook, andverify-commitsruns every time. A skip shows that this tree passed earlier, not that the checks ran during this push, and CONTRIBUTING.md says so; the--workspacecommands you run by hand are unaffected. A--rerundrops this step's earlier pass before it runs, so a run that fails cannot leave the old pass behind for the next ordinary run, and a successful one records a fresh pass;--dry-runtouches nothing. The process environment is read as raw bytes, so a variable that is not valid Unicode cannot abort the hook, and values that are Unicode hash exactly as before. The wiring from--rerunandROCM_PRE_PUSH_RERUNinto the stamp logic goes through arun_infunction that tests drive with a fake store; the thinrun()over the real git store has no test of its own.Fixes #896.
Git environment
Git is hermetic here: when pointed at a directory it runs with
GIT_DIRand the other repository-selecting variables removed (reusing the helper from #918), because git and prek export an absoluteGIT_DIRto a hook run from a linked worktree, and the tests build their scratch repositories the same way. A test re-runs the test binary under a hostileGIT_DIRand checks that the outer repository is untouched.Tests
This is developer tooling with no change to what the
rocmbinary does, so there is no Gherkin scenario. Unit tests cover each rule above, and I checked that each goes red when its target is broken (12 further mutations after review, all red). The hop frommain.rsthroughrun()to the stamp store is two lines of passthrough and is not tested directly.Run locally:
cargo test --locked -p xtask --all-targets(547 passed),cargo clippy --locked -p xtask --all-targets -- -D warnings, fmt,check-crate-edgesand prek config validation. Not run: the full workspace gate, Windows, and a realgit pushthrough the hook. CI is the authoritative check for the rest.