dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
489 stars 190 forks source link

VSCode C# Extension throws an error when Enter is hit within a .cshtml file #8175

Closed allisonchou closed 1 year ago

allisonchou commented 1 year ago

Transferred from https://github.com/OmniSharp/omnisharp-vscode/issues/5555.

Issue Description

VSCode C# Extension throws an error when Enter is hit. I have tried all keys on a regular English keyboard and the only one giving the error is Enter.

image

Steps to Reproduce

Expected Behavior

Not throwing an error every time the Enter key is being hit.

Actual Behavior

Hitting Enter pops up an error message

Logs

Razor log

[Error - 1:45:58 p.m.] [null]
[Error - 1:45:58 p.m.] Request textDocument/onTypeFormatting failed.
  Message: Unhandled method textDocument/onTypeFormatting
  Code: -32000 
[object Object]

OmniSharp log

[fail]: OmniSharp.Stdio.Host
        ************  Response (0.7742ms) ************ 
{
  "Request_seq": 274,
  "Command": "/close",
  "Running": true,
  "Success": false,
  "Message": "\"System.ArgumentException: 'Index.cshtml__virtual.cs' is not open.\\n   at Microsoft.CodeAnalysis.Workspace.CheckDocumentIsOpen(DocumentId documentId)\\n   at Microsoft.CodeAnalysis.Workspace.<>c.<OnDocumentClosed>b__199_0(Solution oldSolution, ValueTuple`4 data)\\n   at Microsoft.CodeAnalysis.Workspace.SetCurrentSolution[TData](Func`3 transformation, TData data, Action`3 onBeforeUpdate, Action`3 onAfterUpdate)\\n   at Microsoft.CodeAnalysis.Workspace.OnDocumentClosed(DocumentId documentId, TextLoader reloader, Boolean updateActiveContext)\\n   at OmniSharp.OmniSharpWorkspace.CloseDocument(DocumentId documentId) in /home/vsts/work/1/s/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs:line 106\\n   at OmniSharp.Roslyn.CSharp.Services.Files.FileCloseService.Handle(FileCloseRequest request) in /home/vsts/work/1/s/src/OmniSharp.Roslyn.CSharp/Services/Files/FileCloseService.cs:line 25\\n   at OmniSharp.Endpoint.EndpointHandler`2.AggregateResponsesFromLanguageHandlers(ExportHandler`2[] handlers, TRequest request) in /home/vsts/work/1/s/src/OmniSharp.Host/Endpoint/EndpointHandler.cs:line 190\\n   at OmniSharp.Endpoint.EndpointHandler`2.HandleRequestForLanguage(String language, TRequest request, RequestPacket packet) in /home/vsts/work/1/s/src/OmniSharp.Host/Endpoint/EndpointHandler.cs:line 233\\n   at OmniSharp.Endpoint.EndpointHandler`2.Process(RequestPacket packet, LanguageModel model, JToken requestObject) in /home/vsts/work/1/s/src/OmniSharp.Host/Endpoint/EndpointHandler.cs:line 142\\n   at OmniSharp.Stdio.Host.HandleRequest(String json, ILogger logger) in /home/vsts/work/1/s/src/OmniSharp.Stdio/Host.cs:line 258\"",
  "Body": null,
  "Seq": 443,
  "Type": "response"
}

C# log

Empty

Environment information

VSCode version: 1.74.3 C# Extension: 1.25.4

Mono Information OmniSharp using mono: 6.12.0
Dotnet Information .NET SDK: Version: 7.0.101 Commit: bb24aafa11 Runtime Environment: OS Name: manjaro OS Version: OS Platform: Linux RID: manjaro-x64 Base Path: /usr/share/dotnet/sdk/7.0.101/ Host: Version: 7.0.1 Architecture: x64 Commit: 97203d38ba .NET SDKs installed: 5.0.408 [/usr/share/dotnet/sdk] 7.0.101 [/usr/share/dotnet/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 7.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 5.0.17 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.12 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Other architectures found: None Environment variables: DOTNET_ROOT [/usr/share/dotnet] global.json file: Not found
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |better-cpp-syntax|jeff-hykin|1.17.2| |blazorwasm-companion|ms-dotnettools|1.1.0| |cmake|twxs|0.0.17| |cmake-language-support-vscode|josetr|0.0.9| |cmake-tools|ms-vscode|1.13.41| |cpptools|ms-vscode|1.13.9| |cpptools-extension-pack|ms-vscode|1.3.0| |cpptools-themes|ms-vscode|2.0.0| |csharp|ms-dotnettools|1.25.4| |dakara-foldplus|dakara|1.5.2| |dotnet-core-essentials|KishoreIthadi|0.0.8| |doxdocgen|cschlosser|1.4.0| |es7-react-js-snippets|dsznajder|4.4.3| |fabric8-analytics|redhat|0.3.6| |felgo|felgo|1.0.3| |fluent-icons|miguelsolorio|0.0.18| |format-html-in-php|rifi2k|1.7.0| |githistory|donjayamanne|0.6.19| |go|golang|0.37.1| |godot-csharp-vscode|neikeq|0.2.1| |godot-tools|geequlim|1.3.1| |hexeditor|ms-vscode|1.9.10| |html-snippets|abusaidm|0.2.1| |icons|tal7aouy|3.6.1| |inline-fold|moalamri|0.2.2| |intellicode-api-usage-examples|VisualStudioExptTeam|0.2.6| |isort|ms-python|2022.8.0| |java|redhat|1.14.0| |js-jsx-snippets|skyran|11.0.1| |jupyter|ms-toolsai|2022.11.1003412109| |jupyter-keymap|ms-toolsai|1.0.0| |jupyter-renderers|ms-toolsai|1.0.14| |kite|kiteco|0.147.0| |LiveServer|ritwickdey|5.7.9| |mono-debug|ms-vscode|0.16.3| |node-snippets|chris-noring|1.3.3| |php|tal7aouy|1.0.2| |php-debug|xdebug|1.30.0| |php-intellisense|zobo|1.0.13| |php-pack|xdebug|1.0.3| |python|ms-python|2022.20.2| |qtvsctools|tonka3000|0.11.0| |rainbow-csv|mechatroner|3.5.0| |regionfolder|maptz|1.0.20| |remote-containers|ms-vscode-remote|0.266.1| |rust-analyzer|rust-lang|0.3.1377| |sass-indented|syler|1.8.23| |targetframeworksswitcher|yamachu|1.0.3| |unity-code-snippets|kleber-swf|1.3.0| |unity-debug|Unity|3.0.2| |unity-tools|Tobiah|1.2.12| |vetur|octref|0.36.1| |volar|Vue|1.0.24| |vsc-material-theme-icons|equinusocio|2.5.0| |vscode-css-peek|pranaygp|4.2.0| |vscode-diff|fabiospampinato|1.4.2| |vscode-docker|ms-azuretools|1.23.3| |vscode-dotnet-runtime|ms-dotnettools|1.6.0| |vscode-eslint|dbaeumer|2.2.6| |vscode-gradle|vscjava|3.12.6| |vscode-html-css|ecmel|1.13.1| |vscode-icons|vscode-icons-team|12.2.0| |vscode-intelephense-client|bmewburn|1.9.4| |vscode-java-debug|vscjava|0.47.0| |vscode-java-dependency|vscjava|0.21.1| |vscode-java-pack|vscjava|0.25.7| |vscode-java-test|vscjava|0.37.1| |vscode-jupyter-cell-tags|ms-toolsai|0.1.6| |vscode-jupyter-slideshow|ms-toolsai|0.1.5| |vscode-maven|vscjava|0.40.4| |vscode-pylance|ms-python|2023.1.30| |vscode-tailwindcss|bradlc|0.9.6| |vscode-yaml|redhat|1.11.0| |vscodeintellicode|VisualStudioExptTeam|1.2.30| |vue|znck|0.12.0| |yaclock|jameslan|1.0.1|;
ChristopherPerrault commented 1 year ago

I receive this error whenever a code format is attempted. Enabling VSCode's Editor: Format On Type setting makes the error pop up for me every time a line is completed (therefore meaning the Enter key).

I assume it's a problem that comes from the ever-faulty Microsoft C# extension trying to format the code but failing. Going to VSCode's settings and using "@ext:ms-dotnettools.csharp" to navigate to the extension's settings, finding CSharp>Format:Enable, disabling it and restarting didn't fix the issue.

Toggling the setting "Omnisharp: Use Editor Formatting Settings" also did not resolve the issue.

Adjusting the editor in settings.json ("editor.defaultFormatter": "ms-dotnettools.csharp") to use "esbenp.prettier-vscode" (if you have Prettier installed) DID get rid of the C# extension's complaints, but your code will remain unformatted as Prettier does not handle C#/.cshtml files. So while I cannot fix it, I can at least silence it, especially if I want my Format on Type enabled.

davidwengier commented 1 year ago

This was fixed by https://github.com/dotnet/razor/pull/8318