dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.13k stars 4.04k forks source link

dotnet format incorrectly removing using directives of Blazor/Razor components since 8.0.2 #72160

Open jonashendrickx opened 9 months ago

jonashendrickx commented 9 months ago

Version Used:

.NET 8.0.201

Steps to Reproduce:

I've also attempted to remove lines from _imports.razor, but that didn't seem to make a difference.

Diagnostic Id:

If this is a report about a bug in an analyzer, please include the diagnostic if possible (e.g. "IDE0030").

Expected Behavior:

It should not happen.

Actual Behavior:

When running dotnet format, it seems to remove all the usings everywhere in my unit tests where I do not have a GlobalUsings.cs file.

My _Imports.razor:

...
@using Passwordless.AdminConsole.Components
@using Passwordless.AdminConsole.Components.Shared
@using Passwordless.AdminConsole.Components.Shared.ApexCharts
@using Passwordless.AdminConsole.Components.Shared.Cards
@using Passwordless.AdminConsole.Components.Shared.Icons
@using Passwordless.AdminConsole.Components.Shared.Links
@using Passwordless.AdminConsole.Components.Shared.Stats
...

It would remove the using clauses in my unit test project of the lines above. Is this expected behavior? It was not happening in .NET 8.0.1 or .NET 8.0.0.

In Program.cs I am also seeing the using import removed for the following line:

app.MapRazorComponents<App>();

If I add a GlobalUsings declaration in my unit tests project, it no longer seems to happen.

jonashendrickx commented 9 months ago

Even the default Blazor templates are broken when adding the .editorconfig from dotnet/roslyn or dotnet/aspnetcore.

Folder structure: --Program.cs --Components/App.razor

The documentation does not seem to suggest that both files have to be in the same folder.

JoeRobich commented 9 months ago

Cross-posted from https://github.com/dotnet/format/issues/2119

When I build locally I see the following error:

Source/passwordless-server/src/AdminConsole/Program.cs(212,28): error CS0246: The type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)

I think it is related to https://github.com/dotnet/roslyn/issues/72015. Roslyn wasn't cleanly loading Blazor projects which would lead to some using directives appearing to be unused. This should be fixed in the next released SDKs.

jonashendrickx commented 9 months ago

@JoeRobich Thank you for pointing me in the right direction! Very much appreciated!