dotnet / format

Home for the dotnet-format command
MIT License
1.92k stars 174 forks source link

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. #1546

Closed OoLunar closed 2 years ago

OoLunar commented 2 years ago

Version Used: 6.0.2

Steps to Reproduce: Clone https://github.com/OoLunar/Tomoe/commit/f37434a797fa3c8906302989539b9400b1a3b851 on branch rewrite/DSP_3.0 (Apologies for the non-minimal repro, unsure on how to reproduce)

Expected Behavior: Successfully formatted <x> files.

Actual Behavior:

[lunar@J-Laptop 03:03:54pm ~/Code/Lunar/Tomoe]$ dotnet format -v diag
  The dotnet runtime version is '6.0.2'.
  The dotnet CLI version is '6.0.200'.
  Using MSBuild.exe located in '/usr/src/dotnet-sdk-6.0.2/sdk/6.0.200/'.
  Formatting code files in workspace '/home/lunar/Code/Lunar/Tomoe/Tomoe.csproj'.
  Loading workspace.
    Determining projects to restore...
  All projects are up-to-date for restore.
  Project Tomoe is using configuration from '/home/lunar/Code/Lunar/Tomoe/.editorconfig'.
  Project Tomoe is using configuration from '/home/lunar/Code/Lunar/Tomoe/Migrations/.editorconfig'.
  Project Tomoe is using configuration from '/home/lunar/Code/Lunar/Tomoe/obj/Debug/net6.0/Tomoe.GeneratedMSBuildEditorConfig.editorconfig'.
  Project Tomoe is using configuration from '/usr/src/dotnet-sdk-6.0.2/sdk/6.0.200/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_6_default.editorconfig'.
  Complete in 3209ms.
  Determining formattable files.
  Complete in 778ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 79 analyzers on Tomoe.
  Complete in 4126ms.
  Fixing diagnostics...
  Complete in 5ms.
  Analysis complete in 4131ms.

// This is repeated 253 times.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
// End repeat

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes()
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.<>c.<LoadAnalyzersAndFixers>b__0_0(Assembly assembly)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
   at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.LoadAnalyzersAndFixers(IEnumerable`1 assemblies)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Project project)
   at System.Collections.Immutable.ImmutableDictionary.<>c__DisplayClass9_0`3.<ToImmutableDictionary>b__0(TSource element)
   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 items, MutationInput origin, KeyCollisionBehavior collisionBehavior)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs, Boolean avoidToHashMap)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 keyComparer, IEqualityComparer`1 valueComparer)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Solution solution, FormatOptions formatOptions, ILogger logger)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFormatter.FormatAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.RunCodeFormattersAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()

// This is repeated 253 times.
System.IO.FileNotFoundException:
File name: 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
// End repeat

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
[lunar@J-Laptop 03:04:04pm ~/Code/Lunar/Tomoe]$

Notes: Apologies for the non-minimal repro, as I'm not sure what's causing this in the first place. Additional apologies if this belongs in roslyn-analyzers instead of this repo. I didn't see anything related to CAxxxx, so I'm assuming this belongs here.

sharwell commented 2 years ago

If you add the following to a .cs file, what version does the compiler report in the error message?

#error version
OoLunar commented 2 years ago
#error: 'version' [Tomoe] csharp(CS1029)
Compiler version: '4.2.0-1.22074.8 (979fa475)'. Language version: 10.0.
jcouv commented 2 years ago

As a general comment, it may be useful for dotnet format to print out its own version.

OoLunar commented 2 years ago

My apologies, I didn't realize dotnet format had it's own repo. I'll remember next time.

ranma42 commented 2 years ago

Might be related to https://github.com/dotnet/format/issues/1519 and/or https://github.com/dotnet/format/issues/1433

You might also want to try the workaround proposed in https://github.com/dotnet/format/issues/1433#issuecomment-1055920818

guitarrapc commented 2 years ago

Issue not fixed with https://github.com/dotnet/format/issues/1433#issuecomment-1055920818. Change .NET SDK to 6.0.100 fixed issue for GitHub Actions environment.

snebjorn commented 2 years ago

I'm also getting the ReflectionTypeLoadException - Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp' when running dotnet format.

❯ dotnet format
Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   ...
❯ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.200
 Commit:    4c30de7899

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.200\

Host (useful for support):
  Version: 6.0.2
  Commit:  839cdfb0ec

.NET SDKs installed:
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.1.404 [C:\Program Files\dotnet\sdk]
  5.0.101 [C:\Program Files\dotnet\sdk]
  6.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
sharwell commented 2 years ago

@OoLunar Can you use the Report a Problem feature of Visual Studio to submit a new issue here? In the Additional Attachments section, the item I'm looking for is a binlog, which you can collect by executing a command line build and passing the /bl flag. This will produce an msbuild.binlog file.

OoLunar commented 2 years ago

Unfortunately I cannot do this as I'm on Linux and use Visual Studio Code. Are there any alternatives I can do?

Edit: I failed to read. Will attach the binlog in a moment

JoeRobich commented 2 years ago

@OoLunar There is a fix coming in the next .NET 6 SDK release. In the meantime you can install dotnet format as a global tool to work around this issue (See https://github.com/dotnet/format/issues/1519#issuecomment-1055922333). Closing this issue as duplicate of https://github.com/dotnet/format/issues/1519

nietras commented 1 year ago

I am getting this issue but for 4.5.0.0 in .NET 7.0.202.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
jerohero commented 1 year ago

I got the same issue but (temporarily) resolved it by removing <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> from my .csproj.

oakular commented 1 year ago

@jerohero that fixed it for me, thank you.

tvanantwerp commented 1 year ago

I got the same issue but (temporarily) resolved it by removing <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> from my .csproj.

This also fixed the problem for me, though I'm not sure why. I don't understand why this would be a problem.

flobernd commented 1 year ago

Just encountered the same issue with the .NET 7 SDK while running dotnet-format in a GitHub Action workflow. Any comment from official side here?

manuelesvi commented 1 year ago

Same situation here. Does not work in .NET 7.0:

`dotnet --info

.NET SDK: Version: 7.0.203 Commit: 5b005c19f5

Runtime Environment: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.203\

Host: Version: 7.0.5 Architecture: x64 Commit: 8042d61b17

.NET SDKs installed: 6.0.402 [C:\Program Files\dotnet\sdk] 6.0.408 [C:\Program Files\dotnet\sdk] 7.0.100-rc.1.22431.12 [C:\Program Files\dotnet\sdk] 7.0.203 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.0-rc.1.22427.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.0-rc.1.22426.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.0-rc.1.22427.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: C:\Users\manchax\source\repos\matrix\global.json

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download`

ThibautMagnus commented 1 year ago

Same situation here. Does not work in .NET 7.0:

`dotnet --info

Version: 7.0.5 Architecture: x64 Commit: 8042d61b17

.NET SDKs installed: 7.0.203 [C:\Program Files\dotnet\sdk] 7.0.302 [C:\Program Files\dotnet\sdk]

miroljub1995 commented 1 year ago

@JoeRobich we have similar problem (just version is 4.6.0.0 instead of 4.1.0.0) on github actions with 7.0.302 version. Should this issue be reopened?

JoeRobich commented 1 year ago

The fix for the current SDKs will be in the next servicing release of the SDKs. Sorry the infrastructure that updated the Roslyn version fell behind.

dnaprawa commented 1 year ago

Same for me with .NET 7 SDK

ghost commented 1 year ago

We have the same issue in github actions. The same commit yesterday had passed dotnet format --verify-no-changes, but today it failed with

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.6.0.0, Culture=neutral, PublicKeyToken=31bf3[8](https://github.com/eToro/EtoroOps.Flows/actions/runs/5154865172/jobs/9284376141#step:6:9)56ad364e35'. The system cannot find the file specified.
jdwholmes commented 1 year ago

+1 for the error @konstantinkos-tr is receiving. Same version(s)

viktorfallman commented 1 year ago

Same problem as @konstantinkos-tr

glahaye commented 1 year ago

Same here, with the same version.

ffecteau30 commented 1 year ago

Same problem as @konstantinkos-tr with same version

jmpompeo commented 1 year ago

running into the same issue as @konstantinkos-tr with same version

enriquinho-catalan commented 1 year ago

For those running into this problem. The following worked for me. Use the dotnet-format standalone tool instead of the one bundled into dotnet.

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

Invoke with dotnet-format

For other versions of dotnet (6, 8, etc). Change the version and source: ex:

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

This was sufficient for me, you may need to look for a specific version of the tool if this doesn't work for you.

slonopotamus commented 1 year ago

I switched to Rust and everything worked perfect since then.

On the topic: just wait or use LTS versions of .NET.

timothyeckert commented 2 months ago

This seems to be broken again in v6.0.423.

fcl-9 commented 2 months ago

This seems to be broken again in v6.0.423.

We start facing the same issue today after installing this NuGet Microsoft.Extensions.Telemetry.Abstractions" Version="8.0.0". We are using same version of the SDK v6.0.423.

davgia commented 2 weeks ago

This seems to be broken again in v6.0.423.

We start facing the same issue today after installing this NuGet Microsoft.Extensions.Telemetry.Abstractions" Version="8.0.0". We are using same version of the SDK v6.0.423.

We encounter the same issue after installing System.Diagnostics.DiagnosticSource version 8.0.1. The version of dotnet format is 5.1.250801. We are running GitLab pipelines using the following base image: mcr.microsoft.com/dotnet/sdk:6.0. The command executed is: dotnet format --verify-no-changes $DOTNET_WORKSPACE.