Skip to content

HasLoggedErrors should respect MSBuildWarningsAsErrors #5511

@sbomer

Description

@sbomer

Steps to reproduce

warnaserr.tar.gz

    public class MyTask : Task
    {
        public override bool Execute()
        {
            Log.LogWarning(subcategory: null,
                           warningCode: "MyWarning1234",
                           helpKeyword: null,
                           file: null,
                           lineNumber: 0,
                           columnNumber: 0,
                           endLineNumber: 0,
                           endColumnNumber: 0,
                           message: "my warning");

            return !Log.HasLoggedErrors;
        }
    }
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <MSBuildWarningsAsErrors>MyWarning1234</MSBuildWarningsAsErrors>
  </PropertyGroup>

  <UsingTask TaskName="task.MyTask" AssemblyFile="$(MSBuildThisFileDirectory)../task/bin/Debug/net5.0/task.dll" />
  <Target Name="MyTargetOuter"
          DependsOnTargets="MyTargetInner">
    <Message Importance="High" Text="Outer target" />
  </Target>

  <Target Name="MyTargetInner">
    <MyTask />
  </Target>
> dotnet msbuild /t:MyTargetOuter
warnaserr/project/project.csproj(16,5): error MyWarning1234: my warning
  Outer target

Expected behavior

Log.HasLoggedErrors returns true when the logged warning is treated as an error due to MSBuildWarningsAsErrors, causing the build to stop after MyTask.

Actual behavior

HasLoggedErrors returns false, the build continues, and the outer target runs even after the task produced an error.

Perhaps this is the intended behavior. If that's the case, is there a recommended way for tasks to check whether any of their warnings have been turned into errors?

We are using ToolTask (which relies on HasLoggedErrors by default) for the IL linker MSBuild task, and we don't want to continue the build if any errors were logged - otherwise we touch a file on disk that prevents the linker from running incrementally next time.

Environment data

msbuild /version output: 16.7.0.36003

OS info: 18.04.1-Ubuntu

If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc): dotnet cli

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions