Brian Gradin
Brian Gradin

Reputation: 2275

Why can't Microsoft analyzers find Microsoft.CodeAnalysis?

I'm trying to add Microsoft.CodeAnalysis.FXCopAnalyzers (latest stable version) to my ASP.NET project. When I install it via NuGet, I get a ton of errors like:

An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AbstractTypesShouldNotHaveConstructorsAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.. An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AvoidEmptyInterfacesAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.. An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.CancellationTokenParametersMustComeLastAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

Note: For the sake of conciseness, I've omitted 127 of the errors. They are all very similar. One thing to note, though, is that not all of the analyzers are in the Microsoft.CodeQuality namespace; some are in Microsoft.NetCore, and some are in Microsoft.NetFramework.

So I say ok, I must need to install Microsoft.CodeAnalysis v2.6.0.0 first. Seems weird that that wouldn't be a dependency that's automatically installed by NuGet but whatever.

So I revert all my changes in Git, reopen the solution, install Microsoft.CodeAnalysis v2.6.0.0, and then install the latest stable version of Microsoft.CodeAnalysis.FXCopAnalyzers.

Same errors!

When I look in packages/Microsoft.CodeAnalysis.2.6.0, I notice that there isn't a .dll anywhere in there. What is the correct way to install this? Is installing Microsoft.CodeAnalysis even the correct solution? Why aren't the dependencies of Microsoft.CodeAnalysis.FXCopAnalyzers automatically installed when you install it? Isn't that the whole point of a package manager? I have so many questions and so few answers. Please help.

Upvotes: 22

Views: 26528

Answers (3)

Jhonny Ramirez Zeballos
Jhonny Ramirez Zeballos

Reputation: 3156

Add on the *.csproj

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <EnableNETAnalyzers>true</EnableNETAnalyzers>
  <AnalysisMode>AllEnabledByDefault</AnalysisMode>
  <AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>

Upvotes: 2

boltzmanncte
boltzmanncte

Reputation: 93

For anyone running into this problem in the future just to point out that one of the reasons this may happen is because VS is outdated.

A new installation or update of VS may solve the problem if the solution above doesn't.

Upvotes: 6

Brian Gradin
Brian Gradin

Reputation: 2275

This issue on the Roslyn analyzers github project suggested adding a reference to Microsoft.Net.Compilers v2.6.1. I can't find this package anywhere in the dependency chain for Microsoft.CodeAnalysis.FXCopAnalyzers, but I noticed that I had version 1.0.0 installed in my project. I removed the package and installed v2.10.0, and now everything seems to be working as expected.

Upvotes: 21

Related Questions