Skip to content

fix(init): pre-commit hook runs cargo fmt --check on staged Rust (REQ-210, #438)#505

Merged
avrabe merged 1 commit into
mainfrom
fix/init-hooks-fmt-check
Jun 6, 2026
Merged

fix(init): pre-commit hook runs cargo fmt --check on staged Rust (REQ-210, #438)#505
avrabe merged 1 commit into
mainfrom
fix/init-hooks-fmt-check

Conversation

@avrabe
Copy link
Copy Markdown
Contributor

@avrabe avrabe commented Jun 6, 2026

Problem (#438)

rivet init --hooks installs a pre-commit hook that runs only rivet validate — there is no formatting gate. A misformatted-Rust diff is the single most common first-push CI failure, and AI agents kept hitting it: the commit "passes" locally (validate is clean), then CI's cargo fmt --check job goes red.

The fmt-checking pre-commit hook that this repo actually uses was installed by the dev-only scripts/install-hooks.sh — which rivet init --hooks does not use. So every project that adopts hooks the shipped way gets no formatting gate. (The original issue framed it as the Claude Code hook missing fmt; the accurate root cause is the shipped cmd_init_hooks template.)

Fix

cmd_init_hooks (rivet-cli/src/main.rs) now writes a pre-commit hook that runs cargo fmt --all -- --check before the existing rivet validate step — guarded to stay language-agnostic:

if command -v cargo >/dev/null 2>&1 \
    && git diff --cached --name-only --diff-filter=ACM | grep -q '\.rs$'; then
    if ! cargo fmt --all -- --check; then
        echo "cargo fmt: formatting issues. Run 'cargo fmt --all', then re-stage."
        exit 1
    fi
fi

The fmt step runs only when cargo is on PATH and the commit stages at least one .rs file, so non-Rust rivet projects (and cargo-less machines) skip it silently and are never blocked. Mirrors the proven logic in scripts/install-hooks.sh. Convenience only — CI remains the real gate (REQ-051) and --no-verify still bypasses it.

Verification (acceptance, REQ-210)

  • Temp-repo rivet init --hooks → generated .git/hooks/pre-commit contains the fmt step + the .rs guard + the command -v cargo guard, preserves the rivet validate --format json step, and passes bash -n.
  • cargo test -p rivet-cli --test cli_commands127 passed, 0 failed (the full CLI suite, per the REQ-209 lesson).
  • cargo fmt --check + cargo clippy -p rivet-cli --all-targets -- -D warnings — exit 0.
  • rivet validate — PASS.

REQ-210 filed and flipped to implemented in the same commit (repo convention, cf. REQ-208).

Closes #438.

🤖 Generated with Claude Code

…-210, #438)

`rivet init --hooks` installed a pre-commit hook that ran only `rivet
validate` — no formatting gate. A misformatted-Rust diff is the most common
first-push CI failure, and AI agents kept hitting it: commit "passes"
locally, then CI's `cargo fmt --check` job goes red (#438). The fmt-checking
hook that lives in THIS repo was installed by the dev-only
scripts/install-hooks.sh, which `rivet init --hooks` does not use — so every
project adopting hooks the shipped way got no formatting gate.

cmd_init_hooks now writes a pre-commit hook that runs `cargo fmt --all --
--check` before `rivet validate`, guarded so it stays language-agnostic:
the fmt step runs only when `command -v cargo` succeeds AND the commit
stages at least one `.rs` file (`git diff --cached … | grep -q '\.rs$'`).
Non-Rust rivet projects and cargo-less machines skip it silently and are
never blocked. Mirrors scripts/install-hooks.sh. Convenience only; CI is the
real gate (REQ-051) and `--no-verify` still bypasses it.

Confirmed with a temp-repo `rivet init --hooks` (generated hook contains the
fmt step + both guards, passes `bash -n`, preserves the validate step);
cargo test -p rivet-cli --test cli_commands (127 passed, 0 failed — the full
suite); cargo fmt --check + cargo clippy -p rivet-cli --all-targets -- -D
warnings (exit 0); rivet validate PASS.

Implements: REQ-210
Refs: REQ-051

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@avrabe avrabe merged commit ecb073a into main Jun 6, 2026
24 of 25 checks passed
@avrabe avrabe deleted the fix/init-hooks-fmt-check branch June 6, 2026 07:37
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2026

📐 Rivet artifact delta

Change Count
Added 1
Removed 0
Modified 0
Downstream impacted (depth ≤ 5) 0

Graph

graph LR
  REQ_210["REQ-210"]:::added
  classDef added fill:#d4edda,stroke:#28a745,color:#155724
  classDef removed fill:#f8d7da,stroke:#dc3545,color:#721c24
  classDef modified fill:#fff3cd,stroke:#ffc107,color:#856404
  classDef overflow fill:#e2e3e5,stroke:#6c757d,color:#495057,stroke-dasharray: 3 3
Loading
Added
  • REQ-210

📎 Full HTML dashboard attached as workflow artifact rivet-delta-pr-505download from the workflow run.

Posted by rivet-delta workflow. The graph shows only changed artifacts; open the HTML dashboard (above) for full context.

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.

Claude Code pre-commit hook stamps provenance but doesn't run cargo fmt --check → AI commits pass locally, fail CI Format

1 participant