Skip to content

ci: check that Cargo.lock is what cargo writes, so a misplaced entry fails CI and not the next checkout - #288

Merged
aRustyDev merged 1 commit into
mainfrom
ci/cargo-lock-guard
Nov 17, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
ci/cargo-lock-guard

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

A guard for what #284 fixed: Cargo.lock must be what cargo itself writes, so that a misplaced entry fails CI instead of surfacing as a dirty checkout. Part of the follow-up to #279 and #284; this PR closes nothing.

The gap

cargo metadata --locked, which every other step runs, accepts a lockfile whose packages are in the wrong order (measured on main at 15bcfb0: exit 0, nothing changed). A cargo command without --locked then rewrites the file by 14 lines. #279 added thrust-preview-provisioner one place late, nothing failed, and a clean checkout of main was dirty after its first cargo run until #284. Nothing in CI could have said so.

What it does

scripts/check-cargo-lock.sh runs cargo metadata without --locked, the way a person does, and compares the lockfile with what it was:

  • exit 0: cargo left it alone. exit 1: cargo rewrote it; the diff is printed. This also catches a lockfile that has not heard of a change to a manifest. exit 2: it cannot vouch (no Cargo.toml, no lockfile, cargo failing, which is not an answer about the order).
  • It puts the lockfile back as it found it, whatever the result (an interrupted run included), so running it never leaves a checkout dirty.
  • --self-test builds small workspaces with no dependencies (no network) and checks the accept and refuse cases against cargo's own output: the lockfile cargo wrote, two packages swapped, a member the lockfile has not heard of, no lockfile, no manifest, and a cargo that fails; each refusal also checks that the file was put back. POSIX sh, as CI runs its scripts under dash.

Wiring: two steps of the deps job in lint.yml (the self-test and the check); plan-ci.py lists the script with the other guards of the deps job (so a change to it runs deps and ci-scripts, and not everything, which is what "a script this planner does not know" does); docs/src/testing/running.md has the commands and the deps row. Cargo.lock itself already runs everything.

Controls (AGENTS.md rule 8)

  • The real defect: the guard on the lockfile as it was before chore(deps): put thrust-preview-provisioner in Cargo.lock where cargo sorts it #284 (git show 15bcfb0:Cargo.lock, which differs from today's only by that move, 14 lines) exits 1 and prints the move (thrust-preview-provisioner ahead of thrust-profiling), and leaves the file byte-identical to what it was given. On today's lockfile it exits 0 ("what cargo writes (491 packages)") and the tree stays clean.
  • The self-test can fail: four broken copies of the script, each of which must fail the self-test on the cases that guard the line it breaks:
Broken Fails
cargo run with --locked (what the rest of CI does) 2: the disorder is accepted, and the stale member becomes an error
no restore after a rewrite 2: the rewritten file is left rewritten
a cargo failure counted as success 1
any change accepted 4
  • Shells: the self-test is 10 of 10 on macOS and under dash in a Linux container (rust:1, /bin/sh is dash, CHECK_CARGO=cargo).

Verification

  • test_plan_ci.py: 7008 passed, none bad (it holds one sample per planner rule; the rule is extended, not added). The planner puts the new script with the other deps guards, an unknown script and Cargo.lock still run everything.
  • All 24 steps of the deps job, the 22 it had and the two new ones, which also lint the edited workflow (lint-all-workflows.sh, actionlint). My local list of the steps was compared with lint.yml mechanically.
  • shellcheck on the new script.

Not checked

  • The docs job's book build (mdbook-mermaid is not installed here); the docs edit is two lines in a table and a code block.
  • That the guard's run time on a cold runner is small: it runs cargo metadata, which the deps job's check-deps.sh has just run with the same cache, so the crates are already downloaded. I have not seen it on a runner.
  • The whole CI run.

Refs: #284

🤖 Generated with Claude Code

…fails CI and not the next checkout

`cargo metadata --locked`, which every other step runs, accepts a lockfile whose packages are in the wrong order, and a cargo command
without `--locked` then rewrites the file. #279 added `thrust-preview-provisioner` one place late and #284 moved it; nothing had failed
in between, and a clean checkout of `main` was dirty after its first cargo run. Nothing in CI could have said so.

`scripts/check-cargo-lock.sh` runs `cargo metadata` without `--locked` and fails (exit 1, with the diff) when the lockfile is not what
cargo wrote back, which also catches a lockfile that has not heard of a change to a manifest. It exits 2 when it cannot vouch (no
Cargo.toml, no lockfile, cargo failing). It puts the lockfile back as it found it whatever the result, so it never leaves a checkout
dirty. `--self-test` builds small workspaces with no dependencies and checks the accept and refuse cases against cargo's own output (no
network).

- `.github/workflows/lint.yml`: two steps of the `deps` job, the self-test and the check.
- `.github/scripts/plan-ci.py`: the script is a guard of the `deps` job (`deps` and `ci-scripts`), and not "a script this planner does
  not know", which runs everything. `Cargo.lock` itself already runs everything.
- `docs/src/testing/running.md`: the two commands and the `deps` row.

Controls (AGENTS.md rule 8): the guard on the lockfile as it was before #284 (`git show 15bcfb0:Cargo.lock`) exits 1 and prints the
14-line move, and leaves that file as it found it; on the real lockfile it exits 0 (491 packages) and the tree stays clean. Four
broken copies of the script each fail the self-test on the cases that guard the broken line: cargo run with `--locked` (the disorder
is accepted, 2 cases fail), no restore after a rewrite (2), a cargo failure counted as success (1), a check that accepts any change
(4). The self-test is 10 of 10 on macOS and under dash on Linux.

Refs: #284
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev enabled auto-merge (squash) November 17, 2026 22:01
@aRustyDev
aRustyDev merged commit 1968e2e into main Nov 17, 2026
46 checks passed
@aRustyDev
aRustyDev deleted the ci/cargo-lock-guard branch November 17, 2026 22:18
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.

1 participant