msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

[Bug]: TreatWarningsAsErrors reverse behaviour

Open Planche95 opened this issue 1 year ago • 2 comments

Issue Description

Im hosting my code in Azure DevOps and Im using Yaml pipelines to build my app. I tried to run a pipeline today but I got an issue: image

Previous build was successful and I did not make any changes to the code: image

I noticed that MSBuild changed from version 17.7.3+8ec440e68 to version 17.8.3+195e7f5a3.

My next step was to reproduce the issue locally, because when I built the project locally everything went without issues. I noticed there is an update for my VS Studio Enterprise so I installed it and it changed the MSBuild version locally: image

after the update, build started to fail with the same issues as in the pipeline: image

This the csproj file that I used previously without wny issues:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<AzureFunctionsVersion>v4</AzureFunctionsVersion>
		<OutputType>Exe</OutputType>
		<ImplicitUsings>enable</ImplicitUsings>
    <CodeAnalysisTreatWarningsAsErrors>False</CodeAnalysisTreatWarningsAsErrors>
	</PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.3" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.5" />
    <PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Azure.Data.Tables" Version="12.7.1" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.13.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
  </ItemGroup>
  <ItemGroup>
		<None Update="host.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
		<None Update="local.settings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
			<CopyToPublishDirectory>Never</CopyToPublishDirectory>
		</None>
	</ItemGroup>
	<ItemGroup>
		<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
	</ItemGroup>
</Project>

what fixes the issue for me is to add the below lines to the csproj:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

This is the way I understand it should work:

  • For 'TreatWarningsAsErrors' set to true build should fail with issues like CA1305 or CA1822, just like it fails on the screenshots added above
  • For 'TreatWarningsAsErrors' set to false build should ignore this type of warning and build without issues

Right now behavior is reversed, when I set TreatWarningsAsErrors to True build is successful.

Steps to Reproduce

Steps in the description, below csproj working correctly:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<AzureFunctionsVersion>v4</AzureFunctionsVersion>
		<OutputType>Exe</OutputType>
		<ImplicitUsings>enable</ImplicitUsings>
    <CodeAnalysisTreatWarningsAsErrors>False</CodeAnalysisTreatWarningsAsErrors>
	</PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.3" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.5" />
    <PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Azure.Data.Tables" Version="12.7.1" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.13.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
  </ItemGroup>
  <ItemGroup>
		<None Update="host.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
		<None Update="local.settings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
			<CopyToPublishDirectory>Never</CopyToPublishDirectory>
		</None>
	</ItemGroup>
	<ItemGroup>
		<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
	</ItemGroup>
</Project>

but this is is not:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<AzureFunctionsVersion>v4</AzureFunctionsVersion>
		<OutputType>Exe</OutputType>
		<ImplicitUsings>enable</ImplicitUsings>
    <CodeAnalysisTreatWarningsAsErrors>False</CodeAnalysisTreatWarningsAsErrors>
	</PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.3" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.5" />
    <PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Azure.Data.Tables" Version="12.7.1" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.13.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
  </ItemGroup>
  <ItemGroup>
		<None Update="host.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
		<None Update="local.settings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
			<CopyToPublishDirectory>Never</CopyToPublishDirectory>
		</None>
	</ItemGroup>
	<ItemGroup>
		<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
	</ItemGroup>
</Project>

only difference is:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

Expected Behavior

Successful build, ignoring warnings

Actual Behavior

Failed build because of warnings

Analysis

No response

Versions & Configurations

MSBuild 17.8.3+195e7f5a3 dla .NET Framework 17.8.3.51904

Planche95 avatar Dec 12 '23 16:12 Planche95

Another project.

Case 1

  • CodeAnalysisTreatWarningsAsErrors set to False
  • TreatWarningsAsErrors set to False

Result: 3 Warnings and 1 Error image

Case 2

  • CodeAnalysisTreatWarningsAsErrors set to False
  • TreatWarningsAsErrors set to True

Result: 3 Warnings and 1 Error

Result: Previous 3 Warnings are now errors but previous errors disappear image

It looks like CodeAnalysisTreatWarningsAsErrors doesn't work without TreatWarningsAsErrors set to True, but it gets rid of one type of error to get another.

Planche95 avatar Dec 13 '23 09:12 Planche95

Hello, I was unable to repro this in version 17.8, even with the examples of the files that you have added to your post. Is it possible that another project is overriding the build settings?

And, could you please try to run this file with a small sample project (just a helloWorls.cs file with some unused variable in it for a warning) on your machine to check for the same behaviour. If you still see this behaviour please send us a binlog through (more information on how to do that is here)

maridematte avatar Apr 23 '24 10:04 maridematte

This issue is marked as stale because feedback has been requested for 30 days with no response. Please respond within 14 days or this issue will be closed due to inactivity.

This issue was closed due to inactivity. If you can still reproduce this bug, please comment with the requested information, detailed steps to reproduce the problem, or any other notes that might help in the investigation.