vscode-csharp: Doesn't resolve correct blazor component usings/imports

Issue Description

Steps to Reproduce

  1. Create a new dotnet blazor server project (dotnet new blazorserver -o test)
  2. Open the project in Visual Studio Code, with the C# extension installed
  3. Open any file which uses components (such as Shared/MainLayout.razor) and the _Imports.razor file

(Note that this does not happen in Visual Studio 2019)

Expected Behavior

This extension doesn’t show any errors for the generated code

Actual Behavior

All components used in Blazor html markup and in C# @code sections (not present in the generated code) are highlighted in blue or red (info/error) because the extension fails to identify and resolve correct @using directives in the top-level _Imports.razor file

Update

I have disabled all extensions except for the OmniSharp C# one and it is still reporting every single component usage as an error.

Logs

OmniSharp log

OmniSharp log

Starting OmniSharp server at 29.5.2021, 21:14:17
    Target: c:\Users\user\Documents\test\test

OmniSharp server started. Path: c:\Users\user.vscode\extensions\ms-dotnettools.csharp-1.23.12.omnisharp\1.37.10\OmniSharp.exe PID: 11868

    Starting OmniSharp on Windows 6.2.9200.0 (x64)
    DotNetPath set to dotnet
    Located 2 MSBuild instance(s)
        1: Visual Studio Community 2019 16.9.31205.134 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin"
        2: StandAlone 16.11.0 - "c:\Users\user\.vscode\extensions\ms-dotnettools.csharp-1.23.12\.omnisharp\1.37.10\.msbuild\Current\Bin"
    Registered MSBuild instance: Visual Studio Community 2019 16.9.31205.134 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin"
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140
    Detecting Cake files in 'c:\Users\user\Documents\test\test'.
    Could not find any Cake files
    No solution files found in 'c:\Users\user\Documents\test\test'
    Queue project update for 'c:\Users\user\Documents\test\test\test.csproj'
    Detecting CSX files in 'c:\Users\user\Documents\test\test'.
    Could not find any CSX files
    Configuration finished.
    Omnisharp server running using Stdio at location 'c:\Users\user\Documents\test\test' on host 7776.
    Loading project: c:\Users\user\Documents\test\test\test.csproj
    Successfully loaded project file 'c:\Users\user\Documents\test\test\test.csproj'.
    Adding project 'c:\Users\user\Documents\test\test\test.csproj'
    Update project: test

C# log

C# log

*empty log*

Environment information

VSCode version: 1.56.2 C# Extension: 1.23.12

Dotnet Information .NET SDK: Version: 5.0.202 Commit: db7cc87d51

Runtime: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.202\

Host (useful for support): Version: 5.0.5 Commit: 2f740adc14

.NET SDKs installed: 3.1.408 [C:\Program Files\dotnet\sdk] 5.0.202 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.5 [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
aspnetcorerazor-html-css-class-completion kevin-chatham 1.0.3
auto-close-tag formulahendry 0.5.10
auto-complete-tag formulahendry 0.1.0
auto-rename-tag formulahendry 0.1.6
auto-using Fudge 0.7.15
autoimport steoates 1.5.4
blazorwasm-companion ms-dotnettools 1.1.0
bracket-pair-colorizer-2 CoenraadS 0.2.0
browserslist webben 1.1.0
code-spell-checker streetsidesoftware 1.10.2
codespaces GitHub 0.10.4
comment-ts salbert 1.0.21
csharp ms-dotnettools 1.23.12
csharp-colors logerfo 0.1.8
cshtml fireside21 0.1.3
debugger-for-chrome msjsdiag 4.12.12
docfx-assistant tintoy 0.1.5
docomment k–kato 0.1.20
dotenv mikestead 1.0.1
easyzoom NabeelValley 0.1.0
EditorConfig EditorConfig 0.16.4
fluent-icons miguelsolorio 0.0.11
format-selection-as-html adrianwilczynski 2.3.0
git-graph mhutchie 1.30.0
github-markdown-preview bierner 0.0.2
github-vscode-theme GitHub 4.1.1
gitignore codezombiech 0.7.0
gitlens eamodio 11.4.1
headwind heybourn 1.7.0
html-css-class-completion Zignd 1.20.0
html-slim-scss-css-class-completion gencer 1.7.8
JSDocTagComplete HookyQR 0.0.2
markdown-checkbox bierner 0.1.3
markdown-emoji bierner 0.1.0
markdown-preview-github-styles bierner 0.2.0
markdown-yaml-preamble bierner 0.0.4
material-icon-theme PKief 4.7.0
material-product-icons PKief 1.0.4
material-theme zhuangtongfa 3.10.14
npm-intellisense christian-kohler 1.3.1
postcss csstools 1.0.9
prettier-vscode esbenp 6.4.0
quokka-vscode WallabyJs 1.0.369
razor-plus austincummings 0.1.4
remote-containers ms-vscode-remote 0.177.2
remote-wsl ms-vscode-remote 0.56.3
svelte-autoimport pivaszbs 1.0.4
svelte-extractor proverbialninja 0.0.3
svelte-intellisense ardenivanov 0.7.1
svelte-vscode svelte 105.0.0
tailwind-docs austenc 1.4.0
tailwind-sass-syntax macieklad 1.3.0
theme-dracula dracula-theme 2.22.3
vscode-docker ms-azuretools 1.13.0
vscode-dotnet-runtime ms-dotnettools 1.1.0
vscode-eslint dbaeumer 2.1.20
vscode-firefox-debug firefox-devtools 2.9.4
vscode-html-css ecmel 1.10.2
vscode-html-scss P-de-Jong 0.0.42
vscode-language-babel mgmcdermott 0.0.31
vscode-npm-script eg2 0.3.21
vscode-pull-request-github GitHub 0.26.0
vscode-sqlite alexcvzz 0.12.0
vscode-stylelint stylelint 0.86.0
vscode-tailwindcss bradlc 0.6.6
vscode-versionlens pflannery 1.0.9
vscode-yaml redhat 0.19.2
vscodeintellicode VisualStudioExptTeam 1.2.14
zoombar-vscode wraith13 1.4.0

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 22
  • Comments: 34 (7 by maintainers)

Most upvoted comments

Hi All, had the same issue. I found a work-around in VS Code by downgrading the C# extension from the current version 1.23.12 to 1.23.11. You can do this in the the extensions panel by clicking on the config button for the C# extension and click ‘Install another version’, then select the previous version. Version 1.23.12 was released about 12 days ago, and obviously has broken razor intellisense

This issue seems to be back with the new v2 C# extension. The _Imports.razor file it’s complete ignored, then the intellisense go nutz.

The downgrade it’s a working around, going back to v1.26.0 solved it for me.

@blackshot, @PeterDraex no resolution on this?

@davidwengier I’m facing the same issue right now and my project is open. I’ve created a snapshot branch here.

It’s working like a charm in VS, hit this issue as I’m trying out VS Code as alternative:

  1. Clone a fresh repo
  2. Restore MAUI workloads
  3. Rebuild the solution, first time it takes a while
  4. Open a file - EventComponent.razor will do.

Symptoms seem consistent with what’s described here. Specifically:

  1. _Imports.razor usings aren’t resolved
  2. Inline components (no code behind class) don’t seem to be registered as namespaces by the compiler.
  3. Custom components are not resolved (RZ10012) - this is true even if I add the relevant using in the same file.

If you need something else to debug this, please share.

I’m also experiencing the same issue but I found a workaround and hope it helps troubleshoot the issue. Just like others have said I’m also having issues with some components not being picked up by vscode (RZ10012) and I noticed that some work ok, while others don’t. After some experimentation, I noticed that the components that were being picked-up correctly were using a .cs partial class and they explicitly inherited from ComponentBase in the c# partial class cs file. I started refactoring all components to have a cs file with a partial class, even those without any code block, and one by one they started working as soon as I added the ComponentBase inheritance in the c# partial class. This is far from a solution because while CS code behind files is great for complex components its not ideal for very simple components or components that don’t even require a “@code” block. Hope this helps you get to the bug and find a solution.

Also, before finding this workaround I noticed that components that were not being picked-up started to do so after opening the component file in vscode.

@blackshot, @PeterDraex no resolution on this?

@davidwengier I’m facing the same issue right now and my project is open. I’ve created a snapshot branch here.

It’s working like a charm in VS, hit this issue as I’m trying out VS Code as alternative:

  1. Clone a fresh repo
  2. Restore MAUI workloads
  3. Rebuild the solution, first time it takes a while
  4. Open a file - EventComponent.razor will do.

Symptoms seem consistent with what’s described here. Specifically:

  1. _Imports.razor usings aren’t resolved
  2. Inline components (no code behind class) don’t seem to be registered as namespaces by the compiler.
  3. Custom components are not resolved (RZ10012) - this is true even if I add the relevant using in the same file.

If you need something else to debug this, please share.

@blackshot To set verbosity, open File -> Preferences -> Settings and search for razor.trace

This issue seems to be back with the new v2 C# extension. The _Imports.razor file it’s complete ignored, then the intellisense go nutz.

The downgrade it’s a working around, going back to v1.26.0 solved it for me.

Same for me.

same here. _Imports.razor: error finding .Shared folder; App.razor: Router component can’t be found;

Code template generated by dotnet new command

Compiling without a problem.

After update to a new version of VS Code Insiders, I am seeing an System.InvalidOperationException in C# Output window, coming from LanguageServerHost.

Expand logs
Using dotnet configured on PATH
Dotnet path: C:\Program Files\dotnet\dotnet.exe
Activating C# + C# Dev Kit + C# IntelliCode...
waiting for named pipe information from server...
[stdout] {"pipeName":"\\\\.\\pipe\\70987ca1-88df-4050-a63c-98fd16286a13"}
received named pipe information from server
attempting to connect client to server...
client has connected to server
[Info  - 1:15:06 PM] [Program] Language server initialized
[Error - 1:15:16 PM] [LanguageServerHost] System.InvalidOperationException: Was not able to get hello world response from remote - line 39
   at Roslyn.Utilities.Contract.Fail(String message, Int32 lineNumber) in /_/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/Contract.cs:line 154
   at Microsoft.CodeAnalysis.LanguageServer.BrokeredServices.Services.HelloWorld.RemoteHelloWorldProvider.SayHelloToRemoteServerAsync(CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/BrokeredServices/Services/HelloWorld/RemoteHelloWorldProvider.cs:line 39
   at Microsoft.CodeAnalysis.LanguageServer.BrokeredServices.ServiceBrokerFactory.CreateAndConnectAsync(String brokeredServicePipeName) in /_/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/BrokeredServices/ServiceBrokerFactory.cs:line 82
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 136
[Error - 1:16:36 PM] [LanguageServerHost] System.ArgumentException: Range={ Start={ Line=111, Character=37 }, End={ Line=900, Character=0 } }. text.Length=29910. text.Lines.Count=785
 ---> System.ArgumentOutOfRangeException: The requested line number 900 must be less than the number of lines 785. (Parameter 'Line')
   at Microsoft.CodeAnalysis.Text.TextLineCollection.GetPosition(LinePosition position) in /_/src/Compilers/Core/Portable/Text/TextLineCollection.cs:line 67
   at Microsoft.CodeAnalysis.Text.TextLineCollection.GetTextSpan(LinePositionSpan span) in /_/src/Compilers/Core/Portable/Text/TextLineCollection.cs:line 78
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.RangeToTextSpan(Range range, SourceText text) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 298
   --- End of inner exception stack trace ---
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.RangeToTextSpan(Range range, SourceText text) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 298
   at Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens.SemanticTokensHelpers.ComputeSemanticTokensDataAsync(ClientCapabilities capabilities, Document document, Range[] ranges, ClassificationOptions options, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs:line 85
   at Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens.SemanticTokensHelpers.HandleRequestHelperAsync(IGlobalOptionService globalOptions, SemanticTokensRefreshQueue semanticTokensRefreshQueue, Range[] ranges, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs:line 50
   at Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens.SemanticTokensRangeHandler.HandleRequestAsync(SemanticTokensRangeParams request, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensRangeHandler.cs:line 45
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 176
[Error - 1:16:36 PM] Request textDocument/semanticTokens/range failed.
  Message: Range={ Start={ Line=111, Character=37 }, End={ Line=900, Character=0 } }. text.Length=29910. text.Lines.Count=785
  Code: -32000 
[object Object]

Also, in the file layout you included it looks like maybe the “Componentes” folder starts with a space? Or is that just a typo?

it was a typo writting here in github, foldername is fine

Thanks, i updated the comment with the verbose logs

Can reproduce as of the latest prerelease. Also getting 'RenderTreeBuilder' does not contain a definition for 'AddComponentParameter' and no accessible extension method 'AddComponentParameter' accepting a first argument of type 'RenderTreeBuilder' could be found (are you missing a using directive or an assembly reference?) repeatedly from intellisense.

Oddly enough downgrading to 1.23.11 fixed the issue, but then I upgraded back to 1.23.12 and it keeps working. 😵

@NTaylorMullen I attached my OmniSharp log in my issue. It doesn’t throw an unhandled exception of any kind, and I’m targeting .NET 5, not 6, so I don’t think #4573 relates to this one (this appears to describe the same issue though: dotnet/aspnetcore#33350)