dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.74k stars 1.07k forks source link

dotnet format: IDE0051 format issues with Razor code behind files with SDK 8.0.200 #39817

Open afuersch opened 8 months ago

afuersch commented 8 months ago

Since dotnet format has moved to the dotnet SDK repository I'll recreate the issue here to hope for more feedback.

Since the .NET SDK update to version 8.0.200 it seems that the format tool doesn't link the Razor pages with their code behind files anymore.

We use code behind files *.razor.cs for our Razor components. Using the format tool now leads to a IDE0051 unused private member warning. Looks like the Razor code does not exist for the tool.

I've created a repository with the sample Blazor WASM application and moved the C# code of Counter.razor and Weather.razor to a code-behind file to demonstrate the issue.

E.g. the verify no style changes command brings up IDE0051 error and says that the Click event handler defined in the code behind file is unused.

D:\source\github\blazor-net8-dotnet-format> .\format-verify.cmd
D:\source\github\blazor-net8-dotnet-format>dotnet format BlazorApp.sln style --verify-no-changes

D:\source\github\blazor-net8-dotnet-format\BlazorApp\Pages\Counter.razor.cs(9,18): warning IDE0051: Private member 'Counter.IncrementCount' is unused [D:\source\github\blazor-net8-dotnet-format\BlazorApp\BlazorApp.csproj]

Running dotnet format formats the Counter.razor.cs file and removes the IncrementCount() method.

D:\source\github\blazor-net8-dotnet-format>dotnet format BlazorApp.sln -v normal
  Formatting code files in workspace 'D:\source\github\blazor-net8-dotnet-format\BlazorApp.sln'.
  Formatted code file 'D:\source\github\blazor-net8-dotnet-format\BlazorApp\Pages\Counter.razor.cs'.
  Format complete in 5646ms.

The automatically removed method leads to a failed build.

D:\source\github\blazor-net8-dotnet-format>dotnet build BlazorApp.sln
MSBuild version 17.9.4+90725d08d for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
D:\source\github\blazor-net8-dotnet-format\BlazorApp\Pages\Counter.razor(9,44): error CS0103: The name 'IncrementCount' does not exist in the current context [D:\
source\github\blazor-net8-dotnet-format\BlazorApp\BlazorApp.csproj]

Build FAILED.

D:\source\github\blazor-net8-dotnet-format\BlazorApp\Pages\Counter.razor(9,44): error CS0103: The name 'IncrementCount' does not exist in the current context [D:\
source\github\blazor-net8-dotnet-format\BlazorApp\BlazorApp.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:04.26

Environment

.NET

> dotnet --info
.NET SDK:
 Version:           8.0.200
 Commit:            438cab6a9d
 Workload version:  8.0.200-manifests.e575128c

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.200\

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.2
  Architecture: x64
  Commit:       1381d5ebd2

.NET SDKs installed:
  7.0.313 [C:\Program Files\dotnet\sdk]
  7.0.406 [C:\Program Files\dotnet\sdk]
  8.0.102 [C:\Program Files\dotnet\sdk]
  8.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.2 [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

dotnet format

> dotnet format --version
8.0.453106+2651752953c0d41c8c7b8d661cf2237151af33d0
QuintinWillison commented 8 months ago

We're seeing this "break" a number of our codebases that use code behind files (.razor.cs) - with dotnet format locally producing very strange results (removing chunks of code and complaining about not having auto fixes) and matching failures in CI (where we run with dotnet format --verify-no-changes) after upgrading to 8.0.203:

    - uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.203

Workaround for us has been an .editorconfig file in the affected project folder:

root = false

[*.razor.cs]

# These rules appear to be broken for dotnet format after the .NET 8.0.3 upgrade (8.0.203).
# So we're disabling them for now and hopefully they can be re-enabled in time.
dotnet_diagnostic.CA1822.severity = suggestion
dotnet_diagnostic.CA1823.severity = suggestion
dotnet_diagnostic.IDE0044.severity = suggestion
dotnet_diagnostic.IDE0051.severity = suggestion
dotnet_diagnostic.IDE0052.severity = suggestion
dotnet_diagnostic.IDE0060.severity = none

This feels like a pretty big regression. 😞

FlorianGG commented 7 months ago

I completely agree. This regression is quite concerning as it significantly impacts our workflow. Spending time dealing with these issues is slowing us down considerably and affecting our productivity. Hopefully, this will be resolved quickly so we can get back to a stable and efficient development environment.

axylophon commented 7 months ago

Are there any plans to investigate the issue? It is still untriaged and broken.

dotnet format is completely unusable for Blazor right now

TeaDrinkingProgrammer commented 5 months ago

This is a very severe regression. I am very surprised this hasn't even been investigated yet. A formatter breaking code is about as severe as a regression can get.

TeaDrinkingProgrammer commented 5 months ago

We're seeing this "break" a number of our codebases that use code behind files (.razor.cs) - with dotnet format locally producing very strange results (removing chunks of code and complaining about not having auto fixes) and matching failures in CI (where we run with dotnet format --verify-no-changes) after upgrading to 8.0.203:

    - uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.203

Workaround for us has been an .editorconfig file in the affected project folder:

root = false

[*.razor.cs]

# These rules appear to be broken for dotnet format after the .NET 8.0.3 upgrade (8.0.203).
# So we're disabling them for now and hopefully they can be re-enabled in time.
dotnet_diagnostic.CA1822.severity = suggestion
dotnet_diagnostic.CA1823.severity = suggestion
dotnet_diagnostic.IDE0044.severity = suggestion
dotnet_diagnostic.IDE0051.severity = suggestion
dotnet_diagnostic.IDE0052.severity = suggestion
dotnet_diagnostic.IDE0060.severity = none

This feels like a pretty big regression. 😞

Thank you so much for the workaround. Very much appreaciated 🙌 . I just needed to add S1144, S2326 and S3459 because I use Sonarlint.

MackinnonBuck commented 5 months ago

@jaredpar Do you think the culprit is dotnet format, or is this an issue with Razor tooling?

jaredpar commented 5 months ago

Instinct is this is a dotnet format issue.

axylophon commented 2 months ago

Are there any updates?