Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 422950b

Browse files
committed
Relax SwiftLint version logic to fallback to latest version
1 parent 065876e commit 422950b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Package.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
// See https://github.com/erikdoe/ocmock/issues/500#issuecomment-1002700625
1616
.package(url: "https://github.com/erikdoe/ocmock", revision: "afd2c6924e8a36cb872bc475248b978f743c6050"),
1717
.package(url: "https://github.com/Quick/Quick", from: "6.0.0"),
18-
.package(url: "https://github.com/realm/SwiftLint", .exactItem(loadSwiftLintVersion()))
18+
swiftLintPlugin()
1919
],
2020
targets: [
2121
.target(
@@ -62,25 +62,42 @@ let package = Package(
6262
]
6363
)
6464

65-
func loadSwiftLintVersion() -> Version {
65+
func swiftLintPlugin() -> PackageDescription.Package.Dependency {
66+
let url = "https://github.com/realm/SwiftLint"
67+
68+
// Swift can access and read the config file with the current logic only within the package repo.
69+
// This is, the check will fail when 'swift build' attempts to resolve the package as a dependency.
70+
// To workaround this limitation, we fall back to the latest version of the plugin when the config file read fails.
71+
// This should be safe to do, given the SwiftLint plugin is only used in development.
72+
guard let version = loadSwiftLintVersion() else {
73+
return .package(url: url, from: "0.54.0")
74+
}
75+
76+
return .package(url: url, .exactItem(version))
77+
}
78+
79+
func loadSwiftLintVersion() -> Version? {
6680
let swiftLintConfigURL = URL(fileURLWithPath: #file)
6781
.deletingLastPathComponent()
6882
.appendingPathComponent(".swiftlint.yml")
6983

7084
guard let yamlString = try? String(contentsOf: swiftLintConfigURL) else {
71-
fatalError("Failed to read SwiftLint config file at \(swiftLintConfigURL).")
85+
print("[!] Failed to read SwiftLint config file at \(swiftLintConfigURL).")
86+
return .none
7287
}
7388

7489
guard let versionLine = yamlString.components(separatedBy: .newlines)
7590
.first(where: { $0.contains("swiftlint_version") }) else {
76-
fatalError("SwiftLint version not found in YAML file.")
91+
print("[!] SwiftLint version not found in YAML file.")
92+
return .none
7793
}
7894

7995
// Assumes the format `swiftlint_version: <version>`
8096
guard let version = Version(versionLine.components(separatedBy: ":")
8197
.last?
8298
.trimmingCharacters(in: .whitespaces) ?? "") else {
83-
fatalError("Failed to extract SwiftLint version.")
99+
print("[!] Failed to extract SwiftLint version.")
100+
return .none
84101
}
85102

86103
return version

0 commit comments

Comments
 (0)