Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable code analysis when using MSBuild 14

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

like image 881
mu88 Avatar asked Apr 28 '17 13:04

mu88


2 Answers

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>
//------------------------------------------------------------------------------
like image 148
Leo Liu-MSFT Avatar answered Oct 16 '22 23:10

Leo Liu-MSFT


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:

  • https://github.com/dotnet/roslyn/issues/23591#issuecomment-507802134
  • https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019#directorybuildprops-and-directorybuildtargets

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'" >
like image 8
jessehouwing Avatar answered Oct 16 '22 23:10

jessehouwing