dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

Trying to format .razor files, but getting error "Request textDocument/rangeFormatting failed" #19609

Closed vidz1979 closed 4 years ago

vidz1979 commented 4 years ago

Issue Description

Trying to autoformat .razor files, but getting error.

Steps to Reproduce

Start a new Blazor Server Side project, open a .razor file and try to auto format.

Expected Behavior

Formatted file on save or format document

Actual Behavior

Error on Razor Logs. [Error - 14:04:58] Request textDocument/rangeFormatting failed. Message: Internal Error - System.NullReferenceException: Object reference not set to an instance of an object.

Logs

OmniSharp

Starting OmniSharp server at 05/03/2020 14:03:20 Target: d:\repos_dotnet\BSS_test

OmniSharp server started. Path: C:\Users\Junior.MODAVERAO.vscode\extensions\ms-dotnettools.csharp-1.21.13.omnisharp\1.34.13\OmniSharp.exe PID: 14816

    Starting OmniSharp on Windows 6.2.9200.0 (x64)
    DotNetPath set to dotnet
    Located 2 MSBuild instance(s)
        1: Visual Studio Community 2019 16.4.29709.97 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin"
        2: StandAlone 16.4 - "c:\Users\Junior.MODAVERAO\.vscode\extensions\ms-dotnettools.csharp-1.21.13\.omnisharp\1.34.13\.msbuild\Current\Bin"
    Registered MSBuild instance: Visual Studio Community 2019 16.4.29709.97 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin"
    Detecting Cake files in 'd:\repos\_dotnet\BSS_test'.
    Could not find any Cake files
    No solution files found in 'd:\repos\_dotnet\BSS_test'
    Queue project update for 'd:\repos\_dotnet\BSS_test\BSS_test.csproj'
    Detecting CSX files in 'd:\repos\_dotnet\BSS_test'.
    Could not find any CSX files
    Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
    Loading project: d:\repos\_dotnet\BSS_test\BSS_test.csproj
    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.EditorConfigWorkspaceOptionsProvider, Order: 200
    Configuration finished.
    Omnisharp server running using Stdio at location 'd:\repos\_dotnet\BSS_test' on host 17908.
    Miscellaneous file: d:\repos\_dotnet\BSS_test\Pages\FetchData.razor__virtual.cs added to workspace
    Successfully loaded project file 'd:\repos\_dotnet\BSS_test\BSS_test.csproj'.
    Adding project 'd:\repos\_dotnet\BSS_test\BSS_test.csproj'
    Update project: BSS_test

Razor

[Info - 14:04:58] Finished: Routing Request (30) textDocument/rangeFormatting in 7ms [Error - 14:04:58] Request textDocument/rangeFormatting failed. Message: Internal Error - System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.RazorSyntaxTreeExtensions.<>c.b__11(RazorDirectiveSyntax directive) in //src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/RazorSyntaxTreeExtensions.cs:line 39 at System.Linq.Enumerable.WhereEnumerableIterator1.ToArray() at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.RazorSyntaxTreeExtensions.GetCodeBlockDirectives(RazorSyntaxTree syntaxTree) in /_/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/RazorSyntaxTreeExtensions.cs:line 36 at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.DefaultRazorFormattingService.FormatCodeBlockDirectivesAsync(FormattingContext context) in /_/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DefaultRazorFormattingService.cs:line 107 at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.DefaultRazorFormattingService.FormatAsync(Uri uri, RazorCodeDocument codeDocument, Range range, FormattingOptions options) in /_/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DefaultRazorFormattingService.cs:line 83 at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.RazorFormattingEndpoint.Handle(DocumentRangeFormattingParams request, CancellationToken cancellationToken) in /_/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/RazorFormattingEndpoint.cs:line 99 at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestPostProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestPreProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next) at OmniSharp.Extensions.JsonRpc.RequestRouterBase1.RouteRequest(TDescriptor descriptor, Request request, CancellationToken token) Code: -32602 [Client - 14:04:58] Notifying document 'd:\repos_dotnet\BSS_test\Pages\FetchData.razor' changed 'htmlChanged' [Info - 14:04:58] Finding descriptor for textDocument/didSave [Info - 14:04:58] Found attributes 1, razor::file:///d%3A/repos/_dotnet/BSS_test/Pages/FetchData.razor [Info - 14:04:58] Looking for handler for descriptors textDocument/didSave [Info - 14:04:58] Checking handler textDocument/didSave:Microsoft.AspNetCore.Razor.LanguageServer.RazorDocumentSynchronizationEndpoint [Info - 14:04:58] Registration options OmniSharp.Extensions.LanguageServer.Protocol.Models.TextDocumentSaveRegistrationOptions [Info - 14:04:58] Document Selector [*/.{cshtml,razor}] [Info - 14:04:58] Handler Selected: Microsoft.AspNetCore.Razor.LanguageServer.RazorDocumentSynchronizationEndpoint via [*/.{cshtml,razor}] (targeting OmniSharp.Extensions.JsonRpc.IJsonRpcNotificationHandler`1[[OmniSharp.Extensions.LanguageServer.Protocol.Models.DidSaveTextDocumentParams, OmniSharp.Extensions.LanguageProtocol, Version=0.14.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022]]) [Info - 14:04:58] Starting: Routing Notification textDocument/didSave [Info - 14:04:58] Converting params for Notification textDocument/didSave to OmniSharp.Extensions.LanguageServer.Protocol.Models.DidSaveTextDocumentParams [Info - 14:04:58] Saved Document /d%3A/repos/_dotnet/BSS_test/Pages/FetchData.razor [Info - 14:04:58] Finished: Routing Notification textDocument/didSave in 0ms [Client - 14:05:08] -- Stopping Issue Data Collection--

Workspace information

Razor document:

Expand

```Razor @page "/fetchdata" @using BSS_test.Data @inject WeatherForecastService ForecastService

Weather forecast

This component demonstrates fetching data from a service.

@if (forecasts == null) {

Loading...

} else { @foreach (var forecast in forecasts) { }
Date Temp. (C) Temp. (F) Summary
@forecast.Date.ToShortDateString() @forecast.TemperatureC @forecast.TemperatureF @forecast.Summary
} @code { private WeatherForecast[] forecasts; protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } } ```

Projected CSharp document:

Expand

```C# ////////////////////// Projected CSharp as seen by extension /////////////////////// // #pragma warning disable 1591 namespace BSS_test.Pages { #line hidden using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; #nullable restore #line 1 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using System.Net.Http; #line default #line hidden #nullable disable #nullable restore #line 2 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Authorization; #line default #line hidden #nullable disable #nullable restore #line 3 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Authorization; #line default #line hidden #nullable disable #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Forms; #line default #line hidden #nullable disable #nullable restore #line 5 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Routing; #line default #line hidden #nullable disable #nullable restore #line 6 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Web; #line default #line hidden #nullable disable #nullable restore #line 7 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.JSInterop; #line default #line hidden #nullable disable #nullable restore #line 8 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using BSS_test; #line default #line hidden #nullable disable #nullable restore #line 9 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using BSS_test.Shared; #line default #line hidden #nullable disable #nullable restore #line 3 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" using BSS_test.Data; #line default #line hidden #nullable disable [Microsoft.AspNetCore.Components.RouteAttribute("/fetchdata")] public partial class FetchData : Microsoft.AspNetCore.Components.ComponentBase { #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { #nullable restore #line 1 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" global::System.Object __typeHelper = "/fetchdata"; #line default #line hidden #nullable disable } ))(); ((System.Action)(() => { #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" WeatherForecastService __typeHelper = default!; #line default #line hidden #nullable disable } ))(); ((System.Action)(() => { #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" global::System.Object ForecastService = null!; #line default #line hidden #nullable disable } ))(); } #pragma warning restore 219 #pragma warning disable 0414 private static System.Object __o = null; #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) { #nullable restore #line 10 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" if (forecasts == null) { #line default #line hidden #nullable disable #nullable restore #line 12 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } else { #line default #line hidden #nullable disable #nullable restore #line 26 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" foreach (var forecast in forecasts) { #line default #line hidden #nullable disable #nullable restore #line 29 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.Date.ToShortDateString(); #line default #line hidden #nullable disable #nullable restore #line 30 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.TemperatureC; #line default #line hidden #nullable disable #nullable restore #line 31 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.TemperatureF; #line default #line hidden #nullable disable #nullable restore #line 32 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.Summary; #line default #line hidden #nullable disable #nullable restore #line 33 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } #line default #line hidden #nullable disable #nullable restore #line 36 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } #line default #line hidden #nullable disable } #pragma warning restore 1998 #nullable restore #line 39 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" private WeatherForecast[] forecasts; protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } #line default #line hidden #nullable disable [global::Microsoft.AspNetCore.Components.InjectAttribute] private WeatherForecastService ForecastService { get; set; } } } #pragma warning restore 1591 ////////////////////// Projected CSharp as seen by VSCode /////////////////////// // #pragma warning disable 1591 namespace BSS_test.Pages { #line hidden using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; #nullable restore #line 1 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using System.Net.Http; #line default #line hidden #nullable disable #nullable restore #line 2 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Authorization; #line default #line hidden #nullable disable #nullable restore #line 3 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Authorization; #line default #line hidden #nullable disable #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Forms; #line default #line hidden #nullable disable #nullable restore #line 5 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Routing; #line default #line hidden #nullable disable #nullable restore #line 6 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.AspNetCore.Components.Web; #line default #line hidden #nullable disable #nullable restore #line 7 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using Microsoft.JSInterop; #line default #line hidden #nullable disable #nullable restore #line 8 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using BSS_test; #line default #line hidden #nullable disable #nullable restore #line 9 "/d:/repos/_dotnet/BSS_test/_Imports.razor" using BSS_test.Shared; #line default #line hidden #nullable disable #nullable restore #line 3 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" using BSS_test.Data; #line default #line hidden #nullable disable [Microsoft.AspNetCore.Components.RouteAttribute("/fetchdata")] public partial class FetchData : Microsoft.AspNetCore.Components.ComponentBase { #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { #nullable restore #line 1 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" global::System.Object __typeHelper = "/fetchdata"; #line default #line hidden #nullable disable } ))(); ((System.Action)(() => { #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" WeatherForecastService __typeHelper = default!; #line default #line hidden #nullable disable } ))(); ((System.Action)(() => { #nullable restore #line 4 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" global::System.Object ForecastService = null!; #line default #line hidden #nullable disable } ))(); } #pragma warning restore 219 #pragma warning disable 0414 private static System.Object __o = null; #pragma warning restore 0414 #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) { #nullable restore #line 10 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" if (forecasts == null) { #line default #line hidden #nullable disable #nullable restore #line 12 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } else { #line default #line hidden #nullable disable #nullable restore #line 26 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" foreach (var forecast in forecasts) { #line default #line hidden #nullable disable #nullable restore #line 29 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.Date.ToShortDateString(); #line default #line hidden #nullable disable #nullable restore #line 30 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.TemperatureC; #line default #line hidden #nullable disable #nullable restore #line 31 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.TemperatureF; #line default #line hidden #nullable disable #nullable restore #line 32 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" __o = forecast.Summary; #line default #line hidden #nullable disable #nullable restore #line 33 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } #line default #line hidden #nullable disable #nullable restore #line 36 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" } #line default #line hidden #nullable disable } #pragma warning restore 1998 #nullable restore #line 39 "/d:/repos/_dotnet/BSS_test/Pages/FetchData.razor" private WeatherForecast[] forecasts; protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } #line default #line hidden #nullable disable [global::Microsoft.AspNetCore.Components.InjectAttribute] private WeatherForecastService ForecastService { get; set; } } } #pragma warning restore 1591 // 3 ```

Projected Html document:

Expand

```Html ////////////////////// Projected Html as seen by extension /////////////////////// @page "/fetchdata" @using BSS_test.Data @inject WeatherForecastService ForecastService

Weather forecast

This component demonstrates fetching data from a service.

@if (forecasts == null) {

Loading...

} else { @foreach (var forecast in forecasts) { }
Date Temp. (C) Temp. (F) Summary
@forecast.Date.ToShortDateString() @forecast.TemperatureC @forecast.TemperatureF @forecast.Summary
} @code { private WeatherForecast[] forecasts; protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } } ////////////////////// Projected Html as seen by VSCode /////////////////////// @page "/fetchdata" @using BSS_test.Data @inject WeatherForecastService ForecastService

Weather forecast

This component demonstrates fetching data from a service.

@if (forecasts == null) {

Loading...

} else { @foreach (var forecast in forecasts) { }
Date Temp. (C) Temp. (F) Summary
@forecast.Date.ToShortDateString() @forecast.TemperatureC @forecast.TemperatureF @forecast.Summary
} @code { private WeatherForecast[] forecasts; protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } } // 4 ```

Machine information

VSCode version: 1.42.1 Razor.VSCode version: Unable to find Razor extension version.

dotnet --info

Expand

``` SDK do .NET Core (refletindo qualquer global.json): Version: 3.1.102 Commit: 573d158fea Ambiente de runtime: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.102\ Host (useful for support): Version: 3.1.2 Commit: 916b5cba26 .NET Core SDKs installed: 2.1.200 [C:\Program Files\dotnet\sdk] 2.1.700 [C:\Program Files\dotnet\sdk] 2.2.300 [C:\Program Files\dotnet\sdk] 3.0.100-preview8-013656 [C:\Program Files\dotnet\sdk] 3.1.101 [C:\Program Files\dotnet\sdk] 3.1.102 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview8.19405.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview8-28405-07 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-preview8-28405-07 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download ```

Extensions

Expand

|Extension|Author|Version| |---|---|---| |csharp|ms-dotnettools|1.21.13| |remote-wsl|ms-vscode-remote|0.42.3| |theme-dracula|dracula-theme|2.20.0|;

javiercn commented 4 years ago

@jrvidotti thanks for contacting us.

@NTaylorMullen @ajaybhargavb @ryanbrandenburg thoughts?

ajaybhargavb commented 4 years ago

Thanks for the report @jrvidotti. This is indeed a bug. I've sent https://github.com/dotnet/aspnetcore-tooling/pull/1663 to fix this for the next release. In the meanwhile, you can disable Razor formatting in settings, image

vidz1979 commented 4 years ago

Nice! Thanks a lot for quick answer and solution.

ajaybhargavb commented 4 years ago

@jrvidotti, a new release with the fix has been published. Update your C# extension to v1.21.14 to get it. You can re-enable Razor formatting if you disabled it.