Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TL: Fix logs appearance from nuget client/credential provider #9407

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/MSBuild/TerminalLogger/MessageSeverity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ namespace Microsoft.Build.Logging.TerminalLogger;
/// <summary>
/// Enumerates the supported message severities.
/// </summary>
internal enum MessageSeverity { Warning, Error }
internal enum MessageSeverity { Warning, Error, Blocking }
38 changes: 36 additions & 2 deletions src/MSBuild/TerminalLogger/TerminalLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;

using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Build.Framework;
Expand All @@ -25,6 +25,9 @@ namespace Microsoft.Build.Logging.TerminalLogger;
/// </remarks>
internal sealed class TerminalLogger : INodeLogger
{
private const string FilePathPattern = " -> ";
private readonly string[] _immediateMessageMarkers = new[] { "[CredentialProvider]", "--interactive" };

/// <summary>
/// A wrapper over the project context ID passed to us in <see cref="IEventSource"/> logger events.
/// </summary>
Expand Down Expand Up @@ -549,7 +552,7 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
{
// Detect project output path by matching high-importance messages against the "$(MSBuildProjectName) -> ..."
// pattern used by the CopyFilesToOutputDirectory target.
int index = message.IndexOf(" -> ", StringComparison.Ordinal);
int index = message.IndexOf(FilePathPattern, StringComparison.Ordinal);
if (index > 0)
{
var projectFileName = Path.GetFileName(e.ProjectFile.AsSpan());
Expand All @@ -561,6 +564,12 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
project.OutputPath = outputPath;
}
}

// Detect markers that require special attention from a customer.
if (_immediateMessageMarkers.Any(marker => message.IndexOf(marker, StringComparison.Ordinal) > 0))
{
RenderImmediateMessage(message, MessageSeverity.Blocking);
}
}
}

Expand All @@ -586,6 +595,11 @@ private void WarningRaised(object sender, BuildWarningEventArgs e)
threadId: e.ThreadId,
logOutputProperties: null);

if (_immediateMessageMarkers.Any(marker => message.IndexOf(marker, StringComparison.Ordinal) > 0))
{
RenderImmediateMessage(message, MessageSeverity.Warning);
}

project.AddBuildMessage(MessageSeverity.Warning, message);
}
}
Expand Down Expand Up @@ -848,6 +862,26 @@ private string RenderBuildResult(bool succeeded, bool hasError, bool hasWarning)
}
}

/// <summary>
/// Print a build messages to the output that require special customer's attention.
/// </summary>
/// <param name="message">Build message needed to be shown immediately.</param>
/// <param name="severity">Message severity.</param>
private void RenderImmediateMessage(string message, MessageSeverity severity)
{
string styledMessage = severity switch
{
MessageSeverity.Warning => AnsiCodes.Colorize(message, TerminalColor.Yellow),
MessageSeverity.Blocking => AnsiCodes.Colorize(message, TerminalColor.Blue),
_ => string.Empty,
};

if (!string.IsNullOrEmpty(styledMessage))
{
Terminal.WriteLine(styledMessage);
}
}

/// <summary>
/// Returns the <see cref="_nodes"/> index corresponding to the given <see cref="BuildEventContext"/>.
/// </summary>
Expand Down