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

Rendering message about build cancellation (Terminal Logger) #10055

Merged
merged 17 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 24 additions & 2 deletions src/Build.UnitTests/BackEnd/EventSourceSink_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void ConsumeEventsGoodEvents()
EventHandlerHelper testHandlers = new EventHandlerHelper(sink, null);
VerifyRegisteredHandlers(RaiseEventHelper.BuildStarted, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.BuildFinished, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.BuildCanceled, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.NormalMessage, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.TaskFinished, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.CommandLine, eventHelper, testHandlers);
Expand All @@ -66,6 +67,7 @@ public void ConsumeEventsGoodEventsNoHandlers()
RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildStarted);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildFinished);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildCanceled);
eventHelper.RaiseBuildEvent(RaiseEventHelper.NormalMessage);
eventHelper.RaiseBuildEvent(RaiseEventHelper.TaskFinished);
eventHelper.RaiseBuildEvent(RaiseEventHelper.CommandLine);
Expand Down Expand Up @@ -98,6 +100,7 @@ public void LoggerExceptionInEventHandler()
{
RaiseExceptionInEventHandler(RaiseEventHelper.BuildStarted, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.BuildFinished, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.BuildCanceled, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.NormalMessage, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.TaskFinished, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.CommandLine, exception);
Expand Down Expand Up @@ -127,7 +130,7 @@ public void RaiseGenericBuildEventArgs()
});
}
/// <summary>
/// Verify that shutdown un registers all of the event handlers
/// Verify that shutdown unregisters all of the event handlers
/// </summary>
[Fact]
public void VerifyShutdown()
Expand Down Expand Up @@ -294,7 +297,10 @@ private static void VerifyRegisteredHandlers(BuildEventArgs buildEventToRaise, R
try
{
eventHelper.RaiseBuildEvent(buildEventToRaise);
if (buildEventToRaise.GetType() != typeof(GenericBuildStatusEventArgs))
Type eventType = buildEventToRaise.GetType();

if (eventType != typeof(GenericBuildStatusEventArgs) &&
eventType != typeof(BuildCanceledEventArgs))
{
Assert.Equal(testHandlers.RaisedEvent, buildEventToRaise); // "Expected buildevent in handler to match buildevent raised on event source"
Assert.Equal(testHandlers.RaisedEvent, testHandlers.RaisedAnyEvent); // "Expected RaisedEvent and RaisedAnyEvent to match"
Expand Down Expand Up @@ -715,6 +721,11 @@ internal sealed class RaiseEventHelper
/// </summary>
private static BuildFinishedEventArgs s_buildFinished = new BuildFinishedEventArgs("Message", "Keyword", true);

/// <summary>
/// Build Canceled Event
/// </summary>
private static BuildCanceledEventArgs s_buildCanceled = new BuildCanceledEventArgs("Message");

/// <summary>
/// Build Message Event
/// </summary>
Expand Down Expand Up @@ -846,6 +857,17 @@ internal static BuildFinishedEventArgs BuildFinished
}
}

/// <summary>
/// Event which can be raised in multiple tests.
/// </summary>
internal static BuildCanceledEventArgs BuildCanceled
{
get
{
return s_buildCanceled;
}
}

/// <summary>
/// Event which can be raised in multiple tests.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/LoggingService_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public void RegisterGoodDistributedAndCentralLoggerTestBuildStartedFinished()
Assert.Equal(2, regularILoggerB.BuildStartedCount);
Assert.Equal(2, regularILoggerC.BuildStartedCount);

// Make sure if we call build started again we only get one other build started event.
// Make sure if we call build finished again we only get one other build finished event.
_initializedService.LogBuildFinished(true);
Assert.Equal(2, regularILoggerA.BuildFinishedCount);
Assert.Equal(2, regularILoggerB.BuildFinishedCount);
Expand Down
50 changes: 26 additions & 24 deletions src/Build.UnitTests/BackEnd/LoggingServicesLogMethod_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -999,17 +999,21 @@ public void ProjectFinished()
/// Make sure we can log a build started event correctly.
/// Test both the LogOnlyCriticalEvents true and false
/// </summary>
[Fact]
public void LogBuildStarted()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void LogBuildStarted(bool onlyLogCriticalEvents)
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);

service.OnlyLogCriticalEvents = onlyLogCriticalEvents;
service.LogBuildStarted();

string message = onlyLogCriticalEvents ? string.Empty : ResourceUtilities.GetResourceString("BuildStarted");

BuildStartedEventArgs buildEvent =
new BuildStartedEventArgs(
ResourceUtilities.GetResourceString("BuildStarted"),
message,
null /* no help keyword */,
service.ProcessedBuildEvent.Timestamp);

Expand All @@ -1018,26 +1022,6 @@ public void LogBuildStarted()
new EventArgsEqualityComparer<BuildStartedEventArgs>());
}

[Fact(Skip = "https://github.com/dotnet/msbuild/issues/437")]
[Trait("Category", "netcore-osx-failing")]
[Trait("Category", "netcore-linux-failing")]
public void LogBuildStartedCriticalOnly()
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.OnlyLogCriticalEvents = true;
service.LogBuildStarted();

BuildStartedEventArgs buildEvent =
new BuildStartedEventArgs(
string.Empty,
null /* no help keyword */);

Assert.IsType<BuildStartedEventArgs>(service.ProcessedBuildEvent);
Assert.Equal(buildEvent, (BuildStartedEventArgs)service.ProcessedBuildEvent,
new EventArgsEqualityComparer<BuildStartedEventArgs>());
}

/// <summary>
/// Make sure we can log a build finished event correctly.
/// Verify the success cases as well as OnlyLogCriticalEvents
Expand All @@ -1062,6 +1046,24 @@ public void LogBuildFinished()
Assert.True(((BuildFinishedEventArgs)service.ProcessedBuildEvent).IsEquivalent(buildEvent));
}

[Fact]
public void LogBuildCanceled()
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.LogBuildCanceled();


BuildCanceledEventArgs buildEvent =
new BuildCanceledEventArgs(
ResourceUtilities.GetResourceString("AbortingBuild"),
service.ProcessedBuildEvent.Timestamp);

Assert.IsType<BuildCanceledEventArgs>(service.ProcessedBuildEvent);
Assert.Equal(buildEvent, (BuildCanceledEventArgs)service.ProcessedBuildEvent,
new EventArgsEqualityComparer<BuildCanceledEventArgs>());
}

/// <summary>
/// Exercise Asynchronous code path, this method should return right away as there are no events to process.
/// This will be further tested in the LoggingService_Tests class.
Expand Down
4 changes: 4 additions & 0 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ public void LogBuildFinished(bool success)
{
}

/// <inheritdoc />
public void LogBuildCanceled()
{
}

/// <inheritdoc />
public BuildEventContext CreateEvaluationBuildEventContext(int nodeId, int submissionId)
Expand Down
4 changes: 2 additions & 2 deletions src/Build.UnitTests/Graph/ResultCacheBasedBuilds_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void InvalidCacheFilesShouldLogError(byte[] cacheContents)
result.OverallResult.ShouldBe(BuildResultCode.Failure);

_logger.FullLog.ShouldContain("MSB4256:");
_logger.AllBuildEvents.Count.ShouldBe(4);
_logger.AllBuildEvents.Count.ShouldBe(5);
_logger.ErrorCount.ShouldBe(1);
}

Expand Down Expand Up @@ -566,7 +566,7 @@ public void NonExistingInputResultsCacheShouldLogError()

result.OverallResult.ShouldBe(BuildResultCode.Failure);

_logger.AllBuildEvents.Count.ShouldBe(4);
_logger.AllBuildEvents.Count.ShouldBe(5);
_logger.Errors.First().Message.ShouldContain("MSB4255:");
_logger.Errors.First().Message.ShouldContain("FileDoesNotExist1");
_logger.Errors.First().Message.ShouldContain("FileDoesNotExist2");
Expand Down
3 changes: 3 additions & 0 deletions src/Build/BackEnd/BuildManager/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ public void CancelAllSubmissions()

private void CancelAllSubmissions(bool async)
{
ILoggingService loggingService = ((IBuildComponentHost)this).LoggingService;
loggingService.LogBuildCanceled();
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved

var parentThreadCulture = _buildParameters != null
? _buildParameters.Culture
: CultureInfo.CurrentCulture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public ISet<string> WarningsAsMessages
/// This property is ignored by this event sink and relies on the receiver to keep track of whether or not any errors have been logged.
/// </summary>
public ISet<int> BuildSubmissionIdsThatHaveLoggedErrors { get; } = null;

#endregion
#region IBuildEventSink Methods

Expand Down
3 changes: 3 additions & 0 deletions src/Build/BackEnd/Components/Logging/EventSourceSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ public void Consume(BuildEventArgs buildEvent)
HaveLoggedBuildFinishedEvent = true;
RaiseBuildFinishedEvent(null, buildFinishedEvent);
break;
case BuildCanceledEventArgs buildCanceledEvent:
RaiseStatusEvent(null, buildCanceledEvent);
break;
case CustomBuildEventArgs customBuildEvent:
RaiseCustomEvent(null, customBuildEvent);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/Build/BackEnd/Components/Logging/ILoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ MessageImportance MinimumRequiredMessageImportance
/// <param name="success">Did the build succeed or not</param>
void LogBuildFinished(bool success);

/// <summary>
/// Logs that the build has canceled
/// </summary>
void LogBuildCanceled();

/// <summary>
/// Create an evaluation context, by generating a new evaluation id.
/// </summary>
Expand Down Expand Up @@ -650,6 +655,7 @@ bool HaveLoggedBuildFinishedEvent
get;
set;
}

#endregion
/// <summary>
/// Entry point for a sink to consume an event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ public void LogBuildFinished(bool success)
WaitForLoggingToProcessEvents();
}

/// <inheritdoc />
public void LogBuildCanceled()
{
string message = ResourceUtilities.GetResourceString("AbortingBuild");
BuildCanceledEventArgs buildEvent = new BuildCanceledEventArgs(message);

ProcessLoggingEvent(buildEvent);
}

/// <inheritdoc />
public BuildEventContext CreateEvaluationBuildEventContext(int nodeId, int submissionId)
=> new BuildEventContext(submissionId, nodeId, NextEvaluationId, BuildEventContext.InvalidProjectInstanceId, BuildEventContext.InvalidProjectContextId, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidTaskId);
Expand Down
15 changes: 14 additions & 1 deletion src/Build/Definition/ProjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,11 @@ public ReusableLogger(ILogger originalLogger)
/// </summary>
public event BuildFinishedEventHandler BuildFinished;

/// <summary>
/// The BuildCanceled logging event
/// </summary>
public event BuildCanceledEventHandler BuildCanceled;

/// <summary>
/// The ProjectStarted logging event
/// </summary>
Expand Down Expand Up @@ -2021,7 +2026,7 @@ public ReusableLogger(ILogger originalLogger)
/// The telemetry sent event.
/// </summary>
public event TelemetryEventHandler TelemetryLogged;

/// <summary>
/// Should evaluation events include generated metaprojects?
/// </summary>
Expand Down Expand Up @@ -2392,6 +2397,14 @@ private void BuildFinishedHandler(object sender, BuildFinishedEventArgs e)
BuildFinished?.Invoke(sender, e);
}

/// <summary>
/// Handler for BuildCanceled events.
/// </summary>
private void BuildCanceledHandler(object sender, BuildCanceledEventArgs e)
{
BuildCanceled?.Invoke(sender, e);
}

/// <summary>
/// Handler for Any events.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Logging/ParallelLogger/ParallelConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,10 @@ public override void StatusEventHandler(object sender, BuildStatusEventArgs e)
propertyOutputMap[evaluationKey] = value;
}
}
else if (e is BuildCanceledEventArgs buildCanceled)
{
Console.WriteLine(e.Message);
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
}
}

private void DisplayDeferredStartedEvents(BuildEventContext e)
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Logging/SerialConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ public override void StatusEventHandler(object sender, BuildStatusEventArgs e)
}
}
}
else if (e is BuildCanceledEventArgs buildCanceled)
{;
Console.WriteLine(e.Message);
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
<data name="NoBuildInProgress" xml:space="preserve">
<value>The operation cannot be completed because BeginBuild has not yet been called.</value>
</data>
<data name="AbortingBuild" xml:space="preserve">
<value>Attempting to cancel the build...</value>
</data>
<data name="WaitingForEndOfBuild" xml:space="preserve">
<value>The operation cannot be completed because EndBuild has already been called but existing submissions have not yet completed.</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading