From 660b9a1fa789440aa1cb94caa0e58e3416d80fcb Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 3 Apr 2025 16:28:47 +0100 Subject: [PATCH] [Windows] Remove extra newlines from diagnostic output. We are outputting lots of extra newlines on Windows, which makes it hard to read the diagnostics. rdar://148520063 --- .../DiagnosticsFormatter.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftDiagnostics/DiagnosticsFormatter.swift b/Sources/SwiftDiagnostics/DiagnosticsFormatter.swift index 28f4d18bf4e..9717ecb4ac7 100644 --- a/Sources/SwiftDiagnostics/DiagnosticsFormatter.swift +++ b/Sources/SwiftDiagnostics/DiagnosticsFormatter.swift @@ -293,10 +293,13 @@ public struct DiagnosticsFormatter { ) ) - // If the line did not end with \n (e.g. the last line), append it manually - if annotatedSource.last != "\n" { - annotatedSource.append("\n") + // Remove any trailing newline and replace it; this may seem + // counterintuitive, but if we're running within CMake and we let a + // '\r\n' through, CMake will turn that into *two* newlines. + if let last = annotatedSource.last, last.isNewline { + annotatedSource.removeLast() } + annotatedSource.append("\n") let columnsWithDiagnostics = Set( annotatedLine.diagnostics.map { @@ -331,8 +334,13 @@ public struct DiagnosticsFormatter { } // Add suffix text. - annotatedSource.append(annotatedLine.suffixText) - if annotatedSource.last != "\n" { + if !annotatedLine.suffixText.isEmpty { + annotatedSource.append(annotatedLine.suffixText) + + // See above for an explanation of why we do this + if let last = annotatedSource.last, last.isNewline { + annotatedSource.removeLast() + } annotatedSource.append("\n") } }