diff --git a/src/MSBuild/LiveLogger/LiveLogger.cs b/src/MSBuild/LiveLogger/LiveLogger.cs
index 7772b29a753..3052a9fa792 100644
--- a/src/MSBuild/LiveLogger/LiveLogger.cs
+++ b/src/MSBuild/LiveLogger/LiveLogger.cs
@@ -92,6 +92,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.
///
@@ -366,7 +371,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.
@@ -384,8 +390,19 @@ private void ProjectFinished(object sender, ProjectFinishedEventArgs e)
urlString = uri.AbsoluteUri;
}
+ // 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))
+ {
+ if (outputPathSpan.Length > _initialWorkingDirectory.Length
+ && (outputPathSpan[_initialWorkingDirectory.Length] == Path.DirectorySeparatorChar
+ || outputPathSpan[_initialWorkingDirectory.Length] == Path.AltDirectorySeparatorChar))
+ {
+ outputPathSpan = outputPathSpan.Slice(_initialWorkingDirectory.Length + 1);
+ }
+ }
+
Terminal.WriteLine(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ProjectFinished_OutputPath",
- $"{AnsiCodes.LinkPrefix}{urlString}{AnsiCodes.LinkInfix}{outputPath}{AnsiCodes.LinkSuffix}"));
+ $"{AnsiCodes.LinkPrefix}{urlString}{AnsiCodes.LinkInfix}{outputPathSpan.ToString()}{AnsiCodes.LinkSuffix}"));
}
else
{