dotnet / dotnet/msbuild

Task returns true and logs error results in successful builds in unit tests

Open
#6,206 0 comments 0 reactions 0 assignees View on GitHub
bug testing triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

### Issue Description
In WarningsAsMessagesAndErrors_Tests.cs, you can create a test scenario that has a task return true and log an error, and the build result is a success instead of a failure.

### Steps to Reproduce
Add this test class:
```
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.UnitTests
{
public class CustomLogAndReturnTask : Task
{
public string WarningCode { get; set; }

public string ErrorCode { get; set; }

public bool ReturnHasLoggedErrors { get; set; }

[Required]
public bool Return { get; set; }

///
/// This task returns and logs what you want based on the running test.
///
public override bool Execute()
{
if(!string.IsNullOrEmpty(WarningCode))
{
Log.LogWarning(null, WarningCode, null, null, 0, 0, 0, 0, "Warning Logged!", null);
}

if(!string.IsNullOrEmpty(ErrorCode))
{
Log.LogError(null, ErrorCode, null, null, 0, 0, 0, 0, "Error Logged!", null);
}
return ReturnHasLoggedErrors ? !Log.HasLoggedErrors : Return;
}
}
}
```

Add this test case:
```
///
/// This is incredibly confusing and shouldn't work this way. This was also tested
/// in latest master (6819f7a) and passed. Keeping this test here for visibility.
/// This specific scenario does not repro when building a standard project with a bootstrapped MSBuild.
///
[Fact]
public void TaskReturnsTrueButLogsError_BuildShouldFinishAndFail()
{
using (TestEnvironment env = TestEnvironment.Create(_output))
{
TransientTestProjectWithFiles proj = env.CreateTestProjectWithFiles($@"






");

// What????
MockLogger logger = proj.BuildProjectExpectSuccess();

logger.WarningCount.ShouldBe(1);
logger.ErrorCount.ShouldBe(1);

// The build should CONTINUE when a task returns true.
logger.AssertLogContains("MSB1234");
}
}
```

### Expected Behavior
This test will pass, though it should fail.

### Actual Behavior
Here's the output log:
```
Build started.
Project "build.proj" (default targets):
Building with tools version "Current".
Target "Build" in project "C:\Users\bevillal\AppData\Local\Temp\jjik1gim.uf1\Temporary916c230401364e6faec586e2a834d69e\build.proj" (entry point):
Using "CustomLogAndReturnTask" task from assembly "Microsoft.Build.Engine.UnitTests".
Task "CustomLogAndReturnTask"
C:\Users\bevillal\AppData\Local\Temp\jjik1gim.uf1\Temporary916c230401364e6faec586e2a834d69e\build.proj(5,25): error MSB1235: Error Logged!
Done executing task "CustomLogAndReturnTask".
Task "CustomLogAndReturnTask"
C:\Users\bevillal\AppData\Local\Temp\jjik1gim.uf1\Temporary916c230401364e6faec586e2a834d69e\build.proj(6,25): warning MSB1234: Warning Logged!
Done executing task "CustomLogAndReturnTask".
Done building target "Build" in project "build.proj".
Done building project "build.proj".
Build succeeded.

```

### Analysis
This does not happen in live scenarios. Simply build this project that has a task called CustomLogAndReturnTask (same as above).
```










```
You should see that the build failed.

### Versions & Configurations
Happens in latest master as of 6819f7a.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.