Fix --only-rule to run custom rules#6664
Open
Chupik wants to merge 1 commit into
Open
Conversation
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>
e7f5db5 to
bddee4c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduction
We noticed that SwiftLint in some cases ignores
--only-ruleparam. We finally reproduced the problem and used an LLM to investigate and fix it. The explanation below was drafted with LLM assistance.Problem
activateCustomRuleIdentifiersonly expanded custom rules for the config'sonly_rules:(.onlyConfiguration), and even there only whencustom_rulesitself was the listed identifier. It never expanded for the--only-rulecommand-line flag (.onlyCommandLine), and never for individual custom rules inonly_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 ofcustomRuleConfigurations.swiftlint lint --only-rule <a_custom_rule>— the custom rule's identifier is recognized as valid, but the parentcustom_rulesrule isn't enabled, so the rule never executes.only_rules: [<a_custom_rule>]in.swiftlint.yml— same as the second case.Repro
Expected: both should report one violation.
Fix
activateCustomRuleIdentifiersnow handles both.onlyConfigurationand.onlyCommandLine, and in both modes additionally enables the parentcustom_rulesrule when an individual custom rule is requested. Extracted asprivate static withCustomRulesActivated(_:allRulesWrapped:)for readability.Tests
Tests/FrameworkTests/OnlyRuleCustomRulesTestscovers the three previously broken shapes plus a regression test that--only-rule <built_in_rule>does not accidentally pull in thecustom_rulesparent.