A project runs SwiftFormat and SwiftLint, and trailing commas and multiline opening braces keep flipping between builds; why, and how do you stop it?
answer
- two tools, one whitespace decision
- opposite defaults on trailing commas
- a brace alone on its own line
- both sides can autocorrect
- one owner per layout concern
basics
~20 sBoth tools have layout rules with opposite defaults: SwiftFormat adds trailing commas and moves a multiline statement's brace onto its own line, while SwiftLint's trailing_comma and opening_brace want the reverse. Give each concern one owner and align the other.
solid answer
~40 sSwiftFormat's default `trailingCommas` rule runs with `--trailing-commas always`, adding a comma after the last element of every multiline collection, while SwiftLint's default `trailing_comma` rule, with `mandatory_comma: false`, flags those commas and its `--fix` removes them. SwiftFormat's default `wrapMultilineStatementBraces` moves `{` onto its own line after a condition or signature that wraps, while SwiftLint's `opening_brace` wants the brace on the declaration's line and also autocorrects. Run both autocorrectors and each undoes the other. Let SwiftFormat own layout: set `trailing_comma: mandatory_comma: true` and opening_brace's `ignore_multiline_*` keys in `.swiftlint.yml`, or turn the overlapping rules off there. Alternatively change SwiftFormat's side with `--trailing-commas never` or `--disable wrapMultilineStatementBraces`, after which its `braces` rule pulls the brace back up. Also set `--max-width` to SwiftLint's 120-column `line_length`.
code
yaml · 6 linestrailing_comma:
mandatory_comma: true
opening_brace:
ignore_multiline_statement_conditions: true
ignore_multiline_function_signatures: true
ignore_multiline_type_headers: truego deeper
Recall that SwiftFormat rewrites layout while SwiftLint mainly reports, and that both have rules about trailing commas and opening braces.
Explain the opposite defaults: trailingCommas with always versus trailing_comma without mandatory_comma, and wrapMultilineStatementBraces versus opening_brace.
Diagnose the ping-pong from both tools' autocorrect, give each concern one owner, align the other side's configuration, and run the formatter before the linter.
Treat the split as a written team contract between the two config files and re-check it on every tool upgrade, since a new default rule can reopen the conflict.
## Why the two tools collide SwiftFormat and SwiftLint overlap on purpose: SwiftFormat's README notes that it can fix some style violations SwiftLint reports but cannot autocorrect. The overlap turns into a fight when both tools hold an opinion on the same piece of layout and their defaults disagree. The two classic cases are trailing commas and opening braces. ## Collision 1: trailing commas | | SwiftFormat | SwiftLint | |---|---|---| | Rule | `trailingCommas`, enabled by default | `trailing_comma`, enabled by default | | Default | `--trailing-commas always`: add a comma after the last element of a multiline list | `mandatory_comma: false`: report a trailing comma in an array or dictionary literal | | Autocorrect | every formatting run | `swiftlint lint --fix` removes the comma | With defaults on both sides, every multiline array SwiftFormat touches gains a comma that SwiftLint then reports as "Collection literals should not have trailing commas". If both run their autocorrect, the comma appears and disappears on alternate passes. With a Swift version of 6.1 or later declared, SwiftFormat also adds commas to multiline argument and parameter lists; SwiftLint's rule only inspects collection literals, so those do not collide. ## Collision 2: braces after a wrapped condition SwiftFormat's `braces` rule defaults to K&R style, `{` at the end of the line, which matches SwiftLint's `opening_brace`. The conflict comes from SwiftFormat's default-enabled `wrapMultilineStatementBraces`, which moves the brace onto its own line when the condition or signature before it spans several lines: ```swift struct Pet { let isVaccinated: Bool let ageInMonths: Int } func approve(_ pet: Pet) {} func review(_ pet: Pet) { if pet.isVaccinated, pet.ageInMonths >= 2 { approve(pet) } } ``` SwiftLint's `opening_brace` requires the brace on the same line as the declaration, so it flags this layout and its `--fix` pulls the brace back up; the next SwiftFormat pass pushes it down again. ## Fixing it: one owner per concern Pick which tool owns each piece of layout, then make the other agree or stay silent. The usual split is that **SwiftFormat owns whitespace, wrapping, braces and commas**, because it rewrites them reliably, and **SwiftLint owns everything that needs judgement or reporting**, such as custom rules, complexity limits and banned APIs. Option A, keep SwiftFormat's style and align SwiftLint: 1. In `.swiftlint.yml`, set `trailing_comma` to `mandatory_comma: true`, so SwiftLint instead requires the trailing comma in multiline collection literals that SwiftFormat inserts. 2. Set `opening_brace`'s `ignore_multiline_statement_conditions`, `ignore_multiline_function_signatures` and `ignore_multiline_type_headers` to `true`, so wrapped headers may put the brace on its own line. 3. Or simply disable those SwiftLint rules and let SwiftFormat's `--lint` step enforce them. Option B, keep SwiftLint's style and change SwiftFormat: 1. `--trailing-commas never` makes SwiftFormat remove trailing commas instead of adding them; `collections-only` adds them only in collection literals. 2. `--disable wrapMultilineStatementBraces` stops the wrapping, and with that rule disabled SwiftFormat's `braces` rule moves such braces back onto the previous line, provided the joined line fits `--max-width`. ## The line-length near-miss SwiftLint's `line_length` warns at 120 columns by default, while SwiftFormat's `--max-width` defaults to `none`, so the `wrap` rule never shortens long lines. Setting `--max-width 120` lets SwiftFormat wrap what it can before SwiftLint complains; a long string literal can still exceed the limit, so `line_length` stays a SwiftLint concern. ## Keeping it fixed - Run SwiftFormat first and SwiftLint second, and avoid letting SwiftLint's `--fix` touch rules SwiftFormat owns. - In CI, run `swiftformat --lint` for layout and SwiftLint for its own rules, so each failure names the tool that owns it. - After upgrading either tool, re-run both over a clean checkout: a newly default-enabled rule on either side can reopen the fight. - Write the split down beside both config files, so the next person who touches one knows the other exists. ## Finding the pair that fights Both tools name the rule in every message, which makes the culprit easy to pin down: - A SwiftFormat lint line ends with the rule in parentheses, such as `(trailingCommas) Add or remove trailing commas in comma-separated lists.` - A SwiftLint warning ends with its identifier in parentheses, such as `(trailing_comma)`. - The same file and line appearing in both reports, before and after a SwiftFormat run, identifies the pair; then decide which side yields, using one of the two options above.
- Why not just disable trailingCommas and wrapMultilineStatementBraces in SwiftFormat and keep SwiftLint's defaults?That works and is option B. The cost is style: with `wrapMultilineStatementBraces` off, SwiftFormat's `braces` rule joins the brace to the last line of a wrapped condition, where it can be hard to see where the body starts. Teams that prefer the wrapped brace align SwiftLint instead.
- In what order should a build or CI job run the two tools?Formatter first, linter second. `swiftformat` (or `swiftformat --lint` in CI) settles layout, then SwiftLint reports the rules it owns, and no SwiftLint autocorrection runs on concerns SwiftFormat owns. Running the linter's `--fix` after the formatter on overlapping rules is what recreates the ping-pong.
- How do you check that the two configurations really agree?On a clean checkout, run `swiftformat .`, then SwiftLint, then `swiftformat --lint .` again. The second SwiftFormat pass must report nothing, and SwiftLint must raise no style warning on code SwiftFormat just wrote. Repeat it after upgrading either tool.
Two thermostats in one room set to different temperatures: one heats while the other cools, and the room never settles until one of them is switched off or both get the same setting.
- SwiftFormat reads .swiftlint.yml and adapts its rules to match it.
- Running SwiftLint's --fix after SwiftFormat settles any conflict for good.
- SwiftLint's trailing_comma rule also inspects function call arguments.
- Disabling wrapMultilineStatementBraces leaves wrapped braces where they are.
- The conflict means one of the tools has a bug to report upstream.