Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable Roslyn Analyzers when using msbuild through the command line?

The Roslyn Analyzers are installed as nuget packages, which are dependencies of the FxCop Analyzers (also installed as nuget packages).

I have enabled full solution analysis as instructed here: How to Enable and disable full solution analysis for managed code.

I have a fairly large solution with most of the projects using the FxCop/Roslyn Analyzers and Visual Studio builds fine, usually in under a minute.

However, when running msbuild through the command line using:

"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe" "C:\Source\MySolution\MySmartClient.sln" /p:Configuration=Develop;Platform="Any CPU" /
t:Build

Building the solution takes anywhere from 4-15 minutes. The same is true on the build server which uses the same command.

I've tried /p:RunCodeAnalysis=False and that has no effect. I've also used process monitor to emulate the msbuild command that VS sends to msbuild with no change.

And, according to this doc: How to: Enable and disable automatic code analysis for managed code

The Enable Code Analysis on Build check box only affects static code analysis. It doesn't affect Roslyn code analyzers, which always execute at build if you installed them as a NuGet package.

These excessive build times are not practical. Is there any way to disable when using msbuild through the command line?

like image 693
Rich Avatar asked Jun 07 '19 22:06

Rich


People also ask

How do I turn off Roslyn code analysis?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.

What are Roslyn analyzers?

. NET Compiler Platform (Roslyn) Analyzers inspect your C# or Visual Basic code for style, quality, maintainability, design, and other issues. This inspection or analysis happens during design time in all open files. Analyzers are divided into the following groups: Code style analyzers are built into Visual Studio.


3 Answers

In case anyone else happens to find themselves here, I came across this issue on the dotnet/roslyn project on Github:

Feature: MSBuild switch for turning on/off analysis #23591

The preceding issue describes a work-around:

Substitute for old MSBuild properties? #1431

<PropertyGroup>
    <RunCodeAnalysis Condition="'$(RunCodeAnalysis)' == ''">true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="<whatever analyzers package you are depending on>" Condition="'$(RunCodeAnalysis)' == 'true'" />
</ItemGroup>
# You'll need to run a restore when changing this value
msbuild /p:RunCodeAnalysis=false

Although, I had a couple of differences though since I'm not using package references. This worked for me.

<ItemGroup>
    <Analyzer Include="<whatever analyzers package you are depending on>" Condition="'$(RunCodeAnalysis)' == 'true'" />
</ItemGroup>

<!-- I added the condition to the EnsureNugetPackageBuildImports too. -->
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="'$(RunCodeAnalysis)' == 'true' AND !Exists('<relative path to the prop of whatever analyzers you are depending on>')" Text="$([System.String]::Format('$(ErrorText)', '<relative path to the prop of whatever analyzers you are depending on>'))" />
</Target>
like image 180
Rich Avatar answered Oct 26 '22 05:10

Rich


The documentation has changed since the original answers. There is now this page documenting how to disable code analysis from analyzers:

There are 3 MSBuild properties you can use to control analyzer behavior (all default to true):

  • RunAnalyzersDuringBuild Controls whether analyzers run at build time.
  • RunAnalyzersDuringLiveAnalysis Controls whether analyzers analyze code live at design time.
  • RunAnalyzers Disables analyzers at both build and design time. This property takes precedence over RunAnalyzersDuringBuild and RunAnalyzersDuringLiveAnalysis.

Edit: it looks like there is an issue being tracked where these props don't work unless your project has Microsoft.CodeAnalysis.targets included. So your mileage may vary until this is fixed.

like image 35
RyanY Avatar answered Oct 26 '22 04:10

RyanY


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'" >

To disable a specific analyzer, use this trick:

We just spent 2 hours figuring out how to disable an analyzer based on an MSBuild property, AMA.

https://twitter.com/Nick_Craver/status/1173996405276467202?s=09

image

like image 45
jessehouwing Avatar answered Oct 26 '22 03:10

jessehouwing