Skip to content

Add SwiftFormat lint check on pull requests#599

Open
bjorkert wants to merge 2 commits intodevfrom
add_lint_workflow
Open

Add SwiftFormat lint check on pull requests#599
bjorkert wants to merge 2 commits intodevfrom
add_lint_workflow

Conversation

@bjorkert
Copy link
Copy Markdown
Contributor

@bjorkert bjorkert commented Apr 9, 2026

Adds a Lint workflow that runs swiftformat --lint on every pull request.

What it does

  • Runs on ubuntu-latest inside the official swift:6.0 container — no macOS minutes consumed.
  • Builds SwiftFormat from BuildTools/Package.swift so the CI version is always the same pin developers use locally (currently 0.56.1). No separate config to drift.
  • Same --header and --exclude flags as Scripts/swiftformat.sh, so whatever passes locally passes in CI.
  • --lint mode: reports violations and exits non-zero without rewriting any files. The PR check goes red; the contributor runs swiftformat locally and pushes the fix themselves. CI never commits back over a contributor's branch.
  • BuildTools/.build is cached by Package.resolved hash — first run compiles SwiftFormat (~2–3 min), subsequent runs are <30s.

Why not auto-fix and commit back

  • The default GITHUB_TOKEN can't push to fork PRs, so any auto-fix path requires a PAT with push rights — a real security footgun for a lint job.
  • Race conditions: CI commits a format fix while the contributor is pushing a real commit, and the contributor has to rebase over a change they didn't author.
  • git blame and commit authorship get muddied by bot commits.
  • A failing check teaches the contributor to run the formatter locally; a silent auto-fix teaches them nothing.

Enabling as a required check

The workflow only reports status. To actually block merges on lint failures, the check needs to be marked Required in branch protection (Settings → Branches → dev → "Require status checks to pass before merging" → add Lint / SwiftFormat). GitHub only lets you require checks it has seen run at least once, so this should be done after the first successful run on this PR.

bjorkert added 2 commits April 9, 2026 21:10
Runs swiftformat --lint in a Linux container on every PR using the
same BuildTools pin (SwiftFormat 0.56.1) and flags as the local
Scripts/swiftformat.sh, so CI stays in sync with `swift run swiftformat`
on a developer machine.

Fails the check on any violation without rewriting files — contributors
fix formatting themselves and push, rather than having CI commit back
over their branch.

BuildTools/.build is cached by Package.resolved hash to keep subsequent
runs fast.
Adds a concurrency group so rapid successive pushes to a PR branch
don't stack up multiple Lint runs — the in-progress run is cancelled
as soon as a newer commit arrives.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant