Reputation: 2744
Is it possible (in Visual Studio using SonarLint extension) to disable Sonar Analyzers during Debug build, but keep them enabled in Release build? The reason is that connecting the solution to SonarQube has drastically increased the build time.
Upvotes: 7
Views: 3926
Reputation: 939
Options 1. Dummy dlls.
If you have SonarLint as part of .csproj
files as next:
<ItemGroup>
<Analyzer Include="$(SolutionDir)CodeAnalRules\SonarAnalyzer.CSharp.dll" />
<Analyzer Include="$(SolutionDir)CodeAnalRules\SonarAnalyzer.dll" />
</ItemGroup>
Compile next code into assembly. Copy and name it as SonarAnalyzer.CSharp.dll
and SonarAnalyzer.dll
. Replace existing Sonar assemblies with this. Return back for 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;
}
}
}
Option 2. Modify project files.
Combined hack to avoid analysis and rebuilds
:: 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://stackoverflow.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"
Upvotes: -1
Reputation: 2744
I ended up modifying the .csproj files to remove the analyzers if I'm building the solution from within Visual Studio in Debug configuration. That way, sonarlint does not complain that rules are outdated, nor does it get affected by updates. I got the answer from 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>
Upvotes: 7
Reputation: 1512
The only way I can think of is to duplicate your rulesets and lower (disable) rules for the Debug mode but keep the original ruleset for release mode. Note that this is going to be painful because SonarLint will them complain that you have lower strength of the ruletset compared to the Quality Profile on SonarQube. Besides, every time you will update it will probably break this manual tweak.
Upvotes: 2