roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

.NET SDK 5: Could not load file or assembly Microsoft.CodeAnalysis

Open kevinoid opened this issue 2 years ago • 5 comments

Creating a new issue similar (identical?) to #853, which was closed.

Product and Version Used: Roslynator.Analyzers 4.0.0 and .NET SDK 5.0.404

Steps to Reproduce: Run the following:

dotnet new console
dotnet add package Roslynator.Analyzers --version 4.0.0
dotnet build

Actual Behavior: ~350 CS8032 warnings such as:

CSC : warning CS8032: An instance of analyzer Roslynator.CSharp.Analysis.AbstractTypeShouldNotHavePublicConstructorsAnalyzer cannot be created from /home/username/.nuget/packages/roslynator.analyzers/4.0.0/analyzers/dotnet/cs/Roslynator.CSharp.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [/path/to/csproj]

Expected Behavior: No CS8032 warnings.

kevinoid avatar Jan 24 '22 16:01 kevinoid

Hi,

Thanks for creating new issue.

Roslynator 4.0 requires Roslyn 4.0.1 (Microsoft.CodeAnalysis.dll and others).

To use Roslynator analyzers you need one of following tools:

  • VS 2022 (for diagnostics inside IDE)
  • .NET SDK 6.0 (for diagnostics at command line)

josefpihrt avatar Jan 24 '22 16:01 josefpihrt

If you cannot use .NET SDK 6 for some reason you can use Roslynator.Analyzers 3.3.0

josefpihrt avatar Jan 24 '22 16:01 josefpihrt

Thanks @JosefPihrt, that's good to know. My apologies if I missed that in the documentation. I don't suppose there's a way to declare that in the package metadata to give users a more comprehensible error message? If there's nothing to fix here, feel free to close.

kevinoid avatar Jan 24 '22 16:01 kevinoid

So having updated the nuget packages might be why all Roslynator analyzers stopped working for me? Unity is on .NET 4.x, so I'm unable to use the latest release with it?

I'm using Omnisharp with VSCode, latest version, which mentions this in the readme:

image

I'm confused about .NET SDK vs just .NET though :/

I can confirm that reverting Roslynator to 3.3.0 fixed the issue:

image

But I'd of course prefer to stay up to date :(

marcospgp avatar May 16 '22 19:05 marcospgp

Unity is on .NET 4.x, so I'm unable to use the latest release with it?

Unity doesn't support Roslyn 4.x, but that's completely unrelated to the use of .NET Framework. Roslyn still ships net472 binaries with every build. This restriction is partially mentioned here in the Source Generators section: https://docs.unity3d.com/2022.2/Documentation/Manual/roslyn-analyzers.html

sharwell avatar May 17 '22 18:05 sharwell

@sharwell I can't find a reference to Roslyn 4.x support on that Unity docs link, maybe this has since been updated?

marcospgp avatar Oct 03 '23 13:10 marcospgp

@marcospgp It's mentioned indirectly:

Install the Microsoft.CodeAnalysis NuGet package. Your source generator must use Microsoft.CodeAnalysis 3.8 to work with Unity.

Source generators (ISourceGenerator and later IIncrementalGenerator) and analyzers (DiagnosticAnalyzer) are defined in the same assembly, so a version restriction for one implies the same version restriction for the other.

sharwell avatar Oct 03 '23 13:10 sharwell

Roslynator analyzers now supports Roslyn 3.8 (see #1349)

josefpihrt avatar Jan 21 '24 18:01 josefpihrt