IDE0005 fix-all-in-solution erroneously removes imports from NuGets referenced via PackageReference in .NET Framework projects
The following issue was found via testing with dotnet format - if a .NET Framework project uses PackageReference to maintain NuGet references instead of packages.config (MS doc ref), any imports for namespaces from NuGet packages installed on that project will be erroneously deleted by the fix-all-in-solution function for IDE0005 (remove unnecessary import), causing the resulting edited project to fail to build due to missing imports.
Same problem is not present in .NET Framework projects using packages.config - in our case, however, it is not feasible to migrate back to packages.config due to PackageReference being a requirement for some of our dependencies (most notably Refit).
Note that this applies to .NET Framework generally and not just ASP.NET Framework - the same issue appears in .NET Framework Windows service projects, for example.
Potentially related: https://github.com/dotnet/format/issues/1061
Version Used: latest in Visual Studio 2022
Steps to Reproduce:
- Open a .NET Framework solution using
PackageReferenceinstead ofpackages.configin Visual Studio 2022. - If not already installed, install the dotnet-format tool globally.
- Run the following in the Package Manager Console:
dotnet format --severity info
Expected Behavior: Only unused imports removed per IDE0005 analyser
Actual Behavior: Both unused imports and imports for namespaces from NuGet packages removed per IDE0005 analyser
By the way 'Remove unused references' in Visual Studio 2022 works completely wrong. It removes anything it wants including used references.
@Oblomoff please file a different issue (remove unused references is not the same as remind 6 unused usings) with a repro provided. Thanks.
I was fighting this today with one of our solutions. If I ran dotnet format with the Action for IDE0005 at either Error or Warning then a subsequent dotnet build was throwing up CS0103. I had to downgrade to Info to skip this buggy auto-fix.