Cargo "fmt --check" failed in CI
cargo fmt --check reports formatting differences without changing files and exits non-zero when any are found. CI uses it as a gate, so unformatted code fails the build even though it compiles.
What this error means
The fmt step prints a diff (Diff in src/...) and exits 1. It is deterministic for a given rustfmt version and config.
cargo
Diff in /home/runner/work/app/src/main.rs at line 4:
fn main() {
- println!("hi" );
+ println!("hi");
}
error: process exited with code 1Common causes
Code not run through rustfmt
The committed code does not match what rustfmt would produce, so --check reports a diff and fails.
rustfmt version or config mismatch
A different rustfmt version, edition, or rustfmt.toml between local and CI changes the expected formatting.
How to fix it
Format the code and commit
Terminal
cargo fmt --all
git add -A && git commit -m "cargo fmt"Pin the rustfmt toolchain in CI
Use the same toolchain version locally and in CI so the formatting target matches.
.github/workflows/ci.yml
rustup component add rustfmt
cargo fmt --all --checkHow to prevent it
- Run
cargo fmt --all(or a pre-commit hook) before pushing. - Pin the toolchain and commit
rustfmt.tomlso formatting is identical everywhere. - Keep editor format-on-save aligned with the project rustfmt config.
Related guides
Cargo clippy "-D warnings" failures in CIFix cargo clippy failures in CI when run with `-D warnings` -- lints that are warnings locally become hard er…
Rust Clippy "-D warnings" / pedantic Lints Failing CIFix CI failing on cargo clippy with -D warnings or clippy::pedantic - lints promoted to errors fail the build…
Rust "unused variable" Denied as Error (-D warnings) in CIFix Rust builds where "unused variable" (or other lints) fail CI under -D warnings / deny(warnings) -- the wa…