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.71k stars 1.06k forks source link

[dotnet format] 'Using directive is unnecessary' false positive in combination with Roslyn source generator #40427

Open BinaryTENSHi opened 6 months ago

BinaryTENSHi commented 6 months ago

Describe the bug

Running dotnet format --verify-no-changes reports a error IDE0005: Using directive is unnecessary. false positive when the namespace is from a used class that was generated using a Roslyn source generator.

Running dotnet format removes the using {namespace}; which then leads to a compilation error afterward.

To Reproduce

I have created a minimal reproducible example here: https://github.com/BinaryTENSHi/dotnet-format-source-generator

  1. Clone the repository
  2. Run dotnet format --verify-no-changes

Running dotnet format and then dotnet build results in a compilation error.

Further technical details

.NET SDK 8.0.204

Basyras commented 6 months ago

Just start having the same problem... I would also note that the somewhat required dotnet build before dotnet format (to make sure all source generated file are present) doesn't help. I was already using net8 for some time but just recently updated to 8.0.204 SDK so the breaking must happened somewhere between those versions. Can repro locally and on our build machine via azure pipelines too (where same SDK version is installed.)

System Info:

Visual studio: 17.9.6 dotnet sdk: 8.0.204 dotnet runtime: 8.0.4

suhrjo commented 4 months ago

Having the same issue where dotnet format style removes used imports. When running dotnet format style -v detailed I get the output:

error IDE0005: Using directive is unnecessary

Which then removes the import. When running dotnet build after, I receive following output:

error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)

System Info:

dotnet runtime version: 8.0.6 dotnet CLI/SDK version: 8.0.301 running dotnet format with Windows Terminal

jvandertil commented 2 months ago

Just want to bump this as we are also hit by this now. Both on SDK 8.0.400 and 8.0.304 (runtime 8.0.8)