Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/MSBuild/LiveLogger/LiveLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public override string ToString()
/// </summary>
private DateTime _buildStartTime;

/// <summary>
/// The working directory when the build starts, to trim relative output paths.
/// </summary>
private readonly string _initialWorkingDirectory = Environment.CurrentDirectory;

/// <summary>
/// True if the build has encountered at least one error.
/// </summary>
Expand Down Expand Up @@ -350,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<char> url = outputPath.Value.Span;
ReadOnlySpan<char> outputPathSpan = outputPath.Value.Span;
ReadOnlySpan<char> url = outputPathSpan;
try
{
// If possible, make the link point to the containing directory of the output.
Expand All @@ -360,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible for outputPathSpan to be the same as _initialWorkingDirectory to cause a crash here, is it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Not normally, but we can't control what projects might emit, so we should definitely guard against this.

|| 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
Expand Down