通过命令行使用msbuild时如何禁用Roslyn Analyzers? [英] How do you disable Roslyn Analyzers when using msbuild through the command line?

查看:123
本文介绍了通过命令行使用msbuild时如何禁用Roslyn Analyzers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Roslyn分析仪作为nuget软件包安装,它们是FxCop分析仪的依赖项(也作为nuget软件包安装).

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.

对于使用FxCop/Roslyn Analyzers的大多数项目,我有一个相当大的解决方案,而Visual Studio的构建通常在不到一分钟的时间内就可以完成.

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.

但是,使用以下命令通过命令行运行msbuild时:

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

构建解决方案大约需要4-15分钟.在使用相同命令的构建服务器上也是如此.

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

我尝试了/p:RunCodeAnalysis=False,但这没有任何效果.我还使用了进程监视器来模拟VS发送给msbuild的msbuild命令,而无需进行任何更改.

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.

并且,根据此文档:

在构建中启用代码分析复选框仅影响静态代码分析.它不会影响Roslyn代码分析器,如果您将它们安装为NuGet软件包,则该代码分析器将始终在构建时执行.

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.

这些过多的构建时间不切实际.通过命令行使用msbuild时可以禁用任何方法吗?

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

推荐答案

它不是真的受支持,但是有一种解决方法:

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

在解决方案根文件夹中创建一个Directory.Build.targets(msbuild> = v15.0),After.{SolutionName}.sln.targets(msbuild< 15.0)文件并添加:

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>

您现在可以传入/p:UseRosynAnalyzers=false来删除项目中配置的所有分析器.

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

另请参阅:

  • 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

您可以编辑条件以在RunCodeAnalysis=FalseNever上也触发.

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:

我们仅花了2个小时来弄清楚如何基于MSBuild属性AMA禁用分析器.

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

这篇关于通过命令行使用msbuild时如何禁用Roslyn Analyzers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆