WarningAsError overriden with SonarQubePrepare task (v4 or v5)

Hello,

I have an issue with the usage of SonarQubePrepare (v4 or v5) in my Azure DevOps pipeline.

To simplify my use case, there is 3 steps

SonarQubePrepare
Dotnet restore
Dotnet build

There is only 1 .net project (csproj) with the following config

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
		<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    </PropertyGroup>
</Project>

The main point is

		<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

I specified a “Directory.Build.targets” in my repository

<Project>
  <Target Name="CheckTreatWarningsAsErrors" BeforeTargets="CoreCompile">
    <Message Text="Project: $(MSBuildProjectName) – TreatWarningsAsErrors=$(TreatWarningsAsErrors)" Importance="High" />
  </Target>
</Project>

The purpose it’s to display the value of the “TreatWarningsAsErrors” property.

During the “dotnet build” step, the TreatWarningsAsErrors property value is

  Project: Project – TreatWarningsAsErrors=false
##[warning]content/src/Program.cs(5,7): Warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

As you can see, the “TreatWarningsAsErrors” is “false” instead of “true” as specified in the csproj above.

If I disabled the step “SonarQubePrepare”, the TreatWarningsAsErrors keep it’s default value, “true” as specified in the csproj.

  Project: Project – TreatWarningsAsErrors=true
##[error]content/src/Program.cs(5,7): Error CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

And the warning is correctly managed as a error.

There is any way to avoid the task “SonarQubePrepare” to override the csproj property of “TreatWarningsAsErrors” ?

Thank you,

Andy.

Hey there.

Take a look at this guide:

1 Like

Thank you :slight_smile: