Skip to content
Latchkey

pre-commit "trailing-whitespace" modifies files and fails in CI

The trailing-whitespace hook removes trailing spaces from lines. Because it edits files, pre-commit reports "files were modified by this hook" and exits non-zero, failing the CI check.

What this error means

The hook prints "Failed" with "files were modified by this hook" and lists files it trimmed. Locally, running it and committing makes it pass.

pre-commit
Trim Trailing Whitespace...................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook

Fixing src/app.py

Common causes

Committed files had trailing whitespace

The hook fixed the whitespace, which is a modification, so pre-commit fails in CI where there is no follow-up commit.

Hooks were not run locally before pushing

CI is the first place the whitespace hook touches the files, so it fails there instead of at commit time.

How to fix it

Run the hook and commit the fixes

  1. Run pre-commit run trailing-whitespace --all-files locally.
  2. Commit the trimmed files.
  3. Push again so CI has nothing to fix.
Terminal
pre-commit run trailing-whitespace --all-files
git add -u && git commit -m "chore: trim trailing whitespace"

Show the diff so the fix is obvious in CI

Add --show-diff-on-failure so the whitespace change is visible in the log.

.github/workflows/ci.yml
- run: pre-commit run --all-files --show-diff-on-failure

How to prevent it

  • Install the git hook locally so whitespace is trimmed at commit time.
  • Use --show-diff-on-failure in CI.
  • Configure editors to strip trailing whitespace on save.

Related guides

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