dotnet / dotnet/format

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

Open
#1,627 3 comments 2 reactions 1 assignee Claimed by @JoeRobich View on GitHub
Area-IDE Bug Investigation Required
Dominant language
No language data
Stars
1.9k
Forks
173
Avg merge
10d 13h
Merged PRs (30d)
1

Description

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](https://docs.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference)), 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.