Skip to content

Commit 8978ae2

Browse files
authored
Add --disable-sourcekit flag to disable SourceKit (#6439)
1 parent faa5859 commit 8978ae2

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
### Enhancements
1414

15-
* None.
15+
* Add a `--disable-sourcekit` flag to the `lint` command to disable SourceKit when needed.
16+
The environment variable `SWIFTLINT_DISABLE_SOURCEKIT` can still be used as well.
17+
[SimplyDanny](https://github.com/SimplyDanny)
18+
[#6282](https://github.com/realm/SwiftLint/issues/6282)
1619

1720
### Bug Fixes
1821

Source/SwiftLintCore/Extensions/Request+SwiftLint.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import Foundation
22
import SourceKittenFramework
33

44
public extension Request {
5-
static let disableSourceKit = {
5+
nonisolated(unsafe) static var disableSourceKitOverride = false
6+
7+
static var disableSourceKit: Bool {
68
#if SWIFTLINT_DISABLE_SOURCEKIT
79
// Compile-time
810
true
911
#else
1012
// Runtime
11-
ProcessInfo.processInfo.environment["SWIFTLINT_DISABLE_SOURCEKIT"] != nil
13+
ProcessInfo.processInfo.environment["SWIFTLINT_DISABLE_SOURCEKIT"] != nil || disableSourceKitOverride
1214
#endif
13-
}()
15+
}
1416

1517
func sendIfNotDisabled() throws -> [String: any SourceKitRepresentable] {
1618
// Skip safety checks if explicitly allowed (e.g., for testing or specific operations)
@@ -45,7 +47,7 @@ public extension Request {
4547
}
4648

4749
guard !Self.disableSourceKit else {
48-
queuedFatalError("SourceKit is disabled by `SWIFTLINT_DISABLE_SOURCEKIT`.")
50+
queuedFatalError("SourceKit is disabled by configuration.")
4951
}
5052
return try send()
5153
}

Source/SwiftLintFramework/LintOrAnalyzeCommand.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44
import Dispatch
55
import Foundation
6+
import SourceKittenFramework
67

78
// swiftlint:disable file_length
89

@@ -53,6 +54,7 @@ package struct LintOrAnalyzeOptions {
5354
let onlyRule: [String]
5455
let autocorrect: Bool
5556
let format: Bool
57+
let disableSourceKit: Bool
5658
let compilerLogPath: String?
5759
let compileCommands: String?
5860
let checkForUpdates: Bool
@@ -81,6 +83,7 @@ package struct LintOrAnalyzeOptions {
8183
onlyRule: [String],
8284
autocorrect: Bool,
8385
format: Bool,
86+
disableSourceKit: Bool,
8487
compilerLogPath: String?,
8588
compileCommands: String?,
8689
checkForUpdates: Bool) {
@@ -108,6 +111,7 @@ package struct LintOrAnalyzeOptions {
108111
self.onlyRule = onlyRule
109112
self.autocorrect = autocorrect
110113
self.format = format
114+
self.disableSourceKit = disableSourceKit
111115
self.compilerLogPath = compilerLogPath
112116
self.compileCommands = compileCommands
113117
self.checkForUpdates = checkForUpdates
@@ -124,6 +128,7 @@ package struct LintOrAnalyzeOptions {
124128

125129
package struct LintOrAnalyzeCommand {
126130
package static func run(_ options: LintOrAnalyzeOptions) async throws {
131+
Request.disableSourceKitOverride = options.mode == .lint && options.disableSourceKit
127132
if let workingDirectory = options.workingDirectory {
128133
if !FileManager.default.changeCurrentDirectoryPath(workingDirectory) {
129134
throw SwiftLintError.usageError(

Source/swiftlint/Commands/Analyze.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extension SwiftLint {
4444
onlyRule: common.onlyRule,
4545
autocorrect: common.fix,
4646
format: common.format,
47+
disableSourceKit: false,
4748
compilerLogPath: compilerLogPath,
4849
compileCommands: compileCommands,
4950
checkForUpdates: common.checkForUpdates

Source/swiftlint/Commands/Lint.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ extension SwiftLint {
1919
var noCache = false
2020
@Flag(help: "Run all rules, even opt-in and disabled ones, ignoring `only_rules`.")
2121
var enableAllRules = false
22+
@Flag(
23+
name: .customLong("disable-sourcekit"),
24+
help: "Do not dynamically load SourceKit at runtime. Skip and report rules that require it."
25+
)
26+
var disableSourceKit = false
2227
@Argument(help: pathsArgumentDescription(for: .lint))
2328
var paths = [String]()
2429

@@ -56,6 +61,7 @@ extension SwiftLint {
5661
onlyRule: common.onlyRule,
5762
autocorrect: common.fix,
5863
format: common.format,
64+
disableSourceKit: disableSourceKit,
5965
compilerLogPath: nil,
6066
compileCommands: nil,
6167
checkForUpdates: common.checkForUpdates

Tests/FrameworkTests/LintOrAnalyzeOptionsTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private extension LintOrAnalyzeOptions {
6464
onlyRule: [],
6565
autocorrect: false,
6666
format: false,
67+
disableSourceKit: false,
6768
compilerLogPath: nil,
6869
compileCommands: nil,
6970
checkForUpdates: false

0 commit comments

Comments
 (0)