48

I'm trying to work with a large opensource project that has a handful of Roslyn Code Analyzers. When I open the solution Visual Studio uses ~35% CPU for about 15 minutes. Using PerfView I've figured out that the code analyzers being run on the solution are bogging down Visual Studio.

I know it's possible to disable analyzers on a per-project basis but this solution contains over 100 projects so I'd rather not do this one-by-one.

My question(s):

  • Can I disable all Roslyn Analyzers for a given solution to avoid this?
  • Can I disable all Roslyn Analyzers for all solutions in Visual Studio?
JoshVarty
  • 9,066
  • 4
  • 52
  • 80

8 Answers8

27

You can disable analyzers on a per-project basis.

To do it, right click on Project>References>Analyzers in the Solution Explorer and hit Open Active Rule Set

screenshot with the location of Open Active Rule Set

You can disable individual analyzers or entire bundles of analyzers.

checkboxes to disable analyzers

This creates a <ProjectName>.ruleset file and modifies the <ProjectName>.csproj, which means that you will share this configuration with your team unless you exclude these changes from source control.

Note: Changes are applied after you close and re-open the solution.


Changes to the .csproj:

<Project ...>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <CodeAnalysisRuleSet>Example.ruleset</CodeAnalysisRuleSet>

Example.ruleset file:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Rules for WpfApplication1" Description="Code analysis rules for WpfApplication1.csproj." ToolsVersion="14.0">
  <Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp" RuleNamespace="Microsoft.CodeAnalysis.CSharp">
    <Rule Id="AD0001" Action="None" />
    <Rule Id="CS0028" Action="None" />
...
Amadeusz Wieczorek
  • 3,139
  • 2
  • 28
  • 32
  • 3
    The issue here is that there are hundreds of projects in the solution so I was hoping I wouldn't have to disable them one-by-one. They're also under source control I don't control, so when I pull I believe my changes will be overwritten, right? – JoshVarty Apr 06 '16 at 20:33
  • 3
    I hope there's a way to manage analyzers on a scale of the entire solution. Maybe someone will pitch in. – Amadeusz Wieczorek Apr 06 '16 at 21:29
  • To go around the source control issues, create a default `.ruleset` file (so that `.csproj` is updated) and commit these changes. Then, apply your configuration and follow [this guide to keep changes out of git repo](https://stackoverflow.com/questions/8309304/what-is-best-practice-for-keeping-secrets-out-of-a-git-repository) – Amadeusz Wieczorek Apr 06 '16 at 21:30
  • 2
    This doesn't actually stop the CPU usage though. – rollsch Dec 28 '18 at 06:25
13

Try Tools/Options/Text Editor/C#/Advanced and disable full solution analysis. It's only available since VS2015 Update 2.

Tamas
  • 6,260
  • 19
  • 30
13

Try a combo of the following in your csproj or Directory.Build.props files

<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzers>false</RunAnalyzers>

https://learn.microsoft.com/en-us/visualstudio/code-quality/disable-code-analysis?view=vs-2019#net-framework-projects

Simon
  • 33,714
  • 21
  • 133
  • 202
10

Disable below setting in Tools/Options/Text Editor/C#/Advanced and disable use 64-bit process for code analysis under analysis group. it was tested in vs2019.

enter image description here

nixda
  • 2,654
  • 12
  • 49
  • 82
Prashanth T
  • 125
  • 1
  • 5
  • 5
    Sorry for the late posting but this is completely incorrect. This option does not disable code analysis at all. It controls where the code analysis is done. If it is enabled then the analysis will be done in another 64-bit process and the results will be passed to VS process via inter process communication. If the setting is enabled then the analysis will be done in the same process. Some extra info from the VS team can be found here: https://github.com/dotnet/roslyn-sdk/issues/515 – SENya Jul 22 '22 at 18:08
8

As of Visual Studio 2022: Tools > Options > Text Editor > C# > Advanced

enter image description here

aydjay
  • 858
  • 11
  • 25
  • 1
    this was the only thing that worked for me. I had set the analysis attributes to 'false' in all of my .csproj files - but the Code Analysis was still taking up CPU – Dave Black Oct 12 '22 at 20:12
5

It is possible to reference a ruleset file located in the parent folder

<Project ...>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <CodeAnalysisRuleSet>..\Example.ruleset</CodeAnalysisRuleSet>

This way you could define one ruleset for the entire solution.

Emiel Koning
  • 4,039
  • 1
  • 16
  • 18
5

One can place .editorconfig file to a folder for which (and its subfolders) one want to turn off all .NET analyzer warnings:

root = true

[*.cs]

# Disable all .NET analyzers
dotnet_analyzer_diagnostic.severity = none
dotnet_diagnostic.severity = none 

This is useful if one has an external library copy in a codebase and one does not want to modify its source code.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • does it just stop the Warning/Notifications or also STOP the analyzers which eat the CPU/RAM? – S.Serpooshan Jun 03 '23 at 08:48
  • I think it stops the analyzers. Asking the question here https://github.com/dotnet/roslyn/discussions would get you a definitive answer. – MartyIX Jun 15 '23 at 09:34
5

In Visual studio 2022 i've resolved with disabled this two check:

  • enable 'pull' diagnostic(experimental,requires restart)
  • Run code analysis in separate process(requires restart)

enter image description here

Before Roslyn eating a lot of ram, after that visual studio work well.

iugs88
  • 971
  • 10
  • 9