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

Fix missed errors appearance on restore #9424

Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<None Update="Snapshots\TerminalLogger_Tests.PrintRestore_Failed.Linux.verified.txt">
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
<None Update="Snapshots\TerminalLogger_Tests.PrintRestore_SuccessWithWarnings.OSX.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
<None Update="Snapshots\TerminalLogger_Tests.PrintRestore_Failed.OSX.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
<None Update="Snapshots\TerminalLogger_Tests.PrintRestore_SuccessWithWarnings.Linux.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
directory/file(1,2,3,4): error AA0000: Restore Failed

Build failed with errors in 0.0s
]9;4;0;\
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
directory/file(1,2,3,4): error AA0000: Restore Failed

Build failed with errors in 0.0s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
directory/file(1,2,3,4): error AA0000: Restore Failed

Build failed with errors in 0.0s
]9;4;0;\
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
directory/file(1,2,3,4): warning AA0000: Restore with Warning

Build succeeded with warnings in 0.0s
]9;4;0;\
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
directory/file(1,2,3,4): warning AA0000: Restore with Warning

Build succeeded with warnings in 0.0s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
directory/file(1,2,3,4): warning AA0000: Restore with Warning

Build succeeded with warnings in 0.0s
]9;4;0;\
24 changes: 24 additions & 0 deletions src/MSBuild.UnitTests/TerminalLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,30 @@ public Task PrintImmediateMessage_Skipped()
return Verify(_outputWriter.ToString(), _settings).UniqueForOSPlatform();
}

[Fact]
public Task PrintRestore_Failed()
{
bool succeeded = false;
ErrorRaised?.Invoke(_eventSender, MakeErrorEventArgs("Restore Failed"));

ProjectFinished?.Invoke(_eventSender, MakeProjectFinishedEventArgs(_projectFile, succeeded));
BuildFinished?.Invoke(_eventSender, MakeBuildFinishedEventArgs(succeeded));

return Verify(_outputWriter.ToString(), _settings).UniqueForOSPlatform();
}

[Fact]
public Task PrintRestore_SuccessWithWarnings()
{
bool succeeded = true;
WarningRaised?.Invoke(_eventSender, MakeWarningEventArgs("Restore with Warning"));

ProjectFinished?.Invoke(_eventSender, MakeProjectFinishedEventArgs(_projectFile, succeeded));
BuildFinished?.Invoke(_eventSender, MakeBuildFinishedEventArgs(succeeded));

return Verify(_outputWriter.ToString(), _settings).UniqueForOSPlatform();
}

[Fact]
public Task PrintBuildSummary_Failed()
{
Expand Down
51 changes: 33 additions & 18 deletions src/MSBuild/TerminalLogger/TerminalLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,28 +591,37 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
/// </summary>
private void WarningRaised(object sender, BuildWarningEventArgs e)
{
var buildEventContext = e.BuildEventContext;
if (buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project))
{
string message = EventArgsFormatting.FormatEventMessage(
BuildEventContext? buildEventContext = e.BuildEventContext;
Project? project = null;
bool isTrackedProject = buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out project);
string message = EventArgsFormatting.FormatEventMessage(
category: AnsiCodes.Colorize("warning", TerminalColor.Yellow),
subcategory: e.Subcategory,
message: e.Message,
code: AnsiCodes.Colorize(e.Code, TerminalColor.Yellow),
file: HighlightFileName(e.File),
projectFile: null,
projectFile: e.ProjectFile ?? null,
lineNumber: e.LineNumber,
endLineNumber: e.EndLineNumber,
columnNumber: e.ColumnNumber,
endColumnNumber: e.EndColumnNumber,
threadId: e.ThreadId,
logOutputProperties: null);

if (isTrackedProject)
{
if (IsImmediateMessage(message))
{
RenderImmediateMessage(message);
}
project.AddBuildMessage(MessageSeverity.Warning, message);

project!.AddBuildMessage(MessageSeverity.Warning, message);
}
else
{
// It is necessary to display warning messages reported by MSBuild, even if it's not tracked in _projects collection.
RenderImmediateMessage(message);
_buildHasWarnings = true;
}
}

Expand All @@ -621,42 +630,48 @@ private void WarningRaised(object sender, BuildWarningEventArgs e)
/// </summary>
/// <param name="message">Raised event.</param>
/// <returns>true if marker is detected.</returns>
private bool IsImmediateMessage(string message)
{
private bool IsImmediateMessage(string message) =>
#if NET7_0_OR_GREATER
return ImmediateMessageRegex().IsMatch(message);
ImmediateMessageRegex().IsMatch(message);
#else
return _immediateMessageKeywords.Any(imk => message.IndexOf(imk, StringComparison.OrdinalIgnoreCase) >= 0);
_immediateMessageKeywords.Any(imk => message.IndexOf(imk, StringComparison.OrdinalIgnoreCase) >= 0);
#endif
}

/// <summary>
/// The <see cref="IEventSource.ErrorRaised"/> callback.
/// </summary>
private void ErrorRaised(object sender, BuildErrorEventArgs e)
{
var buildEventContext = e.BuildEventContext;
if (buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project))
{
string message = EventArgsFormatting.FormatEventMessage(
BuildEventContext? buildEventContext = e.BuildEventContext;
Project? project = null;
bool isTrackedProject = buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out project);
string message = EventArgsFormatting.FormatEventMessage(
category: AnsiCodes.Colorize("error", TerminalColor.Red),
subcategory: e.Subcategory,
message: e.Message,
code: AnsiCodes.Colorize(e.Code, TerminalColor.Red),
file: HighlightFileName(e.File),
projectFile: null,
projectFile: e.ProjectFile ?? null,
lineNumber: e.LineNumber,
endLineNumber: e.EndLineNumber,
columnNumber: e.ColumnNumber,
endColumnNumber: e.EndColumnNumber,
threadId: e.ThreadId,
logOutputProperties: null);

project.AddBuildMessage(MessageSeverity.Error, message);
if (isTrackedProject)
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved
{
project!.AddBuildMessage(MessageSeverity.Error, message);
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
// It is necessary to display error messages reported by MSBuild, even if it's not tracked in _projects collection.
RenderImmediateMessage(message);
ladipro marked this conversation as resolved.
Show resolved Hide resolved
_buildHasErrors = true;
}
}

#endregion
#endregion

#region Refresher thread implementation

Expand Down