Skip to content

Commit 156500d

Browse files
committed
Avoid a roundtrip UTF-8 conversion when emitting JSON data
1 parent 4cf4358 commit 156500d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Sources/PackagePlugin/Implementation.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ extension Plugin {
6868
}
6969
}
7070

71-
// Emit the output from the plugin for SwiftPM to read.
71+
// Encoding the output struct from the plugin for SwiftPM to read.
7272
let encoder = JSONEncoder()
7373
let outputData = try! encoder.encode(output)
74+
75+
// On stdout, write a zero byte followed by the JSON data — this is what libSwiftPM expects to see. Anything before the last zero byte is treated as freeform output from the plugin (such as debug output from `print` statements). Since `FileHandle.write()` doesn't obey buffering we first have to flush any existing output.
7476
fputc(0, stdout)
75-
fputs(String(data: outputData, encoding: .utf8)!, stdout)
76-
fflush(stdout)
77+
fwrite([UInt8](outputData), 1, outputData.count, stdout)
7778
}
7879
}
7980

0 commit comments

Comments
 (0)