Skip to content

Commit dae45ee

Browse files
committed
[Windows] Speculative fix
(cherry picked from commit b3494aa)
1 parent 3e8e2ef commit dae45ee

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,34 @@ extension CompilerPlugin {
115115
/// Main entry point of the plugin — sets up a communication channel with
116116
/// the plugin host and runs the main message loop.
117117
public static func main() throws {
118+
let _stdin = _ss_stdin()
119+
let _stdout = _ss_stdout()
120+
let _stderr = _ss_stderr()
121+
118122
// Duplicate the `stdin` file descriptor, which we will then use for
119123
// receiving messages from the plugin host.
120-
let inputFD = dup(fileno(_ss_stdin()))
124+
let inputFD = dup(fileno(_stdin))
121125
guard inputFD >= 0 else {
122126
internalError("Could not duplicate `stdin`: \(describe(errno: _ss_errno())).")
123127
}
124128

125129
// Having duplicated the original standard-input descriptor, we close
126130
// `stdin` so that attempts by the plugin to read console input (which
127131
// are usually a mistake) return errors instead of blocking.
128-
guard close(fileno(_ss_stdin())) >= 0 else {
132+
guard close(fileno(_stdin)) >= 0 else {
129133
internalError("Could not close `stdin`: \(describe(errno: _ss_errno())).")
130134
}
131135

132136
// Duplicate the `stdout` file descriptor, which we will then use for
133137
// sending messages to the plugin host.
134-
let outputFD = dup(fileno(_ss_stdout()))
138+
let outputFD = dup(fileno(_stdout))
135139
guard outputFD >= 0 else {
136140
internalError("Could not dup `stdout`: \(describe(errno: _ss_errno())).")
137141
}
138142

139143
// Having duplicated the original standard-output descriptor, redirect
140144
// `stdout` to `stderr` so that all free-form text output goes there.
141-
guard dup2(fileno(_ss_stderr()), fileno(_ss_stdout())) >= 0 else {
145+
guard dup2(fileno(_stderr), fileno(_stdout)) >= 0 else {
142146
internalError("Could not dup2 `stdout` to `stderr`: \(describe(errno: _ss_errno())).")
143147
}
144148

@@ -148,9 +152,9 @@ extension CompilerPlugin {
148152
// buffer. As a result, on Windows, we completely disable all
149153
// buffering, which means that partial writes are possible.
150154
#if os(Windows)
151-
setvbuf(_ss_stdout(), nil, _IONBF, 0)
155+
setvbuf(_stdout, nil, _IONBF, 0)
152156
#else
153-
setvbuf(_ss_stdout(), nil, _IOLBF, 0)
157+
setvbuf(_stdout, nil, _IOLBF, 0)
154158
#endif
155159

156160
// Open a message channel for communicating with the plugin host.
@@ -242,7 +246,7 @@ private func describe(errno: CInt) -> String {
242246
private func _write(_ stream: _ss_ptr_FILE, contentsOf buffer: UnsafeRawBufferPointer) throws {
243247
let result = fwrite(buffer.baseAddress, 1, buffer.count, stream)
244248
if result < buffer.count {
245-
throw CompilerPluginError(message: "write(3) failed: \(describe(errno: _ss_errno()))")
249+
throw CompilerPluginError(message: "fwrite(3) failed: \(describe(errno: _ss_errno()))")
246250
}
247251
}
248252

@@ -259,7 +263,7 @@ private func _reading<T>(_ stream: _ss_ptr_FILE, count: Int, _ fn: (UnsafeRawBuf
259263
// Input is closed.
260264
return try fn(UnsafeRawBufferPointer(start: nil, count: 0))
261265
} else {
262-
throw CompilerPluginError(message: "read(2) failed: \(describe(errno: _ss_errno()))")
266+
throw CompilerPluginError(message: "fread(3) failed: \(describe(errno: _ss_errno()))")
263267
}
264268
}
265269
return try fn(UnsafeRawBufferPointer(buffer))

0 commit comments

Comments
 (0)