Skip to content

Commit

Permalink
[5.8] SwiftPM outputs raw diagnostics from SwiftDriver without traili…
Browse files Browse the repository at this point in the history
…ng newlines (swiftlang#5987)

SwiftPM uses the Swift Driver's `-parseable-output` flag to get a sequence of length-prefixed JSON-encoded messages that it can read.  Unfortunately the Swift Driver doesn't JSON-encode its own diagnostics, leading to things like swiftlang#5968.

I filed swiftlang/swift-driver#1241 on the Swift Driver to get it to encode its JSON messages, but meanwhile, it turns out that we can fairly easily fix the fallback logic that SwiftPM uses when it emits raw output.  It was simply missing a newline.  It's safe to always add one, since the logic that parses JSON splits by newlines, so we won't find any terminating newlines.

rdar://103608636
(cherry picked from commit 14d05cc)
  • Loading branch information
abertelrud committed Dec 22, 2022
1 parent 26f5077 commit 182335a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Build/SwiftCompilerOutputParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extension SwiftCompilerOutputParser: JSONMessageStreamingParserDelegate {
return
}

let message = SwiftCompilerMessage(name: "unknown", kind: .unparsableOutput(text))
let message = SwiftCompilerMessage(name: "unknown", kind: .unparsableOutput(text + "\n"))
delegate?.swiftCompilerOutputParser(self, didParse: message)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/BuildTests/SwiftCompilerOutputParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class SwiftCompilerOutputParserTests: XCTestCase {
""".utf8)
delegate.assert(messages: [
SwiftCompilerMessage(name: "unknown", kind: .unparsableOutput("2A"))
SwiftCompilerMessage(name: "unknown", kind: .unparsableOutput("2A\n"))
], errorDescription: nil)

parser.parse(bytes: """
Expand Down
12 changes: 12 additions & 0 deletions Tests/CommandsTests/BuildToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,16 @@ final class BuildToolTests: CommandsTestCase {
XCTAssertMatch(output, .prefix("digraph Jobs {"))
}
}

func testSwiftDriverRawOutputGetsNewlines() throws {
try fixture(name: "DependencyResolution/Internal/Simple") { fixturePath in
// Building with `-wmo` should result in a `remark: Incremental compilation has been disabled: it is not compatible with whole module optimization` message, which should have a trailing newline. Since that message won't be there at all when the legacy compiler driver is used, we gate this check on whether the remark is there in the first place.
let result = try execute(["-c", "release", "-Xswiftc", "-wmo"], packagePath: fixturePath)
if result.stdout.contains("remark: Incremental compilation has been disabled: it is not compatible with whole module optimization") {
XCTAssertMatch(result.stdout, .contains("optimization\n"))
XCTAssertNoMatch(result.stdout, .contains("optimization["))
XCTAssertNoMatch(result.stdout, .contains("optimizationremark"))
}
}
}
}

0 comments on commit 182335a

Please sign in to comment.