vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Cannot format .cshtml files (Razor logs an error)

Open DanteMarshal opened this issue 1 year ago • 1 comments

p.s. CSharp: Report an issue didn't work.

Environment data

dotnet --info output:

.NET SDK:
 Version:           8.0.106
 Commit:            ab2d260803
 Workload version:  8.0.100-manifests.f2afc900

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.106\

.NET workloads installed:
 Workload version: 8.0.100-manifests.f2afc900
 [maui-windows]
   Installation Source: VS 17.8.34408.163
   Manifest Version:    8.0.21/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.21\WorkloadManifest.json
   Install Type:              Msi


Host:
  Version:      8.0.6
  Architecture: x64
  Commit:       3b8b000a0e

.NET SDKs installed:
  8.0.101 [C:\Program Files\dotnet\sdk]
  8.0.106 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

VS Code version:

Version: 1.88.1 (system setup)
Commit: e170252f762678dec6ca2cc69aba1570769a5d39
Date: 2024-04-10T17:41:02.734Z
Electron: 28.2.8
ElectronBuildId: 27744544
Chromium: 120.0.6099.291
Node.js: 18.18.2
V8: 12.0.267.19-electron.0
OS: Windows_NT x64 10.0.22631

C# Extension version: 2.34.12

OmniSharp log

No Log

Razor log

Entering method textDocument/formatting.
[Error - 9:18:25 AM] [null]
[Error - 9:18:25 AM] Request textDocument/formatting failed.
  Message: Specified argument was out of the range of valid values. (Parameter 'Range end line 1 matches or exceeds SourceText boundary 1.')
  Code: -32000 
[object Object]

Steps to reproduce

  1. Open any .cshtml file when the C# extension is enabled
  2. Either manually execute format document or format on save
  3. A VsCode popup appears in the bottom right saying : Request textDocument/formatting failed. Source : C#

Expected behavior

The document is formatted and no error would appear

Actual behavior

The error appears and the document is not formatted

Additional context

This happens even in an empty .cshtml file, In non-empty files only the numbers in the log (e.g. line 1, boundary 1) are different.

DanteMarshal avatar Jun 23 '24 05:06 DanteMarshal

Update : I just updated my VSCode to the following, but the error still occurs.

Version: 1.90.2 (system setup)
Commit: 5437499feb04f7a586f677b155b039bc2b3669eb
Date: 2024-06-18T22:34:26.404Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Windows_NT x64 10.0.22631

DanteMarshal avatar Jun 23 '24 05:06 DanteMarshal

I got the same error. log: exthost.log

Image

Versions: C# - 2.93.22 C# Dev Kit - 1.70.3 VSCode - 1.105.0

frank-reboira avatar Oct 10 '25 07:10 frank-reboira

@frank-reboira Unfortunately I can't repro those errors, even using the same formatting operation on the same file. Could I trouble you to grab some extra logs of the formatting operation, and upload them? Instructions are here: https://github.com/dotnet/razor/blob/main/docs/Formatting.md#turn-on-formatting-logging

NOTE: This is only available in v2.95.x, so might need to wait for the next pre-release

davidwengier avatar Oct 17 '25 01:10 davidwengier

@davidwengier Here is the log,

20251024_161010097_Full_doggie-bus_cshtml.zip

frank-reboira avatar Oct 24 '25 08:10 frank-reboira

Thanks @frank-reboira those were really useful. I think you're hitting this bug: https://github.com/dotnet/razor/issues/11846#issuecomment-2864922878

Can you try going to Settings in VS Code, search for "wrap", and set "HTML > Format: Warp Line Length" to 0, and see if that resolves the issue?

davidwengier avatar Oct 24 '25 11:10 davidwengier

@davidwengier After setting this option, the issue was resolved.

frank-reboira avatar Oct 27 '25 01:10 frank-reboira