英文:
Dotnet-format not running in Github Action due to missing assembly
问题
我已经创建了一个Github操作,用于在CI的一部分中运行 dotnet format --verify-no-changes
以检查格式错误。自从Windows服务器的最新更新后,作业失败,并显示以下错误:
运行 dotnet format --verify-no-changes
dotnet format --verify-no-changes
shell: C:\Program Files\PowerShell\pwsh.EXE -command ". '{0}'"
在加载工作区时遇到警告。将详细选项设置为 'diagnostic' 级别以记录警告。
未经处理的异常: System.Reflection.ReflectionTypeLoadException: 无法加载所请求的一个或多个类型。
无法加载文件或程序集 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'。找不到指定的文件。
无法加载文件或程序集 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'。找不到指定的文件。
...
错误: 进程以退出代码 1 完成。
之前在更新运行镜像之前它是正常工作的,而且在本地运行时也没有任何问题。
到目前为止,我唯一能找到的是 Microsoft.Component.CodeAnalysis.SDK 17.6.33605.316
已经从 17.5.33306.270
进行了更新。
手动为我的解决方案安装 NuGet 包 Microsoft.CodeAnalysis 4.6.0
并没有帮助。
这个问题实际上可能是由最新更新引起的,还是有其他问题?
英文:
I've created a Github action to run dotnet format --verify-no-changes
as part of our CI to check for formatting errors. Since to latest update of the Windows server from
OS Version: 10.0.20348 Build 1668
Image Version: 20230508.3
to
OS Version: 10.0.20348 Build 1726
Image Version: 20230517.1
the job fails with following error:
Run dotnet format --verify-no-changes
dotnet format --verify-no-changes
shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.
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>g____PInvoke|47_0(NativeAssemblyNameParts* pAssemblyNameParts, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Int32 throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
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>g____PInvoke|47_0(NativeAssemblyNameParts* pAssemblyNameParts, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Int32 throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
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)
Error: Process completed with exit code 1.
It was working before the update of the running image and it does run locally without any problems.
So far, the only thing I could find out is that Microsoft.Component.CodeAnalysis.SDK 17.6.33605.316
was updated from 17.5.33306.270
.
Manually installing the NuGet package Microsoft.CodeAnalysis 4.6.0
for my solution did not help.
Can this problem actually come from the latest update or is there anything else wrong?
For reference, here is the code for my action:
# Name of this action
name: Format check on pull request
# Event triggers on push or pull_request
on:
push:
branches:
- master
- development
pull_request:
branches:
- master
- development
jobs:
# ID of the job
dotnet-format:
# Name of the job as ist will be displayed in GitHub
name: Check Code Formatting
# Machine which this action will be run on.
runs-on: windows-latest
# Steps this jobs must take to complete
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Add dotnet-format problem matcher
uses: xt0rted/dotnet-format-problem-matcher@v1
- name: Restore dotnet tools
uses: xt0rted/dotnet-tool-restore@v1
- name: Run dotnet format
run: dotnet format --verify-no-changes
答案1
得分: 2
答案已经在这个链接中提供。
> @ViktorHofer 这里的根本原因是这个存储库没有正确地传递依赖关系。这意味着它错过了Roslyn编译器的版本升级。因此,它引用了Microsoft.CodeAnalysis的4.4.0版本。这意味着在启动时,dotnet format实际上将Microsoft.CodeAnalysis 4.4.0引入到主要的AssemblyLoadContext中。但SDK中的许多分析器已经移至引用4.5.0版本,当这样的分析器加载到dotnet format时,编译器会看到版本不匹配并拒绝分析器。
> 之所以不总是发生这种情况,是因为不同的分析器适用于不同的项目类型。这个bug在控制台应用程序上不会重现,但在几乎所有razor应用程序上都会重现。原因是它们引入了不同的分析器集合。
这个问题可能会在预计于CW25发布的即将推出的更新中得到修复。
英文:
Answer was provided in this link.
> @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 format was effectively pulling in Microsoft.CodeAnalysis at 4.4.0 into the primary AssemblyLoadContext. Many analyzers in the SDK had moved to reference 4.5.0 though and when such an analyzer was loaded into dotnet format the 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 problem will likely be fixed in a coming update expected to drop in CW25.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论