5

I am a bit confused about the various code analyzers for .NET Core.

If you create a default .NET Core web project, you get the Microsoft.CodeAnalysis.Analyzers.

In addition, on NuGet, there is Microsoft.CodeQuality.Analyzers (note the slight difference in the name). After installing it, I see that it contains a lot more rules.

And finally, there is also Microsoft.CodeAnalysis.FxCopAnalyzers, which appears to contain Microsoft.CodeQuality.Analyzers.

So what exactly is the relationship between Microsoft.CodeAnalysis.Analyzers, Microsoft.CodeQuality.Analyzers and Microsoft.CodeAnalysis.FxCopAnalyzers? What should my project have?

P.S. After creating a new project, I search NuGet for Microsoft.CodeAnalysis.Analyzers package (which already exists in my project). It finds it and states my project has v1.1 and that it needs to upgrade it to v2.9.4. However, according to NuGet, v1.1 was released in 2015. This makes no sense as I am using VS2019 and there have been plenty of releases of this analyzer between 2015 and 2019.

However, when I upgrade the package to 2.9.4 - the analyzer has the same 3 rules that v1.1 had. So what exactly am I upgrading?

Mojtaba Nava
  • 858
  • 7
  • 17
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • I'm not quite sure what you mean by MS.CA.Analyzers is created in a default web project. I don't see that. I only see MS.AspNetCore related analyzers. Any way, the [github page](https://github.com/dotnet/roslyn-analyzers) is good place to start. I agree the names are a bit muddled. MS.CA.Analyzers contains analyzers specifically for the APIs in MS.CA, which are the APIs for the C# compiler. I would generally say you only need FxCopAnalyzers, the MS.CQ.Analyzers are brought in by that package. – Mike Zboray Sep 28 '19 at 02:10
  • @MikeZboray From a brand new project. https://i.imgur.com/F74ceUW.png – AngryHacker Sep 30 '19 at 17:07
  • Now there's `Microsoft.CodeAnalysis.CSharp.Analyzers`, `Microsoft.CodeAnalysis.NetAnalyzers`, and `Microsoft.CodeAnalysis.CSharp.NetAnalyzers` in addition to all the others - and don't forget StyleCop rules too - yikes – Dai Jul 18 '21 at 14:07

1 Answers1

4

This is described here: https://github.com/dotnet/roslyn-analyzers/blob/master/README.md

In summary:

  • Microsoft.CodeQuality.Analyzers is the package to use for running analyzers.

    This package contains common code quality improvement rules that are not specific to usage of any particular API

  • Microsoft.CodeAnalysis.Analyzers is for code analysis creators. So for creating an analyzer.
  • Microsoft.CodeAnalysis.FxCopAnalyzers: This is the primary analyzer package for this repo that contains all the ported FxCop code analysis rules (CAxxxx).
Julian
  • 33,915
  • 22
  • 119
  • 174
  • Where are you reading that FxCopAnalyzers are not recommended for new projects? – AngryHacker Sep 30 '19 at 17:16
  • "Microsoft created a set of analyzers, called Microsoft.CodeAnalysis.FxCopAnalyzers, that contains the most important "FxCop" rules from legacy analysis". From https://learn.microsoft.com/en-us/visualstudio/code-quality/install-fxcop-analyzers?view=vs-2019. But I see now it's a bit ambiguous. I think I misinterpreted it. – Julian Sep 30 '19 at 18:17