skip to content

Adopting SwiftLint on a 250k-line app with thousands of warnings, how do --write-baseline, --baseline and --strict make CI fail only on new violations?

level: seniorimportance: must knowfreq 58%

answer

  1. record today's violations once
  2. filter the known ones on every run
  3. upgrade warnings to errors
  4. matched by file, rule and line text
  5. exit status 2 on serious violations

basics

~20 s

Run swiftlint lint --write-baseline once to record every current violation in a JSON file, commit it, and have CI run swiftlint lint --strict --baseline with that file: known violations are filtered out, and any new warning, now an error, fails the job.

solid answer

~40 s

`--write-baseline Baseline.json` saves every violation SwiftLint finds, unfiltered. On later runs `--baseline Baseline.json` drops the violations that file already contains, and `--strict` upgrades the remaining warnings to errors, so SwiftLint exits with status 2 only when something new appears. Matching is per file path, per rule identifier and by the **text of the violating line** plus the violation's reason, not by line number: inserting code above an old violation keeps it baselined, while editing that line, renaming the file or adding another identical violating line makes it new. The flags arrived in SwiftLint 0.55.0, the `baseline` and `write_baseline` config keys in 0.56.0, and 0.65.1 writes paths relative to the working directory so the file is portable.

code

bash · 8 lines
bash
# once, on main
swiftlint lint --write-baseline Baseline.json

# every pull request
swiftlint lint --strict --baseline Baseline.json --reporter json --output swiftlint.json

# what the baseline still holds
swiftlint baseline report Baseline.json --reporter summary

go deeper

for a junior

Recall that --write-baseline records today's violations and --baseline hides them on later runs.

for a middle

Explain why --strict is needed for warnings to fail CI, and that SwiftLint exits with status 2 on serious violations.

for a senior

Explain the matching by file, rule, line text and reason, predict which edits resurface violations, and regenerate the baseline only from main with a reviewed diff.

for a principal

Set the policy for shrinking the baseline over time and for who may regenerate it, since a careless rewrite accepts new violations wholesale.

## The problem a baseline solves The insurance-claims app is seven years old and 250k lines long. Its first full SwiftLint run reports thousands of warnings. Failing the build on all of them would stop every team; ignoring them means new violations vanish into the noise. A **baseline** records the violations that exist today so SwiftLint can report only the ones added after. (Baselines as a general adoption technique, and ratcheting, are covered with static-analysis baselines; this is what SwiftLint itself does.) ## The three commands 1. **Record once.** On a clean `main` branch: ```bash swiftlint lint --write-baseline Baseline.json ``` SwiftLint writes every violation it found, **before any baseline filtering**, as JSON. Commit the file. 2. **Gate every change.** In CI: ```bash swiftlint lint --strict --baseline Baseline.json ``` `--baseline` filters out violations the file already holds. `--strict` upgrades every remaining warning to an error ("Upgrades warnings to serious violations"). SwiftLint exits with status **2** when at least one serious violation survives, which fails the job. 3. **Inspect the debt.** `swiftlint baseline report Baseline.json --reporter summary` prints what the baseline holds, and `swiftlint baseline compare Baseline.json --other-baseline New.json` reports violations present in the second file but not the first. The order matters: leniency is applied to each file's violations first, then the baseline filter, so with `--strict` only unbaselined violations end up as errors. ## How SwiftLint decides a violation is "known" SwiftLint's baseline is not a list of line numbers. For each violation it stores the file path, rule identifier, severity, location, reason and the **text of the source line**. When filtering: - violations are grouped **per file** by relative path, then **per rule identifier**; - an exact match (same location and text) is dropped at once; - what remains is compared by **line text plus reason**, so a violation whose line merely moved still matches its baselined twin; - if a file now holds more unmatched violations with the same text and reason than the baseline has left, every violation in that group is reported, not only the extra one. | Change | Result under `--baseline` | |---|---| | Ten lines inserted above a baselined `force_cast` | still filtered | | The baselined line itself edited | reported as new | | The file renamed or moved | every violation in it reported as new | | A second copy of an identical violating line added | reported as new | | Run from a different working directory | paths stop matching | ## Running it where the build runs The flags have configuration-file equivalents, `baseline: Baseline.json` and `write_baseline: Baseline.json`, plus `strict: true`. They matter when SwiftLint runs as the build tool plugin, which accepts no command-line arguments. A command-line `--strict` or `--lenient` overrides the file's setting, and the two are mutually exclusive. Keep these behaviours in mind: - `--write-baseline` and `--baseline` may name the same path. On the very first run, a missing file is tolerated only in that case; otherwise an unreadable baseline stops the run. - `--write-baseline` writes the unfiltered set, so regenerating it from a branch that added violations silently accepts them. Regenerate on `main` and review the diff. - `warning_threshold: N` is a different gate: it adds an error once the number of reported warnings reaches N. It counts, it does not tell new from old. - Reporters work on what survives the filter: `--reporter json`, `sarif`, `junit` or `checkstyle` formats only the reported violations, and `--output swiftlint.json` writes them to a file the CI system can annotate from. - `swiftlint --fix` runs no lint and ignores `--strict`; it prints "The option --strict has no effect together with --fix." ## Versions `--baseline`, `--write-baseline` and the `baseline` subcommand arrived as experimental features in **SwiftLint 0.55.0**; the `baseline` and `write_baseline` configuration keys arrived in **0.56.0**. **0.65.1** fixed baseline writing to store file locations as paths relative to the current working directory instead of absolute `file://` paths, which is what lets a baseline written on one machine filter violations on a CI runner. Always run SwiftLint from the same directory when writing and reading the file, and treat the baseline as code: it lives in the repository, changes only through reviewed pull requests, and shrinks as the team fixes old violations file by file.

  • A developer renames ClaimViewController.swift and CI suddenly reports 140 violations in it. What happened, and what is the fix?
    SwiftLint groups baselined violations by the file's relative path, so a renamed file has no baseline entries and all its old violations surface as new. Regenerate the baseline on the rename branch with `--write-baseline` after confirming no genuinely new violations were added, or fix the file's violations in the same change.
  • Why not just set warning_threshold just above the current warning count?
    `warning_threshold` adds an error once the reported warnings reach that number, so fixing one old warning makes room for one new warning that nobody notices. It counts warnings; it cannot tell which ones are new. The baseline compares individual violations by file, rule and line text.
  • How do you run the same gate through SwiftLintBuildToolPlugin?
    The plugin passes no arguments you control, so put `baseline: Baseline.json` and `strict: true` in `.swiftlint.yml`. Those keys exist since SwiftLint 0.56.0. Make sure the baseline paths are relative to the directory the plugin runs SwiftLint from, the folder holding the topmost `.swiftlint.yml`.

A baseline is like a rental car's damage sheet that lists each scratch by panel and description: driving the car somewhere else adds nothing, but a fresh scratch, or the same sheet held against a different car, does not match.

  • A SwiftLint baseline records violations by line number.
  • --baseline on its own makes CI fail on new warnings.
  • --write-baseline only writes the new violations.
  • warning_threshold tells new violations from existing ones.
  • Renaming a file keeps its baselined violations hidden.
  • --strict upgrades the baselined violations as well, so CI fails on them.

Assumes SwiftLint 0.65.1: baseline flags since 0.55.0, config keys since 0.56.0, cwd-relative paths in written baselines since 0.65.1.