Skip to content

Commit 41e9701

Browse files
authored
[5.6] Fix an ordering issue of a print statement and a diagnostic arriving right at the exit of a plugin (#3964) (#3966)
There's no strong ordering between the free form output of a plugin and the diagnostics it emits, but for things like thrown exceptions etc, it makes a bit more sense to show the diagnostic last. Perhaps the test shouldn't really depend on the order here — we will see if this addresses the issue in the CI (it seems specific to Linux and I was never able to reproduce it in Docker). I also made the unit test text a little less whimsical, and got rid of the emoji. rdar://86729106 (cherry picked from commit af43c15)
1 parent 048cb2a commit 41e9701

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

Sources/Workspace/DefaultPluginScriptRunner.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,12 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
485485
// Close the output handle through which we talked to the plugin.
486486
try? outputHandle.close()
487487

488-
// Read and pass on any remaining messages from the plugin.
489-
stdoutPipe.fileHandleForReading.readabilityHandler?(stdoutPipe.fileHandleForReading)
490-
491488
// Read and pass on any remaining free-form text output from the plugin.
492489
stderrPipe.fileHandleForReading.readabilityHandler?(stderrPipe.fileHandleForReading)
493490

491+
// Read and pass on any remaining messages from the plugin.
492+
stdoutPipe.fileHandleForReading.readabilityHandler?(stdoutPipe.fileHandleForReading)
493+
494494
// Call the completion block with a result that depends on how the process ended.
495495
callbackQueue.async {
496496
completion(Result {

Tests/FunctionalTests/PluginTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ class PluginTests: XCTestCase {
268268
arguments: [String]
269269
) throws {
270270
// Print some output that should appear before the error diagnostic.
271-
print("This text should appear before the error.")
271+
print("This text should appear before the uncaught thrown error.")
272272
273273
// Throw an uncaught error that should be reported as a diagnostics.
274-
throw "Houston, we have a problem."
274+
throw "This is the uncaught thrown error."
275275
}
276276
}
277277
extension String: Error { }
@@ -372,7 +372,7 @@ class PluginTests: XCTestCase {
372372
// Add each line of emitted output as a `.info` diagnostic.
373373
dispatchPrecondition(condition: .onQueue(delegateQueue))
374374
let textlines = String(decoding: data, as: UTF8.self).split(separator: "\n")
375-
print(textlines.map{ "🧩 \($0)" }.joined(separator: "\n"))
375+
print(textlines.map{ "[TEXT] \($0)" }.joined(separator: "\n"))
376376
diagnostics.append(contentsOf: textlines.map{
377377
Basics.Diagnostic(severity: .info, message: String($0), metadata: .none)
378378
})
@@ -381,6 +381,7 @@ class PluginTests: XCTestCase {
381381
func pluginEmittedDiagnostic(_ diagnostic: Basics.Diagnostic) {
382382
// Add the diagnostic as-is.
383383
dispatchPrecondition(condition: .onQueue(delegateQueue))
384+
print("[DIAG] \(diagnostic)")
384385
diagnostics.append(diagnostic)
385386
}
386387
}
@@ -453,8 +454,8 @@ class PluginTests: XCTestCase {
453454

454455
// Invoke the command plugin that throws an unhandled error at the top level.
455456
testCommand(package: package, plugin: "PluginFailingWithError", targets: [], arguments: [], expectFailure: true) { output in
456-
output.check(diagnostic: .equal("This text should appear before the error."), severity: .info)
457-
output.check(diagnostic: .equal("Houston, we have a problem."), severity: .error)
457+
output.check(diagnostic: .equal("This text should appear before the uncaught thrown error."), severity: .info)
458+
output.check(diagnostic: .equal("This is the uncaught thrown error."), severity: .error)
458459

459460
}
460461
// Invoke the command plugin that exits with code 1 without returning an error.

0 commit comments

Comments
 (0)