Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable a specific Roslyn analyzer using editorconfig?

According to MSDN's .NET code analysis documentation, we can configure the behavior of Roslyn analyzers through the .editorconfig file.

  • We can configure individual rules: dotnet_diagnostic.<rule ID>.severity = <severity>
  • We can configure category rules: dotnet_analyzer_diagnostic.category-<rule category>.severity = <severity>
  • We can configure all the rules: dotnet_analyzer_diagnostic.severity = <severity>

But I can't find any mention of configuring a specific analyzer's rules.

Let's say I have a directory in my project that shouldn't be analyzed by Roslynator, and another one that shouldn't be analyzed by StyleCop. By this, I mean that every single rule of those analyzers should be disabled for their respective directory. Some of these analyzers have hundreds of rules, disabling all of them individually would take forever.

Note: This is just an example, my question is about whether I can target any arbitrary analyzer, not whether StyleCop or Roslynator have their own settings to do this.

Inside my .editorconfig file, I'd like to be able to do something like:

# Disable all Roslynator analysis in Foo/ directory
[/Foo/*.cs]
dotnet_analyzer.Roslynator.severity = none

# Disable all StyleCop analysis in Bar/ directory
[/Bar/*.cs]
dotnet_analyzer.StyleCop.severity = none

Does such a feature exist? Or do I have to disable all analysis for those directories?

like image 510
micka190 Avatar asked Oct 16 '25 01:10

micka190


1 Answers

There is no direct way for that using .editorconfig. But you can remove the analyzer reference completely for specific projects using an MSBuild target that runs before CoreCompile target.

<Target Name="RemoveAnalyzerReference" BeforeTargets="CoreCompile">
  <ItemGroup>
    <Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.NugetPackageId)'=='ANALYZER_NUGET_PACKAGE_ID'"/>
  </ItemGroup>
</Target>

Note that this applies to a given project, not directory. There is a distinction between the two in that a project can reference something outside its directory.

Also note that you can use Directory.Build.targets if you have multiple projects in a directory where you want this to apply to all of these projects.

like image 70
Youssef13 Avatar answered Oct 18 '25 14:10

Youssef13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!