Why can't Microsoft analyzers find Microsoft.CodeAnalysis?

Brian Gradin picture Brian Gradin · Jan 25, 2019 · Viewed 7.7k times · Source

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.

Answer

Brian Gradin picture Brian Gradin · Jan 28, 2019

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.