Pre-commit hooks, formatting & linting
Let machines argue about spaces so reviewers can talk about logic
The problem
Half the comments on an unformatted PR are about style: spacing, <- vs =, line length. Those are cheap for a machine to fix and expensive for a person to review. Then there are the embarrassing slips: a browser() left in, a file that doesn’t parse, a NAMESPACE out of date.
Three layers solve this. Each one is faster and closer to you than the next:
| Layer | Runs | Catches |
|---|---|---|
| Editor (format on save) | While you type | Style |
| Pre-commit hook | On git commit, on staged files |
Style, lint, debug leftovers, unparsable files |
| CI | On every PR | Everything, for everyone, with no way to skip |
The web-repo way
In our TypeScript project, husky installs git hooks when you run npm install, and lint-staged runs the formatter and linter (Biome) on staged files only:
// lint-staged.config.mjs
export default {
"*.{ts,tsx,js,json,css,md}": ["biome check --write --no-errors-on-unmatched"],
};Design choices worth copying:
- Only staged files, so a commit takes about a second, however big the repo.
- Auto-fix, then re-stage. Formatting is fixed, not just reported.
- Slow checks stay in CI. Typecheck, tests and build run on the PR, not on every commit. If a hook is slow, people learn to skip it with
--no-verify. - CI re-runs the same checks (
biome ci), because hooks are local and optional.
The R way
Formatter: Air (or styler)
Air is Posit’s formatter for R. It’s extremely fast, and it’s built into Positron and available for RStudio. styler is the long-standing alternative.
usethis::use_air() # usethis >= 3.2.0: configures the project for Airair format . # format the project
air format . --check # fail if anything would change (for CI)Linter: lintr
lintr::use_lintr(type = "tidyverse") # writes a .lintr config
lintr::lint_package()The linter finds what a formatter can’t: unused variables, T/F instead of TRUE/FALSE, 1:length(x), object names, cyclomatic complexity.
Pre-commit hooks: the precommit package
precommit brings the pre-commit framework to R, with hooks written for R projects. Save templates/pre-commit/pre-commit-config.yaml in your project root as .pre-commit-config.yaml:
repos:
- repo: https://github.com/posit-dev/air-pre-commit
rev: 0.11.0
hooks:
- id: air-format
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3
hooks:
- id: parsable-R
- id: no-browser-statement
- id: no-debug-statement
- id: roxygenize # keep man/ and NAMESPACE in sync
- id: deps-in-desc # every pkg::fn() is declared in DESCRIPTION
- id: lintr
- id: readme-rmd-rendered
- repo: https://github.com/gitleaks/gitleaks # see secret scanning
rev: v8.30.1
hooks:
- id: gitleaksprecommit::use_precommit() # writes the config (if missing) and installs the git hookIt relies on the Python pre-commit tool. precommit::install_precommit() can set it up for you.
CI: the gate nobody can skip
r-lib/actions includes lint.yaml, lint-changed-files.yaml and style.yaml examples, and Air has posit-dev/setup-air:
usethis::use_github_action("lint") # lintr on every PR
usethis::use_github_action(url = "https://github.com/posit-dev/setup-air/blob/main/examples/format-check.yaml")Air’s format-suggest.yaml example goes one better. It posts the formatting fixes as suggestions on the PR, so a contributor without Air can accept them with a click.
Pitfalls
- Format the whole repo once, in its own PR, before switching the check on. Otherwise the first real PR drowns in unrelated diffs.
- Hooks aren’t cloned. Every developer runs
precommit::use_precommit()(ornpm install) once. Put it in CONTRIBUTING. - Keep hooks fast.
roxygenizeis the slow one in big packages; move it to CI if people start skipping hooks. - Pin hook versions (
rev:). Dependabot’spre-commitecosystem can keep them current. - Don’t let the hook and CI disagree. Use the same tool versions and config files for both.
Sources
Web-repo tools
- husky: typicode.github.io/husky
- lint-staged: github.com/lint-staged/lint-staged
- Biome: biomejs.dev
R tools
- Air: posit-dev.github.io/air
- styler: styler.r-lib.org
- lintr: lintr.r-lib.org
- precommit: lorenzwalthert.github.io/precommit
- pre-commit framework: pre-commit.com
- posit-dev/air-pre-commit: github.com/posit-dev/air-pre-commit
- gitleaks pre-commit hook: github.com/gitleaks/gitleaks
- r-lib/actions (
lint.yaml,lint-changed-files.yaml,style.yaml): github.com/r-lib/actions - posit-dev/setup-air: github.com/posit-dev/setup-air
R package documentation
- usethis (
use_air(),use_github_action()): usethis.r-lib.org
- 2026-09-26: Removed the end-of-post checklist.
- 2026-09-27: Added a Sources section.
- 2026-09-27: Moved the pre-commit configuration into a reusable template.