format icon indicating copy to clipboard operation
format copied to clipboard

IDE0005 fix-all-in-solution erroneously removes imports from NuGets referenced via PackageReference in .NET Framework projects

Open sykesjd opened this issue 4 years ago • 3 comments

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:

  1. Open a .NET Framework solution using PackageReference instead of packages.config in Visual Studio 2022.
  2. If not already installed, install the dotnet-format tool globally.
  3. 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

sykesjd avatar Dec 15 '21 16:12 sykesjd

By the way 'Remove unused references' in Visual Studio 2022 works completely wrong. It removes anything it wants including used references.

Oblomoff avatar Dec 18 '21 10:12 Oblomoff

@Oblomoff please file a different issue (remove unused references is not the same as remind 6 unused usings) with a repro provided. Thanks.

CyrusNajmabadi avatar Dec 18 '21 15:12 CyrusNajmabadi

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.

QuintinWillison avatar May 04 '23 12:05 QuintinWillison