Open
Description
New Issue Checklist
- I've Updated SwiftLint to the latest version.
- I've searched for existing GitHub issues.
Bug Description
Given two custom rules, named aaa
and bbb
, with regex
s matching their names (config follows below), and the following input:
let a = "aaa"
let b = "bbb"
and the following command:
$ swiftlint --quiet --config testconfig.yml test.swift --no-cache
The order that the violations are reported in is not deterministic:
% swiftlint --quiet --config testconfig.yml test.swift --no-cache
/Users/someone/Documents/Source/Scratch/test.swift:2:10: error: bbb Violation: No bbb allowed (bbb)
/Users/someone/Documents/Source/Scratch/test.swift:1:10: error: aaa Violation: No aaa allowed (aaa)
% swiftlint --quiet --config testconfig2.yml t3.swift --no-cache
/Users/someone/Documents/Source/Scratch/test.swift:1:10: error: aaa Violation: No aaa allowed (aaa)
/Users/someone/Documents/Source/Scratch/test.swift:2:10: error: bbb Violation: No bbb allowed (bbb)
It looks like the root of the problem is that custom_rules
are stored in a dictionary, so the ordering from the configuration files is lost.
In CustomRules.swift
, we build up the list of custom rules by iterating over a dictionary:
mutating func apply(configuration: Any) throws {
guard let configurationDict = configuration as? [String: Any] else {
throw Issue.invalidConfiguration(ruleID: Parent.identifier)
}
for (key, value) in configurationDict {
var ruleConfiguration = RegexConfiguration<Parent>(identifier: key)
do {
try ruleConfiguration.apply(configuration: value)
} catch {
Issue.invalidConfiguration(ruleID: key).print()
continue
}
customRuleConfigurations.append(ruleConfiguration)
}
}
We should probably sort by name here, to match the way other rules are treated, which is a bit weird.
Environment
- SwiftLint version (run
swiftlint version
to be sure)
0.59.1
- Xcode version (run
xcodebuild -version
to be sure)
Xcode 16.1
Build version 16B40
- Installation method used (Homebrew, CocoaPods, building from source, etc)
homebrew
- Configuration file:
only_rules:
- custom_rules
custom_rules:
aaa:
included: ".*.swift"
name: aaa
message: No aaa allowed
regex: aaa
severity: error
bbb:
included: ".*.swift"
name: bbb
message: No bbb allowed
regex: bbb
severity: error
Are you using nested configurations?
No