Skip to content
Latchkey

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 1

Common 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 --check

How to prevent it

  • Run cargo fmt --all (or a pre-commit hook) before pushing.
  • Pin the toolchain and commit rustfmt.toml so formatting is identical everywhere.
  • Keep editor format-on-save aligned with the project rustfmt config.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →