sdk: Regression using dotnet format when upgrading from 7.0.203 to 7.0.302
Describe the bug
Running dotnet format or dotnet format --verify-no-changes on a plain webapi c# generated dotnet project errors on 7.0.302 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=31bf3856ad364e35'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
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.Collections.Immutable.ImmutableArray.CreateRange[T](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.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass21_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass18_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__17_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__4_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass7_0.<<UseExceptionHandler>b__0>d.MoveNext()
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
at Microsoft.CodeAnalysis.DefaultAnalyzerAssemblyLoader.DirectoryLoadContext.Load(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
at Microsoft.CodeAnalysis.DefaultAnalyzerAssemblyLoader.DirectoryLoadContext.Load(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
I have verified this happening on both linux and Mac (M1, arm64). The same project with 7.0.203 works.
To Reproduce
- Run
dotnet new webapi - Add the following
global.jsonto the root{ "sdk": { "version": "7.0.203", "rollForward": "major", "allowPrerelease": false } } - Run
dotnet format, observe it works - Adjust
global.jsonto7.0.302 - Run
dotnet format, observe the error above
Reproduced on Mac arm64 and linux.
Exceptions (if any)
See full exception of Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. above
Further technical details
- Include the output of
dotnet --info:
`.NET SDK: Version: 7.0.302 Commit: 990cf98a27
Runtime Environment: OS Name: Mac OS X OS Version: 13.3 OS Platform: Darwin RID: osx.13-arm64 Base Path: /usr/local/share/dotnet/sdk/7.0.302/
Host: Version: 7.0.5 Architecture: arm64 Commit: 8042d61b17
.NET SDKs installed: 7.0.408 [/usr/local/share/dotnet/sdk] 8.0.203 [/usr/local/share/dotnet/sdk] 7.0.302 [/usr/local/share/dotnet/sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.16 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.16 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found: None
Environment variables: Not set
global.json file: /Users/adam.knights/testregs/global.json
Learn more: https://aka.ms/dotnet/info
Download .NET: https://aka.ms/dotnet/download`
- The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version:
Running
dotnet formatin the terminal
Also filed as AB#1825560
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 24
- Comments: 25 (8 by maintainers)
Commits related to this issue
- Reverting dotnet due to format issue https://github.com/dotnet/sdk/issues/32598 — committed to microsoft/Omex by neilr81 a year ago
- Fix format issues in 7.0.3xx SDK The repository references the 4.5.0 version of Roslyn and that is what gets loaded into the default `AssemblyLoadContext`. The analyzers in the 7.0.3xx SDK reference ... — committed to dotnet/format by deleted user a year ago
- Fix format issues in 7.0.3xx SDK The repository references the 4.5.0 version of Roslyn and that is what gets loaded into the default `AssemblyLoadContext`. The analyzers in the 7.0.3xx SDK reference ... — committed to dotnet/format by deleted user a year ago
- deps: enforce 7.0.2xx in global.json disable feature roll-forward due to https://github.com/dotnet/sdk/issues/32598 — committed to BSData/phalanx by amis92 a year ago
- deps: enforce 7.0.2xx in global.json (#121) disable feature roll-forward due to https://github.com/dotnet/sdk/issues/32598 — committed to BSData/phalanx by amis92 a year ago
- ci: disable dotnet-format reason: https://github.com/dotnet/sdk/issues/32598 — committed to BSData/phalanx by amis92 a year ago
- ci: disable dotnet-format reason: https://github.com/dotnet/sdk/issues/32598 — committed to BSData/phalanx by amis92 a year ago
- Re-enable Blazor linting (#1283) # Pull Request ## 🤨 Rationale #1280 ## 👩💻 Implementation It looks like a the build agent began pulling in a new version of the .NET tools. `dotnet -... — committed to ni/nimble by msmithNI a year ago
- Disable format because of https://github.com/dotnet/sdk/issues/32598 — committed to gritcsenko/HomeInventory by gritcsenko a year ago
- Enable format check [dotnet/sdk#32598](dotnet/sdk#32598) — committed to gritcsenko/HomeInventory by gritcsenko a year ago
- Move SDK to 7.0.304 https://github.com/dotnet/sdk/issues/32598 — committed to joinrpg/joinrpg-net by leotsarev a year ago
- Move SDK to 7.0.304 https://github.com/dotnet/sdk/issues/32598 — committed to joinrpg/joinrpg-net by leotsarev a year ago
- Move SDK to 7.0.304 (#2244) https://github.com/dotnet/sdk/issues/32598 — committed to joinrpg/joinrpg-net by leotsarev a year ago
@adam-knights, @Shane32 thanks for reporting. I’m able to reproduce this following the steps in the original post. Effectively use a global.json with the contents
Then simply
We’re looking into this.
I’m experiencing the same issue in a GitHub Actions runner. See script below. Worked fine with 7.0.203.
Error:
I can provide a full copy of the stack trace if desired.
The release today has fixed my issue. Thankyou.
Fixed in SDK 7.0.304.
The new bits are out, on schedule: SDK 7.0.304
This release appears, with a quick test, to fix the issues that I was having with dotnet format.
The second Tuesday of the month is the standard cadence, so likely this time next week.
@ViktorHofer the root cause here is that this repo didn’t flow dependencies correctly. That meant it missed a version bump for the Roslyn compiler. As such it was referencing Microsoft.CodeAnalysis at version 4.4.0. That means on startup
dotnet formatwas effectively pulling inMicrosoft.CodeAnalysisat 4.4.0 into the primaryAssemblyLoadContext. Many analyzers in the SDK had moved to reference 4.5.0 though and when such an analyzer was loaded intodotnet formatthe compiler saw the version mismatch and rejected the analyzer.The reason this doesn’t always happen is different analyzers come into play for different project types. This bug does not reproduce on a console app but will reproduce on pretty much every razor app. The reason being that they bring in different sets of analyzers.
The update to 7.0.302 broke all of our build pipelines because we are validating the code format with
dotnet formatfor Pull Requests in Azure DevOps Server.https://learn.microsoft.com/en-us/dotnet/core/releases-and-support#servicing-updates
@sharwell
dotnet formatis an integral part of the .NET SDK starting with .NET 6, not a separate tool. See documentation here https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format . AFAIK, all tools included with the SDK should be provided in full within the SDK installer. So whatever dependenciesdotnet formathas should be included with the SDK, not distributed on demand.dotnet formatworks fine with prior versions of the .NET SDK. There is simply an issue with .NET SDK 7.0.302, where it appears that the SDK installation did not include all of the necessary dependencies ofdotnet format.If a dependency was introduced with
dotnet formatwhich is disallowed to be distributed with the SDK, then the change withindotnet formatshould be reverted so it only uses dependencies known to be included with the SDK. Otherwise, the dependency should be added to the SDK distribution.For anyone that encounters the new type resolution issue (when using EnforceCodeStyleInBuild) that was uncovered by this, a new issue is available here:
https://github.com/dotnet/sdk/issues/33260
We are having this issue on a linux builder machine (via docker) and on our linux dev boxes.
We are unable to downgrade a the microsoft ubuntu ppa doesn’t seem to have the old version.
Also experiencing this when running
dotnet formatinside a Windows build environmentError
dotnet --info