roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

ReflectionTypeLoadException: CodeGenerationParameterSymbol.get_IsParamsArray

Open amcasey opened this issue 1 year ago • 30 comments

Version Used: 4.10.0-3.24175.2+919d4dbfb0dffb35a702417e28ceea652d248bc6

Steps to Reproduce: Trigger CSharpUseCollectionInitializerCodeFixProvider

Expected Behavior:

Fix applied

Actual Behavior:

"Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.10.10.15805, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation." string

amcasey avatar Mar 29 '24 21:03 amcasey

This is occurring because CodeGenerationParameterSymbol in the code style layer breaks Roslyn's rule that the code style layer must only reference public APIs. ISymbol does not allow public implementations:

https://github.com/dotnet/roslyn/blob/0f35ec3b83a0291003bb64fd7b78de0d423d704f/src/Compilers/Core/Portable/Symbols/ISymbol.cs#L22-L23

This type must be removed from the code style layer and/or updated to no longer implement ISymbol.

sharwell avatar Mar 29 '24 21:03 sharwell

ISymbol does not allow public implementations:

That's not the case. Isymbol does not allow 3rd party impls. It allows first party impls. I know, I created it for that purpose :-)

When this interface was updated, we appropriately updated all our apis. We need to ship that updated API now.

CyrusNajmabadi avatar Mar 30 '24 15:03 CyrusNajmabadi

I have this in VS Code since earlier this morning, after I bumped some assets. I think it was the "C# Base language support" extension I bumped, and it's now on v2.23.15.

It's pretty intrusive too.

Haven't analyzed it much, but it breaks commonly used code actions, e.g. Remove unnecessary usings.

Sample:

[Error - 9:56:18 AM] [LanguageServerHost] System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.9.10.6718, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Composition.Hosting.ContainerConfiguration.<>c.<WithAssemblies>b__16_0(Assembly a)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable`1 types, AttributedModelProvider attributeContext)
   at System.Composition.Hosting.ContainerConfiguration.CreateContainer()
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.MefHostExportProvider.Create(String languageName)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices..ctor(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.Create(HostLanguageServices hostLanguageServices)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.GetRequiredMappedCodeStyleLanguageServices(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.RemoveUnnecessaryImports.AbstractRemoveUnnecessaryImportsCodeFixProvider.RemoveUnnecessaryImportsAsync(Document document, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 350
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 304
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetOperationsCoreAsync(Solution originalSolution, IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 241
   at Microsoft.CodeAnalysis.LanguageServer.Handler.CodeActionResolveHandler.HandleRequestAsync(CodeAction codeAction, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs:line 92
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, IMethodHandler handler, CancellationToken cancellationToken)
System.TypeLoadException: Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.9.10.6718, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.

per-samuelsson avatar Apr 03 '24 08:04 per-samuelsson

Same issue here, using latest preview of the C# DevKit (v1.5.14 (pre-release)) and C# v2.26.13 (pre-release). I'm using Apple Silicon, .NET 8.

Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.9.10.12906, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Composition.Hosting.ContainerConfiguration.<>c.<WithAssemblies>b__16_0(Assembly a)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable`1 types, AttributedModelProvider attributeContext)
   at System.Composition.Hosting.ContainerConfiguration.CreateContainer()
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.MefHostExportProvider.Create(String languageName)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices..ctor(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.Create(HostLanguageServices hostLanguageServices)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.GetRequiredMappedCodeStyleLanguageServices(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.RemoveUnnecessaryImports.AbstractRemoveUnnecessaryImportsCodeFixProvider.RemoveUnnecessaryImportsAsync(Document document, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 356
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 310
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetOperationsCoreAsync(Solution originalSolution, IProgress`1 progress, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs:line 247
   at Microsoft.CodeAnalysis.LanguageServer.Handler.CodeActionResolveHandler.HandleRequestAsync(CodeAction codeAction, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHandler.cs:line 92
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, IMethodHandler handler, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 150
System.TypeLoadException: Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.9.10.12906, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
[Error - 15:16:11] Request codeAction/resolve failed.
  Message: Unable to load one or more of the requested types.
Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.9.10.12906, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
  Code: -32000 
[object Object]

abbottdev avatar Apr 22 '24 14:04 abbottdev

Affects the misplaced using directives quick fix / code action, too.

[Error - 3:13:33 PM] [LanguageServerExtensionManager] Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives.MisplacedUsingDirectivesCodeFixProvider threw an exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.7.9.1717, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Composition.Hosting.ContainerConfiguration.<>c.<WithAssemblies>b__16_0(Assembly a)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable`1 types, AttributedModelProvider attributeContext)
   at System.Composition.Hosting.ContainerConfiguration.CreateContainer()
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.MefHostExportProvider.Create(String languageName)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices..ctor(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.Create(HostLanguageServices hostLanguageServices)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at Microsoft.CodeAnalysis.Host.CodeStyleHostLanguageServices.GetRequiredMappedCodeStyleLanguageServices(HostLanguageServices hostLanguageServices)
   at Microsoft.CodeAnalysis.CodeActions.CSharpCodeFixOptionsProviders.GetCSharpCodeFixOptionsProviderAsync(Document document, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives.MisplacedUsingDirectivesCodeFixProvider.RegisterCodeFixesAsync(CodeFixContext context)
   at Microsoft.CodeAnalysis.CodeFixes.CodeFixService.GetCodeFixesAsync(TextDocument document, TextSpan span, CodeFixProvider fixer, CodeChangeProviderMetadata fixerMetadata, CodeActionOptionsProvider fallbackOptions, ImmutableArray`1 diagnostics, Dictionary`2 uniqueDiagosticToEquivalenceKeysMap, Dictionary`2 diagnosticAndEquivalenceKeyToFixersMap, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Features/CodeFixes/CodeFixService.cs:line 661
   at Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](IExtensionManager extensionManager, Object extension, Func`2 function, T defaultValue, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/ExtensionManager/IExtensionManagerExtensions.cs:line 84
System.TypeLoadException: Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.7.9.1717, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.

AnthonyMastrean avatar May 08 '24 19:05 AnthonyMastrean

I have been having this exact problem on my M1 Mac as well. Happens on pretty much any codeAction which makes those unable to be used entirely. I'm using Visual Studio Code 1.89.0, C# extension 2.28.11, C# Dev Kit extension 1.5.20. Error messages look like the following every time:

[Error - 9:38:55 AM] Request codeAction/resolve failed.
  Message: Unable to load one or more of the requested types.
Method 'get_IsParamsArray' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationParameterSymbol' from assembly 'Microsoft.CodeAnalysis.CodeStyle.Fixes, Version=4.8.9.2411, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
  Code: -32000 
[object Object]

kylepope-ge avatar May 09 '24 16:05 kylepope-ge

More investigation this morning on this get_IsParamsArray error... I discovered this problem is with the C# extension (I disabled my other C# Dev Kit and related extensions). The problem started happening in the 2.23.15 release. The previous release (2.22.5) doesn't have the problem, so I'm going to downgrade for now.

kylepope-ge avatar May 10 '24 16:05 kylepope-ge

I have the same problem when trying to get SyntaxGenerator for my source generation purposes. The creation of AdhocWorkspace causes the exception:

try
{
  var syntaxGenerator = SyntaxGenerator.GetGenerator(new AdhocWorkspace(), LanguageNames.CSharp);
}
catch (ReflectionTypeLoadException e)
{
  throw;
}

Tum4ik avatar May 30 '24 12:05 Tum4ik

@Tum4ik Yours is a separate issue even if it's the same exception. It's caused by attempting to load a different version of Microsoft.CodeAnalysis.dll from the version of Microsoft.CodeAnalysis.Workspaces.dll, which is not supported.

sharwell avatar May 30 '24 13:05 sharwell

what is the workaround?

mcallaghan-geotab avatar Jun 05 '24 17:06 mcallaghan-geotab

There is no workaround currently. These features are currently broken even for ourselves.

sharwell avatar Jun 05 '24 21:06 sharwell

@mcallaghan-geotab I have Visual Studio 17.9 on my another PC, I didn't update that to 17.10 and I don't have the problem there. But @sharwell mentioned I have a different problem. Who knows... maybe you can downgrade to 17.9.

Tum4ik avatar Jun 06 '24 14:06 Tum4ik

(experiencing the problem in Rider fwiw - not visual studio)

mcallaghan-geotab avatar Jun 06 '24 14:06 mcallaghan-geotab

Updating Microsoft.CodeAnalysis.CSharp.Workspaces package from 4.9.2 to 4.10.0 in my source generator project has fixed the problem.

Tum4ik avatar Jun 19 '24 10:06 Tum4ik

@sharwell Do I understand correctly that this won't be fixed asap and I have to wait for the 17.11 release instead? If so, it is really disappointing!

smkanadl avatar Jun 21 '24 09:06 smkanadl

Why is this still not fixed? It's blocking a good experience for Visual Studio Code users.

ShreyasJejurkar avatar Jul 24 '24 14:07 ShreyasJejurkar

@ShreyasJejurkar because the solution is complex and is taking time.

CyrusNajmabadi avatar Jul 24 '24 16:07 CyrusNajmabadi

Why is this still not fixed? It's blocking a good experience for Visual Studio Code users.

And for all Visual Studio Users. Which is annoying as it is not cheap at all.

smkanadl avatar Jul 24 '24 18:07 smkanadl

@ShreyasJejurkar because the solution is complex and is taking time.

If breaking it was that easy and underwent unnoticed, fixing it (especially for a payed product) cannot take 4+ month. That's simply not acceptable.

smkanadl avatar Jul 24 '24 19:07 smkanadl

We worked around it by turning off EnforceCodeStyleInBuild for now. dotnet format --verify-no-changes is almost as good for us, and this issue did seem to completely disappear once we did, so I've been content with waiting for a fix to be done properly.

airbreather avatar Jul 24 '24 19:07 airbreather

fixing it (especially for a payed product) cannot take 4+ month

The fix is not simple. And it will take time to get working.

fixing it (especially for a payed product) cannot take 4+ month

there is no relation on the two. fixes take the time needed to fix them. In this case due to the size and complexity of the fix, that means it takes a while.

--

Fundamentally, the issue is due to your setup loading DLLs that are not compatible with binary changes made in VS. We have no workaround on that until new versions of those DLLs ship that actually work with VS, or we mandate that we will not load dlls from your setup. These are non trivial pieces of work.

CyrusNajmabadi avatar Jul 24 '24 21:07 CyrusNajmabadi

fixing it (especially for a payed product) cannot take 4+ month

The fix is not simple. And it will take time to get working.

fixing it (especially for a payed product) cannot take 4+ month

there is no relation on the two. fixes take the time needed to fix them. In this case due to the size and complexity of the fix, that means it takes a while.

--

Fundamentally, the issue is due to your setup loading DLLs that are not compatible with binary changes made in VS. We have no workaround on that until new versions of those DLLs ship that actually work with VS, or we mandate that we will not load dlls from your setup. These are non trivial pieces of work.

I already understood that it is an complex issue. But again: It was done with one release and no quality check was triggered during the release process that basically all "productivity features" are broken.

This is very surprising. And obviously it is not "my setup" loading the wrong dlls, but many people's setups loading the front dlls.

I understand that there may be no quick fix. But for me (and probably many others) a quite expensive tool lost many of its features.

And it's not that Microsoft is just a 2 man company that struggles with manpower to maintain VS. And my opinion is still: Breaking a payed product and not fixing it for 4+ month. Especially if going back is not easy and would also require to go back in SDK version etc.

smkanadl avatar Jul 25 '24 08:07 smkanadl

It was done with one release and no quality check was triggered during the release process that basically all "productivity features" are broken.

All quality checks passed because we test VS. We do not test every single potential configuration out there. In this case, your configuration pulls in analyzers that were built against an older Roslyn version, despite the version of Roslyn in vs moving forward.

Roslyn, as it is expected to do, made intentional binary changes. In this case, adding new required apis on some interfaces related to new language features. We do this all the time. It is a requirement that downstream apis implement those interfaces if they want to get loaded.

I understand that there may be no quick fix. But for me (and probably many others) a quite expensive tool lost many of its features.

Yes. And we are working on a solution. But, as mentioned, it is not simple or easy.

And it's not that Microsoft is just a 2 man company that struggles with manpower to maintain VS.

The solution is not easy. .net itself requires that if an interface has members that those members be implemented in the types implementing those members.

Fundamentally, this is a request to somehow make that work. Something vs has never supported. We are working on a way to get your particular setup working. But it is non trivial and requires a lot of discussion, implementation time, and validation.

CyrusNajmabadi avatar Jul 25 '24 12:07 CyrusNajmabadi

It was done with one release and no quality check was triggered during the release process that basically all "productivity features" are broken.

All quality checks passed because we test VS. We do not test every single potential configuration out there. In this case, your configuration pulls in analyzers that were built against an older Roslyn version, despite the version of Roslyn in vs moving forward.

I would really like to understand what kind of configuration triggers this issue. And if it is a configuration, I must be very unlucky to be affected on multiple devices and it seems unlikely that a configuration issue has no workaround.

So what configuration do I need to properly use my VS?

PS: I appreciate your effort and time to explain the background of this issue. Please don't take anything of what I said personal, I am just very annoyed that I can not use many features of VS that I usually use on a daily basis.

smkanadl avatar Jul 25 '24 13:07 smkanadl

I would really like to understand what kind of configuration triggers this issue. And if it is a configuration, I must be very unlucky to be affected on multiple devices and it seems unlikely that a configuration issue has no workaround.

So what configuration do I need to properly use my VS?

PS: I appreciate your effort and time to explain the background of this issue. Please don't take anything of what I said personal, I am just very annoyed that I can not use many features of VS that I usually use on a daily basis.

I think it's the use of EnforceCodeStyleInBuild. It tries to load the older version of some analyzers from packages you have loaded into vs, where they don't abide by these changed interfaces.

If you disable that, we won't try to load them and things should hopefully be ok

CyrusNajmabadi avatar Jul 25 '24 13:07 CyrusNajmabadi

So what configuration do I need to properly use my VS?

I think it's the use of EnforceCodeStyleInBuild. It tries to load the older version of some analyzers from packages you have loaded into vs, where they don't abide by these changed interfaces.

If you disable that, we won't try to load them and things should hopefully be ok

I gave it a quick try, removed it from my Directory.Build.props file and indeed the tested code fixes work again.

Why is this not listed as a workaround somewhere? It's fair from ideal, but I can still conditionally enable it for CI builds to enforce clean, no warnings builds.

Many thanks for pointing this out 👍

smkanadl avatar Jul 25 '24 13:07 smkanadl

Why is this not listed as a workaround somewhere? It's fair from ideal, but I can still conditionally enable it for CI builds to enforce clean, no warnings builds.

I posted https://github.com/dotnet/roslyn/issues/72811#issuecomment-2248715193 just as I saw the latest round of comments and noticed that nobody had explicitly posted that on this thread.

airbreather avatar Jul 25 '24 13:07 airbreather

I posted #72811 (comment) just as I saw the latest round of comments and noticed that nobody had explicitly posted that on this thread.

That slipped through. Thanks!

smkanadl avatar Jul 25 '24 13:07 smkanadl

17.11 is out and the issue is still not fixed. What is the ETA?

smkanadl avatar Aug 14 '24 19:08 smkanadl

Having the same issue. tried modifying the sstyle packages and it is a no go. Momentarily using copilot as well to do code checks if needed, but can't use the quick tools for code cleanup on the UI of vscode.

patoJamriska avatar Aug 23 '24 16:08 patoJamriska