Skip to content

fix(hooks): rustfmt fallback when cargo fmt exceeds Windows spawn limit - #3658

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/precommit-hook-rustfmt-fallback
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/precommit-hook-rustfmt-fallback

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

scripts/git-hooks/pre-commit runs cargo fmt --check, which enumerates every
workspace target. With ~700 test targets the spawn command line exceeds the
Windows limit, so cargo dies before formatting anything:

$ git commit --allow-empty -m test
pre-commit: rustfmt check failed. Run 'cargo fmt' and re-stage.
文件名或扩展名太长。 (os error 206)

Result: the hook fails on every commit on this machine and everyone is forced
to use --no-verify, defeating the owner directive that fmt be a commit hook.

Fix

Keep cargo fmt --check as the primary gate. When it fails:

  • If the output contains Diff in diagnostics, it is a real formatting
    failure — block exactly as before (machines where cargo fmt works keep the
    full-repo check).
  • Otherwise it is an operational failure (os error 206, missing component,
    metadata error). Fall back to running rustfmt --edition 2021 --check on
    the .rs files staged in this commit only
    (git diff --cached --name-only --diff-filter=ACMR | grep '\.rs$').
    Any unformatted staged file blocks the commit; clean or no staged .rs
    files passes.

The hook remains a POSIX sh script, runnable under Git Bash.

Verification (this machine, where cargo fmt always hits os error 206)

  • Staged unformatted .rs → commit blocked (pre-commit: unformatted: <file>).
  • Staged formatted .rs → commit allowed.
  • Empty commit / no staged .rs → allowed.
  • This very commit was written through the fixed hook (no --no-verify).

Generated with Devin

@caomengxuan666
caomengxuan666 merged commit 10d13dd into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
Conflict resolution: scripts/git-hooks/pre-commit kept the PR's
fail-closed batched check-rustfmt.py gate. Master's staged-file
fallback (#3658) is the weaker same-symptom workaround this PR
supersedes (whole-workspace coverage vs staged-only, fail-closed vs
fail-open); the audit trail on #3569 recommends this direction.

Also applies rustfmt --edition 2021 to four master-side files
(src/executor/{external_inner,external_redirects,trap_exec}.rs,
tests/pipeline_stderr_redirect_streaming.rs) that drifted unformatted
under commits that bypassed full-workspace fmt coverage; required so
the merged tree passes the gate being introduced.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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