Disable code analysis when using MSBuild 14

9.8k Views Asked by At

I have a .NET solution containing several C# 6.0 projects. Every project references the StyleCop Analyzer via NuGet. Within Visual Studio, I have the possibility to distinguish between building and analyzing the code, but I don't see how to do this with MSBuild v14.0 on the command line (e. g. on a CI server). I'm calling msbuild mySolution.sln /t:Rebuild with the following options, none of them worked:

  • /p:RunCodeAnalysis=False
  • /p:RunCodeAnalysisOnThisProject=False
  • /p:RunCodeAnalysis=False,RunCodeAnalysisOnThisProject=False

Whatever I do, the warnings SAxxxx remain in the output. Does anyone know how to disable code analysis when using MSBuild?

Background: on our CI server, I want to distinguish between "basic MSBuild warnings" and warnings coming from static code analysis.

Regards

4

There are 4 best solutions below

0
On

In .Net 5 supported project you can simply edit the .csproj and add:

  • <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild> Disable code analysis on build
  • <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis> Disable code analysis on live analysis
  • <RunAnalyzers>false</RunAnalyzers> prevent analyzers from running on this project

I use it in my Unit Testing Projects by adding it in the PropertyGroup that holds the TargetFramework like so:

<PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <IsPackable>false</IsPackable>
        <!--disable code analysis on this XUNIT Project-->
        <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
        <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
        <RunAnalyzers>false</RunAnalyzers>
</PropertyGroup>

for further details refer to MS Documentation

0
On

If you have a lot of csproj files in solution, it slow to set each project. Download extension for visual studio 2022: SwitchRunCodeAnalysis

6
On

anyone know how to disable code analysis when using MSBuild?

The RunCodeAnalysis setting as defined in the build server TFSBuild.proj differs significantly from the local MSBuild project schema options.

Build server support the value of "Always, Default, Never" for RunCodeAnalysis. In contrast, locally MSBuild supports "True or False" for RunCodeAnalysis.

You can check the section of the Microsoft.TeamFoundation.Build.targets file:

<Target Name="CoreCompileSolution">
 
  <PropertyGroup>
    <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Always'">RunCodeAnalysis=true</CodeAnalysisOption>
    <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Never'">RunCodeAnalysis=false</CodeAnalysisOption>
    <!-- ... -->
  </PropertyGroup>
  <!-- ... -->
</Target>

From this we can infer that "Default" setting does not provide a value to the runtime, while "Always" and "Never" map to True/False respectively.

On the build server:

Always tells MSBuild to compile all projects with RunCodeAnalysis=True

Never tells MSBuild to suppress code analysis (RunCodeAnalysis=False) on all projects.

So the values for RunCodeAnalysis are either Default,Always,Never or True,False, depending on how you build.

You can refer to the How to: Edit a Build Type and CodeAnalysis, FxCop and Team Build to more detailed info.

Update: According to the mu88 replied, I have create a test demo on the Jenkins with RunCodeAnalysis=False, the code analysis is disabled as expected. Below is my configuration on the Jenkins:

enter image description here

Besides, You can also check the build log whether has the section from "Running Code Analysis..." to "Code Analysis Complete " And for the warnings SAxxxx remain in the output, this is not Code Analysis result. You can test it on the Visual Studio without code analysis. After install the package StyleCop.Analyzers, then build the project, you will get those warnings.

So please double check whether the build log on the Jenkins contains the section "Running Code Analysis..." and "Code Analysis Complete " after build the project with parameter:/p:RunCodeAnalysis=False.

Update2:

If you want to suppress StyleCop Warning, you can trick StyleCop into not processing a file at all by adding this header at the top of .cs file:

//------------------------------------------------------------------------------
// <auto-generated>
// Well, not really. This is just a trick to get StyleCop off my back.
// </auto-generated>
//------------------------------------------------------------------------------
6
On

It's not really supported, but there is a workaround:

Create a Directory.Build.targets (msbuild >= v15.0), After.{SolutionName}.sln.targets (msbuild < 15.0) file in your solution root folder and add:

<Project>
  <Target Name="DisableAnalyzers" 
           BeforeTargets="CoreCompile" 
           Condition="'$(UseRoslynAnalyzers)' == 'false'"> 
    <!-- 
       Disable analyzers via an MSBuild property settable on the command line. 
    --> 
    <ItemGroup> 
      <Analyzer Remove="@(Analyzer)" /> 
    </ItemGroup> 
  </Target> 
</Project>

You can pass in /p:UseRoslynAnalyzers=false now to remove all analyzers configured in the project.

See also:

You can edit the condition to also trigger on RunCodeAnalysis=False or Never.

<Target Name="DisableAnalyzers" 
        BeforeTargets="CoreCompile" 
        Condition="
           '$(UseRoslynAnalyzers)' == 'false' 
           or '$(RunCodeAnalysis)' == 'false' 
           or '$(RunCodeAnalysis)' == 'never'" >