Skip to content

Commit 3b36bde

Browse files
committed
[CompilerPlugin] Use fread(3)/fwrite(3) instead of read(2)/write(2)
Speculative fix for Windows
1 parent 10cc964 commit 3b36bde

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ extension CompilerPlugin {
155155

156156
// Open a message channel for communicating with the plugin host.
157157
let connection = PluginHostConnection(
158-
inputStream: inputFD,
159-
outputStream: outputFD
158+
inputStream: fdopen(inputFD, "r"),
159+
outputStream: fdopen(outputFD, "w")
160160
)
161161

162162
// Handle messages from the host until the input stream is closed,
@@ -181,8 +181,8 @@ extension CompilerPlugin {
181181
}
182182

183183
internal struct PluginHostConnection: MessageConnection {
184-
fileprivate let inputStream: CInt
185-
fileprivate let outputStream: CInt
184+
fileprivate let inputStream: _ss_ptr_FILE
185+
fileprivate let outputStream: _ss_ptr_FILE
186186

187187
func sendMessage<TX: Encodable>(_ message: TX) throws {
188188
// Encode the message as JSON.
@@ -200,6 +200,8 @@ internal struct PluginHostConnection: MessageConnection {
200200
try payload.withUnsafeBytes { buffer in
201201
try _write(outputStream, contentsOf: buffer)
202202
}
203+
204+
fflush(outputStream)
203205
}
204206

205207
func waitForNextMessage<RX: Decodable>(_ ty: RX.Type) throws -> RX? {
@@ -237,41 +239,27 @@ private func describe(errno: CInt) -> String {
237239
return String(describing: errno)
238240
}
239241

240-
private func _write(_ fd: CInt, contentsOf buffer: UnsafeRawBufferPointer) throws {
241-
var ptr = buffer.baseAddress!
242-
var remaining = buffer.count
243-
while remaining > 0 {
244-
switch write(fd, ptr, numericCast(remaining)) {
245-
case 0:
246-
throw CompilerPluginError(message: "write(2) closed")
247-
case -1:
248-
throw CompilerPluginError(message: "read(2) failed: \(describe(errno: _ss_errno()))")
249-
case let result:
250-
ptr += Int(result)
251-
remaining -= Int(result)
252-
}
242+
private func _write(_ stream: _ss_ptr_FILE, contentsOf buffer: UnsafeRawBufferPointer) throws {
243+
let result = fwrite(buffer.baseAddress, 1, buffer.count, stream)
244+
if result < buffer.count {
245+
throw CompilerPluginError(message: "write(3) failed: \(describe(errno: _ss_errno()))")
253246
}
254247
}
255248

256-
private func _reading<T>(_ fd: CInt, count: Int, _ fn: (UnsafeRawBufferPointer) throws -> T) throws -> T {
249+
private func _reading<T>(_ stream: _ss_ptr_FILE, count: Int, _ fn: (UnsafeRawBufferPointer) throws -> T) throws -> T {
257250
guard count > 0 else {
258251
return try fn(UnsafeRawBufferPointer(start: nil, count: 0))
259252
}
260253
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: count, alignment: 1)
261254
defer { buffer.deallocate() }
262255

263-
var ptr = buffer.baseAddress!
264-
var remaining = buffer.count
265-
while remaining > 0 {
266-
switch read(fd, ptr, numericCast(remaining)) {
267-
case 0:
256+
let result = fread(buffer.baseAddress, 1, count, stream)
257+
if result < count {
258+
if ferror(stream) == 0 {
268259
// Input is closed.
269260
return try fn(UnsafeRawBufferPointer(start: nil, count: 0))
270-
case -1:
261+
} else {
271262
throw CompilerPluginError(message: "read(2) failed: \(describe(errno: _ss_errno()))")
272-
case let result:
273-
ptr += Int(result)
274-
remaining -= Int(result)
275263
}
276264
}
277265
return try fn(UnsafeRawBufferPointer(buffer))

0 commit comments

Comments
 (0)