在调试构建期间禁用 SonarLint 分析

Posted

技术标签:

【中文标题】在调试构建期间禁用 SonarLint 分析【英文标题】:Disable SonarLint analysis during Debug build 【发布时间】:2017-11-16 09:16:33 【问题描述】:

是否可以(在使用 SonarLint 扩展的 Visual Studio 中)在调试构建期间禁用声纳分析器,但在发布构建中保持启用它们?原因是将解决方案连接到 SonarQube 大大增加了构建时间。

【问题讨论】:

【参考方案1】:

如果我在调试配置中从 Visual Studio 中构建解决方案,我最终会修改 .csproj 文件以删除分析器。这样,sonarlint 就不会抱怨规则已经过时,也不会受到更新的影响。我从here得到了答案

<Target Name="DisableAnalyzersForVisualStudioBuild"
        BeforeTargets="CoreCompile"
        Condition="'$(BuildingInsideVisualStudio)' == 'True' And '$(BuildingProject)' == 'True' And '$(Configuration)' == 'Debug'">
  <!--
    Disable analyzers when building a project inside Visual Studio. Note that analyzer behavior for IntelliSense purposes is not altered by this.
  -->
  <ItemGroup>
    <Analyzer Remove="@(Analyzer)"/>
  </ItemGroup>
</Target>

【讨论】:

【参考方案2】:

我能想到的唯一方法是复制您的规则集并降低(禁用)调试模式的规则,但保留发布模式的原始规则集。请注意,这会很痛苦,因为与 SonarQube 上的质量配置文件相比,SonarLint 会抱怨您的规则集强度较低。此外,每次你更新它都可能会破坏这个手动调整。

【讨论】:

我是否使用项目属性中的“代码分析”部分来为不同的配置选择不同的规则集?我发现奇怪的一件事是,即使未选中“在构建时启用代码分析”,声纳分析器似乎正在运行。 您可能会在这里提出问题以支持该标志github.com/SonarSource/sonar-csharp/issues【参考方案3】:

选项 1. 虚拟 dll。

如果您将 SonarLint 作为 .csproj 文件的一部分作为下一个:

  <ItemGroup>
    <Analyzer Include="$(SolutionDir)CodeAnalRules\SonarAnalyzer.CSharp.dll" />
    <Analyzer Include="$(SolutionDir)CodeAnalRules\SonarAnalyzer.dll" />
  </ItemGroup>

将下一个代码编译成程序集。复制并命名为SonarAnalyzer.CSharp.dllSonarAnalyzer.dll。用这个替换现有的声纳组件。返回Release build。

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;

namespace DisableSonarLint

    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class DisableSonarLintAnalyzer : DiagnosticAnalyzer
    
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create<DiagnosticDescriptor>();

        public override void Initialize(AnalysisContext context)
        

        
    

    [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DisableSonarLintCodeFixProvider)), Shared]
    public class DisableSonarLintCodeFixProvider : CodeFixProvider
    
        public override ImmutableArray<String> FixableDiagnosticIds => ImmutableArray.Create<string>();

        public override Task RegisterCodeFixesAsync(CodeFixContext context)
        
            return Task.CompletedTask;
        
    

选项 2. 修改项目文件。

组合 hack 以避免分析和重建

:: Starts Visual Studio 2017 with code analysis turned off to minimize [lead time](https://en.wikipedia.org/wiki/Lead_time).
:: Most probably you will need start Visual Studio with code analysis before sharing/uploading/publishing/pushing code. 
:: You may use [Process Hacker] devenv.exe -> Environment -> DevDivCodeAnalysisRunType = Enabled to set value back without restart
::
::
::   To disable Sonar Analysis or other Roslyn analyzers may use next in `.csproj` files:
::   ```
::   <ItemGroup>
::     <Analyzer Condition="'$(DevDivCodeAnalysisRunType)' != 'Disabled'" Include="$(SolutionDir)CodeAnalRules\SonarAnalyzer.CSharp.dll" />
::   </ItemGroup>
::   ```
::
:: Other ways to improve Eval part REPL of:
::
::   Assign hot key to build only this project `Build.BuildSelection` to get only modified project built, I have used `Ctrl + B + B`
::   https://***.com/questions/247745/is-there-a-standard-keyboard-shortcut-to-build-the-current-project-in-visual-stu
::
::   May stop build on first error
::   https://marketplace.visualstudio.com/items?itemName=EinarEgilsson.StopOnFirstBuildError
::
::   Attach to previously attached process via https://marketplace.visualstudio.com/items?itemName=ErlandR.ReAttach
::   Debugger.Break on relevant process start.
:: 
::   Mount [in memory virtual disk](https://sourceforge.net/projects/imdisk-toolkit/), copy code here and work with it. Several time faster than drive.
::
::   Obtain (better hardware) [https://komp.1k.by/utility-harddisks/samsung/Samsung_MZ_V7P512BW-3243815.html + https://ark.intel.com/products/134903/Intel-Core-i9-8950HK-Processor-12M-Cache-up-to-4_80-GHz]
::
::   Dump in one run and analyze later (use your language and dump target as needed) by placing next in all possibly related places:
::   ```
::   System.Diagnostics.Debug.WriteLine("=#-");
::   System.Diagnostics.Debug.WriteLine(new System.Diagnostics.StackFrame().GetMethod().DeclaringType.Name + "." + new System.Diagnostics.StackFrame().GetMethod().Name + "." + new System.Diagnostics.StackFrame(true).GetFileLineNumber() + ";Thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId + ";Ticks:" + System.DateTime.UtcNow.ToString("MM.ddTHH.mm.ss.fffffff", System.Globalization.CultureInfo.InvariantCulture));
::   System.Diagnostics.Debug.WriteLine((null) + " " + (null) + " " + (null)); 
::   System.Diagnostics.Debug.WriteLine("=#");
::   ```
::   (try some `object OR debug dump` assembly from nuget.org)
::   
::   Use Immediate Window to run code when hit breakpoint
::   
set DevDivCodeAnalysisRunType=Disabled
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"

【讨论】:

以上是关于在调试构建期间禁用 SonarLint 分析的主要内容,如果未能解决你的问题,请参考以下文章

使用 sonarlint-cli 进行 C++ 分析

与 SonarLint Intellij 集成后从 SonarQube 生成代码分析报告

SonarLint插件安装

禁用本地构建的代码分析

用SonarQube和SonarLint分析maven项目中的Java代码坏味道

用SonarQube和SonarLint分析maven项目中的Java代码坏味道