dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.85k stars 670 forks source link

Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp' when triggering code actions #5029

Open 50Wliu opened 2 years ago

50Wliu commented 2 years ago

Issue Description

Given certain code snippets, Omnisharp will reliably throw a System.Reflection.ReflectionTypeLoadException for Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (could not load file or assembly).

Steps to Reproduce

Seems to be as simple as

namespace TestNamespace;

public class Test
{
    private List<int> test;
}

and placing your cursor at the beginning of the word test and asking for code actions. Placing the cursor elsewhere, such as inside the int or List, works as expected. It's only when it's on the word test that it's an issue. This is a .NET 6 project with nullability enabled.

Expected Behavior

Code actions to be returned.

Actual Behavior

No code actions; OmniSharp gives an error response.

Logs

OmniSharp log

My logs are on trace, so that won't work. Here's the startup logs and the full message instead.

Starting OmniSharp server at 1/27/2022, 6:25:23 PM
    Target: c:\Users\winstonliu\repos\redacted\redacted.sln

OmniSharp server started with .NET 6.0.200-preview.22055.15
.
    Path: c:\Users\winstonliu\.vscode\extensions\ms-dotnettools.csharp-1.24.0\.omnisharp\1.38.0-net6.0\OmniSharp.dll
    PID: 29760

[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.DotNetTest
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.DotNetTest, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.Host
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.Host, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.MSBuild
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.MSBuild, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.Roslyn
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.Roslyn, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.Roslyn.CSharp
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.Roslyn.CSharp, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.Script
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.Script, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: OmniSharp.Shared
[dbug]: OmniSharp.CompositionHostBuilder
        Loaded OmniSharp.Shared, Version=1.38.0.0, Culture=neutral, PublicKeyToken=7e5bf7b3b470b509
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded from path: c:\Users\winstonliu\.vscode\extensions\ms-dotnettools.csharp-1.24.0\.razor\OmniSharpPlugin\Microsoft.AspNetCore.Razor.OmniSharpPlugin.dll
[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on Windows 10.0.22544.0 (x64)
[info]: OmniSharp.Services.DotNetCliService
        Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK
[info]: OmniSharp.Services.DotNetCliService
        Using the 'dotnet' on the PATH.
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[dbug]: OmniSharp.CompositionHostBuilder
        .NET SDK requires MSBuild instances version 16.3 or higher
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 1 MSBuild instance(s)
            1: .NET Core SDK 6.0.200 17.1.0 - "C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\"
[dbug]: OmniSharp.CompositionHostBuilder
        MSBuild instance .NET Core SDK 6.0.200 17.1.0 scored at -2147483648
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: .NET Core SDK 6.0.200 17.1.0 - "C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\"
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: Microsoft.CodeAnalysis.Features, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: Microsoft.CodeAnalysis.CSharp.Features, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: Microsoft.CodeAnalysis.ExternalAccess.OmniSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
[trce]: OmniSharp.Services.AssemblyLoader
        Assembly loaded: Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
[trce]: OmniSharp.HostServicesAggregator
        Successfully added Microsoft.CodeAnalysis.Features, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 to host service assemblies.
[trce]: OmniSharp.HostServicesAggregator
        Successfully added Microsoft.CodeAnalysis.CSharp.Features, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 to host service assemblies.
[trce]: OmniSharp.HostServicesAggregator
        Successfully added Microsoft.CodeAnalysis.ExternalAccess.OmniSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 to host service assemblies.
[trce]: OmniSharp.HostServicesAggregator
        Successfully added Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 to host service assemblies.
[trce]: OmniSharp.HostServicesAggregator
        Successfully added Microsoft.AspNetCore.Razor.OmniSharpPlugin.StrongNamed, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to host service assemblies.
[dbug]: OmniSharp.WorkspaceInitializer
        Starting with OmniSharp options: {"RoslynExtensionsOptions":{"EnableDecompilationSupport":false,"EnableAnalyzersSupport":true,"EnableImportCompletion":false,"EnableAsyncCompletion":false,"DocumentAnalysisTimeoutMs":10000,"LocationPaths":null},"FormattingOptions":{"OrganizeImports":false,"EnableEditorConfigSupport":true,"NewLine":"\n","UseTabs":false,"TabSize":4,"IndentationSize":4,"SpacingAfterMethodDeclarationName":false,"SpaceWithinMethodDeclarationParenthesis":false,"SpaceBetweenEmptyMethodDeclarationParentheses":false,"SpaceAfterMethodCallName":false,"SpaceWithinMethodCallParentheses":false,"SpaceBetweenEmptyMethodCallParentheses":false,"SpaceAfterControlFlowStatementKeyword":true,"SpaceWithinExpressionParentheses":false,"SpaceWithinCastParentheses":false,"SpaceWithinOtherParentheses":false,"SpaceAfterCast":false,"SpacesIgnoreAroundVariableDeclaration":false,"SpaceBeforeOpenSquareBracket":false,"SpaceBetweenEmptySquareBrackets":false,"SpaceWithinSquareBrackets":false,"SpaceAfterColonInBaseTypeDeclaration":true,"SpaceAfterComma":true,"SpaceAfterDot":false,"SpaceAfterSemicolonsInForStatement":true,"SpaceBeforeColonInBaseTypeDeclaration":true,"SpaceBeforeComma":false,"SpaceBeforeDot":false,"SpaceBeforeSemicolonsInForStatement":false,"SpacingAroundBinaryOperator":"single","IndentBraces":false,"IndentBlock":true,"IndentSwitchSection":true,"IndentSwitchCaseSection":true,"IndentSwitchCaseSectionWhenBlock":true,"LabelPositioning":"oneLess","WrappingPreserveSingleLine":true,"WrappingKeepStatementsOnSingleLine":true,"NewLinesForBracesInTypes":true,"NewLinesForBracesInMethods":true,"NewLinesForBracesInProperties":true,"NewLinesForBracesInAccessors":true,"NewLinesForBracesInAnonymousMethods":true,"NewLinesForBracesInControlBlocks":true,"NewLinesForBracesInAnonymousTypes":true,"NewLinesForBracesInObjectCollectionArrayInitializers":true,"NewLinesForBracesInLambdaExpressionBody":true,"NewLineForElse":true,"NewLineForCatch":true,"NewLineForFinally":true,"NewLineForMembersInObjectInit":true,"NewLineForMembersInAnonymousTypes":true,"NewLineForClausesInQuery":true},"FileOptions":{"SystemExcludeSearchPatterns":["**/node_modules/**/*","**/bin/**/*","**/obj/**/*","**/.git/**/*","**/.git","**/.svn","**/.hg","**/CVS","**/.DS_Store","**/Thumbs.db"],"ExcludeSearchPatterns":[]},"RenameOptions":{"RenameOverloads":false,"RenameInStrings":false,"RenameInComments":false},"ImplementTypeOptions":{"InsertionBehavior":null,"PropertyGenerationBehavior":null},"DotNetCliOptions":{"LocationPaths":null},"Plugins":{"LocationPaths":null}}
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140
[dbug]: OmniSharp.MSBuild.ProjectSystem
        MSBuild environment: 
Mode: Standalone
RunningTests: False
RunningInVisualStudio: False
MSBuildToolsDirectory32: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15
MSBuildToolsDirectory64: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15
MSBuildSDKsPath: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\Sdks
CurrentMSBuildConfigurationFile: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\MSBuild.dll.config
CurrentMSBuildExePath: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\MSBuild.dll
CurrentMSBuildToolsDirectory: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15
VisualStudioInstallRootDirectory: 
MSBuildExtensionsPath: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15
  "Message": "\"System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.\\r\\nCould not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.\\r\\nCould not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.\\r\\n   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)\\r\\n   at System.Reflection.RuntimeAssembly.get_DefinedTypes()\\r\\n   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()\\r\\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\\r\\n   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()\\r\\n   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)\\r\\n   at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)\\r\\n   at System.Linq.Enumerable.Concat2Iterator`1.ToArray()\\r\\n   at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.CachingCodeFixProviderForProjects.LoadFrom(Project project) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\CachingCodeFixProviderForProjects.cs:line 95\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.CachingCodeFixProviderForProjects.GetAllCodeFixesForProject(ProjectId projectId) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\CachingCodeFixProviderForProjects.cs:line 57\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.BaseCodeActionService`2.GetSortedCodeFixProviders(Document document) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\BaseCodeActionService.cs:line 156\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.BaseCodeActionService`2.AppendFixesAsync(Document document, TextSpan span, IEnumerable`1 diagnostics, List`1 codeActions) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\BaseCodeActionService.cs:line 0\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.BaseCodeActionService`2.CollectCodeFixesActions(Document document, TextSpan span, List`1 codeActions) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\BaseCodeActionService.cs:line 130\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.BaseCodeActionService`2.GetAvailableCodeActions(ICodeActionRequest request) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\BaseCodeActionService.cs:line 88\\r\\n   at OmniSharp.Roslyn.CSharp.Services.Refactoring.V2.GetCodeActionsService.Handle(GetCodeActionsRequest request) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Roslyn.CSharp\\\\Services\\\\Refactoring\\\\V2\\\\GetCodeActionsService.cs:line 38\\r\\n   at OmniSharp.Endpoint.EndpointHandler`2.GetFirstNotEmptyResponseFromHandlers(ExportHandler`2[] handlers, TRequest request) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Host\\\\Endpoint\\\\EndpointHandler.cs:line 218\\r\\n   at OmniSharp.Endpoint.EndpointHandler`2.HandleRequestForLanguage(String language, TRequest request, RequestPacket packet) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Host\\\\Endpoint\\\\EndpointHandler.cs:line 234\\r\\n   at OmniSharp.Endpoint.EndpointHandler`2.Process(RequestPacket packet, LanguageModel model, JToken requestObject) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Host\\\\Endpoint\\\\EndpointHandler.cs:line 143\\r\\n   at OmniSharp.Stdio.Host.HandleRequest(String json, ILogger logger) in D:\\\\a\\\\1\\\\s\\\\src\\\\OmniSharp.Stdio\\\\Host.cs:line 258\\r\\nSystem.IO.FileNotFoundException: 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.\\r\\nFile name: 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'\\r\\nSystem.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.\\r\\nFile name: 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'\"",

C# log

It's blank.

Environment information

VSCode version: 1.63.2 C# Extension: 1.24.0

Dotnet Information .NET SDK (reflecting any global.json): Version: 6.0.200-preview.22055.15 Commit: a3c3a2bf3b Runtime Environment: OS Name: Windows OS Version: 10.0.22544 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\ Host (useful for support): Version: 6.0.1 Commit: 3a25a7f1cc .NET SDKs installed: 5.0.404 [C:\Program Files\dotnet\sdk] 6.0.200-preview.22055.15 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |azure-account|ms-vscode|0.9.11| |azure-pipelines|ms-azure-devops|1.195.0| |azurerm-vscode-tools|msazurermtools|0.15.5| |blazorwasm-companion|ms-dotnettools|1.1.0| |codesnap|adpyke|1.3.4| |cpptools|ms-vscode|1.7.1| |csharp|ms-dotnettools|1.24.0| |debugger-for-edge|msjsdiag|1.0.15| |EditorConfig|EditorConfig|0.16.4| |hexeditor|ms-vscode|1.9.4| |jupyter|ms-toolsai|2021.11.1001550889| |jupyter-keymap|ms-toolsai|1.0.0| |jupyter-renderers|ms-toolsai|1.0.4| |material-theme|zhuangtongfa|3.13.16| |powershell|ms-vscode|2021.12.0| |python|ms-python|2021.12.1559732655| |remote-containers|ms-vscode-remote|0.209.6| |remote-ssh|ms-vscode-remote|0.70.0| |remote-ssh-edit|ms-vscode-remote|0.70.0| |remote-wsl|ms-vscode-remote|0.63.13| |sarif-viewer|MS-SarifVSCode|3.1.1| |theme-bluloco-light|uloco|3.5.0| |vscode-azdo-codereview|Microsoft|1.2021.827002| |vscode-bicep|ms-azuretools|0.4.1124| |vscode-dotnet-runtime|ms-dotnettools|1.5.0| |vscode-edge-devtools|ms-edgedevtools|1.4.2| |vscode-eslint|dbaeumer|2.2.2| |vscode-power-mode|hoovercj|2.2.0| |vscode-remote-extensionpack|ms-vscode-remote|0.21.0| |vscode-research|albertborges|210806.0.2| |vscode-theme-onelight|akamud|2.2.3| |vsliveshare|ms-vsliveshare|1.0.5273| |vulnerability-extension|devprod|1.2022.126001| |wavework|Microsoft|1.2022.118003|;
JoeRobich commented 2 years ago

@50Wliu We will need to take a new O# build to resolve this since we are currently shipping with a 4.0.0 Roslyn. As a workaround you can set your omnisharp.path setting to latest and that will pull an updated O# for you, however it will continually update you with the rolling build.

nightblade9 commented 1 year ago

I'm still getting this issue today. Downgrading .NET SDK and runtime from 7.0 to 6.0 seems to resolve it.