Skip to content
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
58 changes: 58 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using System.Threading;

namespace Microsoft.Build.UnitTests.BackEnd
{
Expand Down Expand Up @@ -107,6 +108,63 @@ public void TasksNotDiscoveredWhenTaskConditionFalse()
logger.AssertLogContains("Made it");
}

[Fact]
public void CanceledTasksDoNotLogMSB4181()
{
using (TestEnvironment env = TestEnvironment.Create(_testOutput))
{
BuildManager manager = new BuildManager();
ProjectCollection collection = new ProjectCollection();

string contents = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion ='Current'>
<Target Name='test'>
<Exec Command='" + Helpers.GetSleepCommand(TimeSpan.FromSeconds(10)) + @"'/>
</Target>
</Project>";

MockLogger logger = new MockLogger(_testOutput);

var project = new Project(XmlReader.Create(new StringReader(contents)), null, MSBuildConstants.CurrentToolsVersion, collection)
{
FullPath = env.CreateFile().Path
};

var _parameters = new BuildParameters
{
ShutdownInProcNodeOnBuildFinish = true,
Loggers = new ILogger[] { logger },
EnableNodeReuse = false
};
;

BuildRequestData data = new BuildRequestData(project.CreateProjectInstance(), new string[] { "test" }, collection.HostServices);
manager.BeginBuild(_parameters);
BuildSubmission asyncResult = manager.PendBuildRequest(data);
asyncResult.ExecuteAsync(null, null);
Thread.Sleep(500);
manager.CancelAllSubmissions();
asyncResult.WaitHandle.WaitOne();
BuildResult result = asyncResult.BuildResult;
manager.EndBuild();

// No errors from cancelling a build.
logger.ErrorCount.ShouldBe(0);
// Warn because the task is being cancelled.
// NOTE: This assertion will fail when debugging into it because "waiting on exec to cancel" warning will be logged.
logger.WarningCount.ShouldBe(1);
// Build failed because it was cancelled.
result.OverallResult.ShouldBe(BuildResultCode.Failure);
// Should log "Cmd being cancelled because build was cancelled" warning
logger.AssertLogContains("MSB5021");
// Should NOT log "exec failed without logging error"
logger.AssertLogDoesntContain("MSB4181");

collection.Dispose();
manager.Dispose();
}
}

/// <summary>
/// Verify when task outputs are overridden the override messages are correctly displayed
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Build/BackEnd/Components/RequestBuilder/TaskBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,14 @@ private async Task<WorkUnitResult> ExecuteInstantiatedTask(ITaskExecutionHost ta
// that is logged as an error. MSBuild tasks are an exception because
// errors are not logged directly from them, but the tasks spawned by them.
IBuildEngine be = host.TaskInstance.BuildEngine;
if (taskReturned && !taskResult && !taskLoggingContext.HasLoggedErrors && (be is TaskHost th ? th.BuildRequestsSucceeded : false) && (be is IBuildEngine7 be7 ? !be7.AllowFailureWithoutError : true))
if (taskReturned // if the task returned
&& !taskResult // and it returned false
&& !taskLoggingContext.HasLoggedErrors // and it didn't log any errors
&& (be is TaskHost th ? th.BuildRequestsSucceeded : false)
&& (be is IBuildEngine7 be7 ? !be7.AllowFailureWithoutError : true) // and it's not allowed to fail unless it logs an error
&& !(_cancellationToken.CanBeCanceled && _cancellationToken.IsCancellationRequested)) // and it wasn't cancelled
{
// Then decide how to log MSB4181
if (_continueOnError == ContinueOnError.WarnAndContinue)
{
taskLoggingContext.LogWarning(null,
Expand Down