Skip to content

Commit 098dc29

Browse files
committed
[CompilerPlugin] Sink error exit logic to "message listener"
(cherry picked from commit 446ca95)
1 parent 9a5b556 commit 098dc29

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ extension CompilerPlugin {
112112
let connection = try StandardIOMessageConnection()
113113
let provider = MacroProviderAdapter(plugin: Self())
114114
let impl = CompilerPluginMessageListener(connection: connection, provider: provider)
115-
do {
116-
try impl.main()
117-
} catch {
118-
// Emit a diagnostic and indicate failure to the plugin host,
119-
// and exit with an error code.
120-
fatalError("Internal Error: \(error)")
121-
}
115+
impl.main()
122116
}
123117
}

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#if swift(>=6)
14+
private import _SwiftSyntaxCShims
1415
public import SwiftSyntaxMacros
1516
#else
17+
@_implementationOnly import _SwiftSyntaxCShims
1618
import SwiftSyntaxMacros
1719
#endif
1820

@@ -83,12 +85,20 @@ public class CompilerPluginMessageListener<Connection: MessageConnection, Provid
8385

8486
/// Run the main message listener loop.
8587
/// Returns when the message connection was closed.
86-
/// Throws an error when it failed to send/receive the message, or failed
87-
/// to serialize/deserialize the message.
88-
public func main() throws {
89-
while let message = try connection.waitForNextMessage(HostToPluginMessage.self) {
90-
let result = handler.handleMessage(message)
91-
try connection.sendMessage(result)
88+
///
89+
/// On internal errors, such as I/O errors or JSON serialization errors, print
90+
/// an error message and `exit(1)`
91+
public func main() {
92+
do {
93+
while let message = try connection.waitForNextMessage(HostToPluginMessage.self) {
94+
let result = handler.handleMessage(message)
95+
try connection.sendMessage(result)
96+
}
97+
} catch {
98+
// Emit a diagnostic and indicate failure to the plugin host,
99+
// and exit with an error code.
100+
fputs("Internal Error: \(error)\n", _stderr)
101+
exit(1)
92102
}
93103
}
94104
}

0 commit comments

Comments
 (0)