dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.85k stars 4.01k forks source link

ReflectionTypeLoadException: CodeGenerationParameterSymbol.get_IsParamsArray #72811

Open amcasey opened 4 months ago

amcasey commented 4 months ago

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

sharwell commented 4 months ago

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.

CyrusNajmabadi commented 4 months ago

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.

per-samuelsson commented 4 months ago

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.
abbottdev commented 4 months ago

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]
AnthonyMastrean commented 3 months ago

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.
kylepope-ge commented 3 months ago

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 commented 3 months ago

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.

Tum4ik commented 2 months ago

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;
}
sharwell commented 2 months ago

@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.

mcallaghan-geotab commented 2 months ago

what is the workaround?

sharwell commented 2 months ago

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

Tum4ik commented 2 months ago

@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.

mcallaghan-geotab commented 2 months ago

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

Tum4ik commented 2 months ago

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

smkanadl commented 2 months ago

@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!

ShreyasJejurkar commented 1 month ago

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

CyrusNajmabadi commented 1 month ago

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

smkanadl commented 1 month ago

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 commented 1 month ago

@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.

airbreather commented 1 month ago

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.

CyrusNajmabadi commented 1 month ago

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.

smkanadl commented 1 month ago

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.

CyrusNajmabadi commented 1 month ago

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.

smkanadl commented 1 month ago

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.

CyrusNajmabadi commented 1 month ago

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

smkanadl commented 1 month ago

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 👍

airbreather commented 1 month ago

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.

smkanadl commented 1 month ago

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 commented 1 week ago

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

patoJamriska commented 2 days ago

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.