Why can't Microsoft analyzers find Microsoft.CodeAnalysis?
Asked Answered
N

3

22

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.

Nereus answered 25/1, 2019 at 18:7 Comment(1)
See also Could not load file or assembly Microsoft.CodeAnalysisSelfcontained
N
21

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.

Nereus answered 28/1, 2019 at 18:17 Comment(3)
Would like to share that, in my setup: C# project (.NET Standard 2.0) containing Code Analyzers (referencing Microsoft.CodeAnalysis packages v3.9.0) and C# project (.NET Core 3.1) referencing to the Code Analyzer project. I could build on my VS in Windows, but failed to build in my WSL using dotnet build with the same error message: "Could not load file or assembly 'Microsoft.CodeAnalysis". Trying all sorts of workarounds, this solution still works, and consistently across all environments. Thanks @brian-gradin for sharing!Burchette
still worked for me with Microsoft.Net.Compilers==4.2.0, this reference needs to be on the csprog referencing the generator csprojLilla
Microsoft.Net.Compilers.Toolset has superseded Microsoft.Net.Compilers.Alten
F
6

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.

Fredericfrederica answered 3/12, 2020 at 11:56 Comment(0)
M
2

Add on the *.csproj

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <EnableNETAnalyzers>true</EnableNETAnalyzers>
  <AnalysisMode>AllEnabledByDefault</AnalysisMode>
  <AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>
Maddie answered 10/7, 2021 at 1:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.