format: `--fix-analyzers` is broken on .NET 6

Shows the following error:

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load type 'Microsoft.CodeAnalysis.IIncrementalGenerator' from assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.CodeAnalysis.SourceProductionContext' from assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.CodeAnalysis.SourceProductionContext' from assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.CodeAnalysis.SourceProductionContext' from assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
...
<some more similar errors>

Steps to reproduce:

mkdir TestProject
cd TestProject
dotnet new console
dotnet new tool-manifest
dotnet tool install dotnet-format
dotnet tool run dotnet-format --fix-analyzers

Tested on .NET 6.0 Debian and Alpine Docker images

EDIT: Also seems to be broken on Windows (tested in 6.0-nanoserver-20H2 Docker image)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 14
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@LorianColtof #1519 was closed and .NET 6.0.202 SDK has been released

A workaround until new SDKs are released would be to install dotnet-format as a global tool and invoke it with dotnet-format (the hyphen is important).

If you are only using .NET 6 SDKs: dotnet tool install -g dotnet-format --version "6.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json

If you are previewing .NET 7 SDKs: dotnet tool install -g dotnet-format --version "7.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json

@JoeRobich I believe everything for this issue is resolved except for releasing a new version of the global tool on nuget.org.

Am I reading the last comment right, that there will be no fix in an upcoming .NET 6.x SDK (like some upcoming 6.0.300 SDK) and it’ll be only in .NET 7?

What @JoeRobich said was that while a new version of .NET 6 or .NET 7 (whichever you’re using) isn’t released (with the fix for this issue), you can use the commands they’ve provided to get a version of dotnet-format with the fix. Do note that with the version installed through those commands, you’ll need to call dotnet-format (with the hyphen!) instead of dotnet format (without the hyphen).

At least this is my understanding of it.

@JoeRobich, I’ve checked out the branch merges/release/6.x-to-main and built and installed it.

Had the same exception as before:

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Repository: https://github.com/TheGreatSageEqualToHeaven/Loretta Commit: 8434c78b8135c5fce3ba984aece3220ca8e96354 Command: dotnet-format analyzers -v diag Output: https://gist.github.com/GGG-KILLER/ad4ca56313380584cfe865ef7254cb95

@JoeRobich @jmarolf Should this issue be closed now?

I can confirm @JoeRobich 's workaround worked for me:

dotnet tool install -g dotnet-format --version "6.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json

then

dotnet-format -v