Visualstudio-docs: CA warnings from code analyzers shown as errors after migration

Created on 1 Oct 2019  Â·  6Comments  Â·  Source: MicrosoftDocs/visualstudio-docs

When migrating to code analyzers our legacy code analysis warnings changed to errors. We soon realized it was because we have set "Treat warnings as errors" under Build Properties for our projects in Visual Studio. But it was a surprise since it seems to be undocumented here.

We thought having compiler warnings as errors, code analysis violations as warnings and style violations as messages worked very well. Is it possible to set it up that way with code analyzers?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 doc-bug easy visual-studio-windowprod vs-ide-code-analysitech

Most helpful comment

Yes, users can set the following MSBuild property in their project file:

<PropertyGroup>
   <CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

Please ensure that the above property is set prior to the imports for the FxCopAnalyzers props file.

All 6 comments

@marhja -- Markus, thank you for your feedback. Here are a few options where you might consider asking your question:​

https://social.msdn.microsoft.com/Forums/vstudio

https://www.stackoverflow.com

https://www.reddit.com/r/VisualStudio/

As well, you might also consider using the Report a Problem tool to report the issue.

@gewarren -- Genevieve, please consider adding this information.

@mavasani Is it possible to have compiler warnings treated as errors but not code analyzer warnings?

Yes, users can set the following MSBuild property in their project file:

<PropertyGroup>
   <CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

Please ensure that the above property is set prior to the imports for the FxCopAnalyzers props file.

To clarify further, you can create a Directory.build.props file with below content and add it to the root of your project/solution:

<Project>
 <PropertyGroup>
    <CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
 </PropertyGroup>
</Project>

It seems like there is a bug right now due to which the property does not seem to get respected in live analysis, but is correctly respected in build. I will file a separate bug to ensure that gets fixed.

FYI: To work around the bug I mentioned above, you can do the following:

  1. Add the Directory.build.props file with the contents above.
  2. Add an explicit Import for the props file prior to the PackageReference:
  <Import Project="$(NuGetPackageRoot)\microsoft.codeanalysis.fxcopanalyzers\2.9.4\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props" />

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4" />
  </ItemGroup>

This will generate an MSBuild warning as follows, which can be ignored:

Severity    Code    Description Project File    Line    Suppression State
Warning MSB4011 "C:\Users\mavasani\.nuget\packages\microsoft.codeanalysis.fxcopanalyzers\2.9.4\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props" cannot be imported again. It was already imported at "C:\Users\mavasani\source\repos\ConsoleApp19\ConsoleApp19\obj\ConsoleApp19.csproj.nuget.g.props (20,5)". This is most likely a build authoring error. This subsequent import will be ignored.    ConsoleApp19        9   

@mavasani -- Thanks, you helped me solve the problem both for live code analysis and for build.

I could not do exactly as you suggested since I have ASP.NET projects which I have not been able to migrate from package.config to PackageReference yet.

Instead I created a CodeAnalysisBugWorkaround.props file with the content you described and added an import of that in my project files before the import of Microsoft.CodeAnalysis.FxCopAnalyzers.props.
This also avoided the MSBuild warning you got. 😊

Could you please provide a link to the bug when you've filed it so I can track it and remove the workaround once it gets fixed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DaanV2 picture DaanV2  Â·  4Comments

CeciAc picture CeciAc  Â·  3Comments

ChrisMaddock picture ChrisMaddock  Â·  3Comments

Ogglas picture Ogglas  Â·  3Comments

wellwind picture wellwind  Â·  3Comments