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

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:05:11

问题


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?


回答1:


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:UseRosynAnalyzers=false now to remove all analyzers configured in the project.

See also:

  • https://github.com/dotnet/roslyn/issues/23591#issuecomment-507802134
  • https://docs.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




回答2:


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>


来源:https://stackoverflow.com/questions/56501705/how-do-you-disable-roslyn-analyzers-when-using-msbuild-through-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!