17

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

Alex
  • 14,104
  • 11
  • 54
  • 77
mu88
  • 4,156
  • 1
  • 23
  • 47

4 Answers4

8

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>
//------------------------------------------------------------------------------
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thanks for your reply, but instead of TFS we're using Jenkins. Anyway, using Never instead of False doesn't change the behaviour. – mu88 May 02 '17 at 06:18
  • Sorry for the delay reply. I have create a test demo on the Jenkins with the RunCodeAnalysis=False, all things works fine. You can refer to my update in the answer. – Leo Liu May 03 '17 at 10:54
  • 1
    Thank you for your reply. For me, setting RunCodeAnalysis to False was the same like disabling StyleCop.Analyzers, sorry for that lack of clarity. What do I have to do if want to exclude the MSBuild warnings that come from StyleCop.Analyzers? Or is it not possible? – mu88 May 04 '17 at 18:50
  • @mu88, you can refer to the update2 in my answer, check if it resolve your question. If above answer resolve your question, you can mark it answer, so it could help other community members who get the same issues. If not, you can tell us your question. Thanks. – Leo Liu May 05 '17 at 02:01
  • 1
    Thanks for your update, but I would like to disable the StyleCop-Warnings on a MSBuild-level and not within the source code. – mu88 May 06 '17 at 15:00
  • To clarify OPs comments and why I started the bounty, this is not a solution for disabling analyzers (the new roslyn ones, not the old static code analysis) when running MSBuild v14 from the command line. – Alex Jul 19 '19 at 17:47
8

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'" >
yaakov
  • 5,552
  • 35
  • 48
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Have you tried this? It seems like it does not trigger on `BeforeTargets="CoreCompile"` for me. I've added a message that I see on `BeforeTargets="Build"`, but it's still trying to run the analyzers. – Alex Jul 22 '19 at 15:16
  • Nuget, packages.config and after.solution.sln.targets (since it's msbuild v14) – Alex Jul 22 '19 at 16:06
  • 1
    I found the problem, we specified the targets as parameters to msbuild, like `/t:Clean,Build`. This worked fine with msbuild > 14 and the DisableAnalyzers-target worked. But when using msbuild 14, this lead to that the DisableAnalyzers-target didn't trigger. If adding the target to the parameter we could see that it ran somehow by the log message that we added, but it didn't affect the analyzers. So the solution for us seem to be to just use the default targets instead of providing msbuild with the targets explicitly when using msbuild 14. – Alex Jul 23 '19 at 07:52
  • One odd thing though, when not using the explicit target parameter for msbuild v14, it's not even trying to run the analyzers regardless of the DisableAnalyzers-target... – Alex Jul 23 '19 at 08:04
  • 1
    Can you do an edit to the answer with the details that matter. Glad you found a solution that works! Now upgrade to MsBuild 16 ASAP ;). – jessehouwing Jul 23 '19 at 09:04
  • great, this kills all sourcegenerators introduced with NET5 and starting with NET6 therefore all razor processing. – springy76 Dec 21 '21 at 16:19
1

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

Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
0

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

zzfima
  • 1,528
  • 1
  • 14
  • 21