Skip to content

Commit dd157e2

Browse files
authored
Use task-local variable (#5919)
1 parent 1d8af83 commit dd157e2

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

Source/SwiftLintCore/Models/Issue.swift

+7-12
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ public enum Issue: LocalizedError, Equatable {
7979
case baselineNotReadable(path: String)
8080

8181
/// Flag to enable warnings for deprecations being printed to the console. Printing is enabled by default.
82-
#if compiler(>=6.0)
8382
package nonisolated(unsafe) static var printDeprecationWarnings = true
84-
#else
85-
package static var printDeprecationWarnings = true
86-
#endif
83+
84+
@TaskLocal private static var messageConsumer: (@Sendable (String) -> Void)?
8785

8886
/// Hook used to capture all messages normally printed to stdout and return them back to the caller.
8987
///
@@ -97,20 +95,17 @@ public enum Issue: LocalizedError, Equatable {
9795
actor Console {
9896
static var content = ""
9997
}
100-
messageConsumer = {
101-
Console.content += (Console.content.isEmpty ? "" : "\n") + $0
102-
}
10398
defer {
104-
messageConsumer = nil
10599
Console.content = ""
106100
}
107-
try runner()
101+
try $messageConsumer.withValue(
102+
{ Console.content += (Console.content.isEmpty ? "" : "\n") + $0 },
103+
operation: runner
104+
)
108105
await Task.yield()
109106
return Console.content
110107
}
111108

112-
@MainActor private static var messageConsumer: (@Sendable (String) -> Void)?
113-
114109
/// Wraps any `Error` into a `SwiftLintError.genericWarning` if it is not already a `SwiftLintError`.
115110
///
116111
/// - parameter error: Any `Error`.
@@ -142,7 +137,7 @@ public enum Issue: LocalizedError, Equatable {
142137
if case .ruleDeprecated = self, !Self.printDeprecationWarnings {
143138
return
144139
}
145-
Task { @MainActor in
140+
Task(priority: .high) { @MainActor in
146141
if let consumer = Self.messageConsumer {
147142
consumer(errorDescription)
148143
} else {

0 commit comments

Comments
 (0)