Skip to content

[CompilerPlugin] Sink error exit logic to "message listener" #2631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Sources/SwiftCompilerPlugin/CompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ extension CompilerPlugin {
let connection = try StandardIOMessageConnection()
let provider = MacroProviderAdapter(plugin: Self())
let impl = CompilerPluginMessageListener(connection: connection, provider: provider)
do {
try impl.main()
} catch {
// Emit a diagnostic and indicate failure to the plugin host,
// and exit with an error code.
fatalError("Internal Error: \(error)")
}
impl.main()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
//===----------------------------------------------------------------------===//

#if swift(>=6)
private import _SwiftSyntaxCShims
public import SwiftSyntaxMacros
#else
@_implementationOnly import _SwiftSyntaxCShims
import SwiftSyntaxMacros
#endif

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

/// Run the main message listener loop.
/// Returns when the message connection was closed.
/// Throws an error when it failed to send/receive the message, or failed
/// to serialize/deserialize the message.
public func main() throws {
while let message = try connection.waitForNextMessage(HostToPluginMessage.self) {
let result = handler.handleMessage(message)
try connection.sendMessage(result)
///
/// On internal errors, such as I/O errors or JSON serialization errors, print
/// an error message and `exit(1)`
public func main() {
do {
while let message = try connection.waitForNextMessage(HostToPluginMessage.self) {
let result = handler.handleMessage(message)
try connection.sendMessage(result)
}
} catch {
// Emit a diagnostic and indicate failure to the plugin host,
// and exit with an error code.
fputs("Internal Error: \(error)\n", _stderr)
exit(1)
}
}
}
Expand Down