Skip to content

Fix --only-rule to run custom rules#6664

Open
Chupik wants to merge 1 commit into
realm:mainfrom
Chupik:fix/only-rule-supports-custom-rules
Open

Fix --only-rule to run custom rules#6664
Chupik wants to merge 1 commit into
realm:mainfrom
Chupik:fix/only-rule-supports-custom-rules

Conversation

@Chupik

@Chupik Chupik commented May 25, 2026

Copy link
Copy Markdown

Introduction

We noticed that SwiftLint in some cases ignores --only-rule param. We finally reproduced the problem and used an LLM to investigate and fix it. The explanation below was drafted with LLM assistance.

Problem

activateCustomRuleIdentifiers only expanded custom rules for the config's only_rules: (.onlyConfiguration), and even there only when custom_rules itself was the listed identifier. It never expanded for the --only-rule command-line flag (.onlyCommandLine), and never for individual custom rules in only_rules:. As a result three command shapes silently produced zero violations:

  • swiftlint lint --only-rule custom_rules — the parent rule runs but every individual custom rule is filtered out of customRuleConfigurations.
  • swiftlint lint --only-rule <a_custom_rule> — the custom rule's identifier is recognized as valid, but the parent custom_rules rule isn't enabled, so the rule never executes.
  • only_rules: [<a_custom_rule>] in .swiftlint.yml — same as the second case.

Repro

# .swiftlint.yml
custom_rules:
  my_rule:
    name: MyRule
    regex: "TODO"
    message: "no TODOs"
$ echo '// TODO: x' > foo.swift
$ swiftlint lint --only-rule my_rule foo.swift
Done linting! Found 0 violations, 0 serious in 1 file.
$ swiftlint lint --only-rule custom_rules foo.swift
Done linting! Found 0 violations, 0 serious in 1 file.

Expected: both should report one violation.

Fix

activateCustomRuleIdentifiers now handles both .onlyConfiguration and .onlyCommandLine, and in both modes additionally enables the parent custom_rules rule when an individual custom rule is requested. Extracted as private static withCustomRulesActivated(_:allRulesWrapped:) for readability.

Tests

Tests/FrameworkTests/OnlyRuleCustomRulesTests covers the three previously broken shapes plus a regression test that --only-rule <built_in_rule> does not accidentally pull in the custom_rules parent.

@SwiftLintBot

SwiftLintBot commented May 25, 2026

Copy link
Copy Markdown
19 Messages
📖 Building this branch resulted in a binary size of 27458.29 KiB vs 27458.24 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.73 s vs 0.72 s on main (1% slower).
📖 Linting Alamofire with this PR took 1.03 s vs 1.04 s on main (0% faster).
📖 Linting Brave with this PR took 6.91 s vs 6.97 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 28.33 s vs 28.42 s on main (0% faster).
📖 Linting Firefox with this PR took 11.95 s vs 12.0 s on main (0% faster).
📖 Linting Kickstarter with this PR took 8.1 s vs 8.12 s on main (0% faster).
📖 Linting Moya with this PR took 0.41 s vs 0.42 s on main (2% faster).
📖 Linting NetNewsWire with this PR took 2.69 s vs 2.69 s on main (0% slower).
📖 Linting Nimble with this PR took 0.63 s vs 0.62 s on main (1% slower).
📖 Linting PocketCasts with this PR took 7.99 s vs 8.16 s on main (2% faster).
📖 Linting Quick with this PR took 0.4 s vs 0.4 s on main (0% slower).
📖 Linting Realm with this PR took 2.86 s vs 2.81 s on main (1% slower).
📖 Linting Sourcery with this PR took 1.75 s vs 1.78 s on main (1% faster).
📖 Linting Swift with this PR took 4.49 s vs 4.52 s on main (0% faster).
📖 Linting SwiftLintPerformanceTests with this PR took 0.31 s vs 0.28 s on main (10% slower).
📖 Linting VLC with this PR took 1.14 s vs 1.18 s on main (3% faster).
📖 Linting Wire with this PR took 18.17 s vs 18.15 s on main (0% slower).
📖 Linting WordPress with this PR took 12.21 s vs 12.24 s on main (0% faster).

Generated by 🚫 Danger

`activateCustomRuleIdentifiers` only expanded custom rules for the
config's `only_rules:` (`.onlyConfiguration`) when `custom_rules` itself
was the listed identifier — never for the `--only-rule` command-line
flag (`.onlyCommandLine`), and never for individual custom rules listed
in `only_rules:`. As a result `--only-rule custom_rules`,
`--only-rule <a_custom_rule>` and `only_rules: [<a_custom_rule>]`
silently produced no violations.

Handle `.onlyCommandLine` the same way as `.onlyConfiguration`, and in
both modes additionally enable the parent `custom_rules` rule when an
individual custom rule is requested so it can actually run.

Add `OnlyRuleCustomRulesTests` covering the three previously broken
shapes plus a regression test that `--only-rule <built_in>` does not
accidentally pull in the `custom_rules` parent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Chupik Chupik force-pushed the fix/only-rule-supports-custom-rules branch from e7f5db5 to bddee4c Compare May 25, 2026 14:33
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.

2 participants