From 886c299f1dda208ceb01c8b3889040a366baead6 Mon Sep 17 00:00:00 2001 From: Anders Bertelrud Date: Wed, 21 Dec 2022 12:03:40 -0800 Subject: [PATCH] SwiftPM outputs raw diagnostics from SwiftDriver without trailing newlines 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 https://github.com/apple/swift-package-manager/issues/5968. I filed https://github.com/apple/swift-package-manager/issues/5968 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 a newline to the raw driver output, since the logic that parses JSON splits by newlines, so we won't find any terminating newlines. Note that we don't add newlines to the output coming from messages from the compiler frontend, since that output already has trailing newlines. rdar://103608636 --- Sources/Build/SwiftCompilerOutputParser.swift | 2 +- Tests/CommandsTests/BuildToolTests.swift | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/Build/SwiftCompilerOutputParser.swift b/Sources/Build/SwiftCompilerOutputParser.swift index fad652f645a..25fd712d41d 100644 --- a/Sources/Build/SwiftCompilerOutputParser.swift +++ b/Sources/Build/SwiftCompilerOutputParser.swift @@ -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) } diff --git a/Tests/CommandsTests/BuildToolTests.swift b/Tests/CommandsTests/BuildToolTests.swift index ed7314143d9..ddd6197f9cc 100644 --- a/Tests/CommandsTests/BuildToolTests.swift +++ b/Tests/CommandsTests/BuildToolTests.swift @@ -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, we only check for this if the message 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")) + } + } + } }