Linting & Formatting Gates
The cheapest job in a pipeline catches a surprising share of problems: syntax errors, unused variables, accidental debug code, whitespace damage, dangerous calls. Linting should be the first stage, it should take seconds, and it should be exactly the same command developers run locally, usually through a pre-commit hook.
A lint stage from small checks
This session builds a ci/lint.sh from tools that need nothing installed: git diff --check (whitespace errors and leftover conflict markers), python3 -m py_compile (syntax) and node --check (JavaScript syntax). It keeps going after the first failure, so one run reports every problem, and it exits non-zero if anything failed:
mkdir shop && cd shop && git init -q
mkdir ci src
cat > ci/lint.sh <<'SH'
#!/usr/bin/env bash
# ci/lint.sh BASE: lint what changed since BASE; report everything, fail at the end
set -uo pipefail
base=${1:-HEAD~1}
status=0
check() { local name=$1; shift; if "$@"; then echo "ok $name"; else echo "FAIL $name"; status=1; fi; }
check "whitespace" git diff --check "$base" --
for f in $(git diff --name-only --diff-filter=d "$base" -- '*.py'); do
check "python $f" python3 -m py_compile "$f"
done
for f in $(git diff --name-only --diff-filter=d "$base" -- '*.js'); do
check "node $f" node --check "$f"
done
exit $status
SH
chmod +x ci/lint.sh
printf 'def total(xs):\n return sum(xs)\n' > src/cart.py
git add . && git commit -qm "init"
printf 'def total(xs):\n return sum(xs) \n\ndef tax(x)\n return x * 0.2\n' > src/cart.py
printf 'export const price = (n) => n * 100;\n' > src/price.js
git add . && git commit -qm "feat: tax"
./ci/lint.sh HEAD~1
echo "exit $?"src/cart.py:2: trailing whitespace.
+ return sum(xs)
FAIL whitespace
File "src/cart.py", line 4
def tax(x)
^
SyntaxError: expected ':'
FAIL python src/cart.py
ok node src/price.js
exit 1
Three habits are visible here:
- Lint the diff, not the world, when adopting a linter on an old codebase: new code must be clean, old code gets cleaned up over time. On pull requests,
BASEis the merge base with the target branch (git merge-base origin/main HEAD). - Report all, then fail. One run with three findings beats three runs with one each.
- Machine-readable failure. The exit code is the gate; the text is for humans.
Real linters per language
The session's checks are minimal on purpose. Real projects use dedicated linters and formatters, pinned in the repository:
| Language / file | Linter | Formatter (check mode in CI) |
|---|---|---|
| Python | Ruff (ruff check) |
ruff format --check or Black --check |
| JavaScript / TypeScript | ESLint | Prettier --check |
| Go | go vet, staticcheck, golangci-lint |
gofmt -l (fail if it lists files) |
| Shell | ShellCheck | shfmt -d |
| Dockerfile | Hadolint | — |
| YAML | yamllint | — |
| GitHub Actions workflows | actionlint (also checks expressions and shell in run:) |
— |
| Terraform | tflint, terraform validate |
terraform fmt -check |
Formatters run in check mode in CI and in write mode on the developer's machine. CI should never reformat and commit code itself: the commit would skip review and retrigger the pipeline.
pre-commit: the same checks before the push
The pre-commit framework runs hooks defined in .pre-commit-config.yaml on every git commit, and the same command (pre-commit run --all-files) runs them in CI. A typical file (non-run: pre-commit downloads hook repositories):
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.17.3
hooks:
- id: ruff-check
- id: ruff-format
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1
hooks:
- id: gitleaksPin rev: to tags (or commit SHAs) and update them with pre-commit autoupdate in a reviewed PR. Hooks on laptops are a convenience: they can be skipped with git commit --no-verify, so CI must run the same checks again. The gitleaks hook is the start of secret detection, covered in Secret Detection with gitleaks.
Test yourself
Why does ci/lint.sh record failures and exit at the end instead of using set -e?
set -edoesn't work in CI- So a single run reports every problem, and the final exit code still fails the job
- To make the job faster
- Linters require it
How should a formatter run in CI?
- In write mode, committing the result
- In check mode, failing the job if files would change
- Only on main
- It shouldn't run in CI
Why must CI repeat checks that pre-commit hooks already run?
- Hooks are slower
- Hooks can be skipped (
--no-verify) or not installed, so only CI guarantees the check ran - CI uses different rules
- It doesn't need to