From 1170ac47451a8360fce7e3d7daeb13042b787051 Mon Sep 17 00:00:00 2001 From: Rainer Sigwald Date: Thu, 13 Apr 2023 12:11:52 -0500 Subject: [PATCH 1/3] Emit relative path to output --- src/MSBuild/LiveLogger/LiveLogger.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/MSBuild/LiveLogger/LiveLogger.cs b/src/MSBuild/LiveLogger/LiveLogger.cs index 5bcb21138d1..0d7e59bc445 100644 --- a/src/MSBuild/LiveLogger/LiveLogger.cs +++ b/src/MSBuild/LiveLogger/LiveLogger.cs @@ -81,6 +81,11 @@ public override string ToString() /// private DateTime _buildStartTime; + /// + /// The working directory when the build starts, to trim relative output paths. + /// + private readonly string _initialWorkingDirectory = Environment.CurrentDirectory; + /// /// True if the build has encountered at least one error. /// @@ -468,6 +473,12 @@ private void MessageRaised(object sender, BuildMessageEventArgs e) _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project)) { ReadOnlyMemory outputPath = e.Message.AsMemory().Slice(index + 4); + + if (outputPath.Span.Slice(0, _initialWorkingDirectory.Length).SequenceEqual(_initialWorkingDirectory.AsSpan())) + { + outputPath = outputPath.Slice(_initialWorkingDirectory.Length + 1); + } + project.OutputPath = outputPath; } } From dc86d5346c9286addb17d05049311c9e4a747df4 Mon Sep 17 00:00:00 2001 From: Rainer Sigwald Date: Mon, 24 Apr 2023 09:36:31 -0500 Subject: [PATCH 2/3] Case-insensitive comparison and fix links --- src/MSBuild/LiveLogger/LiveLogger.cs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/MSBuild/LiveLogger/LiveLogger.cs b/src/MSBuild/LiveLogger/LiveLogger.cs index 0d7e59bc445..3701ea029c3 100644 --- a/src/MSBuild/LiveLogger/LiveLogger.cs +++ b/src/MSBuild/LiveLogger/LiveLogger.cs @@ -355,7 +355,8 @@ private void ProjectFinished(object sender, ProjectFinishedEventArgs e) // Print the output path as a link if we have it. if (outputPath is not null) { - ReadOnlySpan url = outputPath.Value.Span; + ReadOnlySpan outputPathSpan = outputPath.Value.Span; + ReadOnlySpan url = outputPathSpan; try { // If possible, make the link point to the containing directory of the output. @@ -365,10 +366,20 @@ private void ProjectFinished(object sender, ProjectFinishedEventArgs e) { // Ignore any GetDirectoryName exceptions. } + + // If the output path is under the initial working directory, make the console output relative to that to save space. + if (outputPathSpan.StartsWith(_initialWorkingDirectory.AsSpan(), FileUtilities.PathComparison) + && (outputPathSpan[_initialWorkingDirectory.Length] == Path.DirectorySeparatorChar + || outputPathSpan[_initialWorkingDirectory.Length] == Path.AltDirectorySeparatorChar)) + { + outputPathSpan = outputPathSpan.Slice(_initialWorkingDirectory.Length + 1); + } + + #if NETCOREAPP - Terminal.WriteLine($" ({duration:F1}s) → {AnsiCodes.LinkPrefix}{url}{AnsiCodes.LinkInfix}{outputPath}{AnsiCodes.LinkSuffix}"); + Terminal.WriteLine($" ({duration:F1}s) → {AnsiCodes.LinkPrefix}{url}{AnsiCodes.LinkInfix}{outputPathSpan}{AnsiCodes.LinkSuffix}"); #else - Terminal.WriteLine($" ({duration:F1}s) → {AnsiCodes.LinkPrefix}{url.ToString()}{AnsiCodes.LinkInfix}{outputPath.ToString()}{AnsiCodes.LinkSuffix}"); + Terminal.WriteLine($" ({duration:F1}s) → {AnsiCodes.LinkPrefix}{url.ToString()}{AnsiCodes.LinkInfix}{outputPathSpan.ToString()}{AnsiCodes.LinkSuffix}"); #endif } else @@ -473,12 +484,6 @@ private void MessageRaised(object sender, BuildMessageEventArgs e) _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project)) { ReadOnlyMemory outputPath = e.Message.AsMemory().Slice(index + 4); - - if (outputPath.Span.Slice(0, _initialWorkingDirectory.Length).SequenceEqual(_initialWorkingDirectory.AsSpan())) - { - outputPath = outputPath.Slice(_initialWorkingDirectory.Length + 1); - } - project.OutputPath = outputPath; } } From 750d47a1050e7818115a95ec3847b40b2b947f9e Mon Sep 17 00:00:00 2001 From: Rainer Sigwald Date: Tue, 25 Apr 2023 11:02:39 -0500 Subject: [PATCH 3/3] Guard against short output paths --- src/MSBuild/LiveLogger/LiveLogger.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/MSBuild/LiveLogger/LiveLogger.cs b/src/MSBuild/LiveLogger/LiveLogger.cs index 3701ea029c3..0f9fce02b25 100644 --- a/src/MSBuild/LiveLogger/LiveLogger.cs +++ b/src/MSBuild/LiveLogger/LiveLogger.cs @@ -368,14 +368,16 @@ private void ProjectFinished(object sender, ProjectFinishedEventArgs e) } // If the output path is under the initial working directory, make the console output relative to that to save space. - if (outputPathSpan.StartsWith(_initialWorkingDirectory.AsSpan(), FileUtilities.PathComparison) - && (outputPathSpan[_initialWorkingDirectory.Length] == Path.DirectorySeparatorChar - || outputPathSpan[_initialWorkingDirectory.Length] == Path.AltDirectorySeparatorChar)) + if (outputPathSpan.StartsWith(_initialWorkingDirectory.AsSpan(), FileUtilities.PathComparison)) { - outputPathSpan = outputPathSpan.Slice(_initialWorkingDirectory.Length + 1); + if (outputPathSpan.Length > _initialWorkingDirectory.Length + && (outputPathSpan[_initialWorkingDirectory.Length] == Path.DirectorySeparatorChar + || outputPathSpan[_initialWorkingDirectory.Length] == Path.AltDirectorySeparatorChar)) + { + outputPathSpan = outputPathSpan.Slice(_initialWorkingDirectory.Length + 1); + } } - #if NETCOREAPP Terminal.WriteLine($" ({duration:F1}s) → {AnsiCodes.LinkPrefix}{url}{AnsiCodes.LinkInfix}{outputPathSpan}{AnsiCodes.LinkSuffix}"); #else