Skip to content

[Breaking change]: NU1510 is raised for direct references pruned by NuGet #45462

Closed
@ericstj

Description

@ericstj

Description

When referencing a package that is overlaps with the framework referenced you may will receive a NU1510 warning when that reference is not required.

See https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#prunepackagereference https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1510

@nkolev92 @OliaG

Version

.NET 10 Preview 1

Previous behavior

Previously the PackageReference would be allowed, but the contents of the package would be ignored by the .NET SDK in favor of the framework-provided library.

New behavior

Instead of the contents of the package being removed by the .NET SDK, the package will now be removed by NuGet.

NuGet warns if you have a direct reference that's not required so that you might clean up the unnecessary reference.

Type of breaking change

  • Binary incompatible: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
  • Behavioral change: Existing binaries might behave differently at run time.

Reason for change

Removing packages that are not used helps reduce the maintenance burden on developers. They won't be asked to update the packages, the packages won't be downloaded and restored just to be excluded later, and the packages won't appear in the project's build artifacts (deps files, SBOM).

Notifying users when they directly reference packages will help reduce the need of updating these unused packages.

Recommended action

If you only target framworks where the package has been pruned, then remove the package reference completely.

In a multi-targeting project, if you still require the package on some frameworks, then add a condition to only reference on those frameworks. Note - this may be improved in later versions with NuGet/Home#14196.

For example:

<ItemGroup>
    <!-- reference 8.0 System.Text.Json when targeting things older than .NET 8 -->
    <PackageReference Include="System.Text.Json"  Version="8.0.5" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />

    <!-- reference 10.0 System.Linq.AsyncEnumerable when targeting things older than .NET 10 -->
    <PackageReference Include="System.Linq.AsyncEnumerable"  Version="10.0.0-preview.2.25163.2" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net.10'))" />

    <!-- Reference System.Memory on frameworks not compatible with .NET Core 2.1 nor .NETStandard 2.1 -->
    <PackageReference Include="System.Memory" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netcoreapp2.1')) and !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))" />
</ItemGroup>

Feature area

SDK

Affected APIs

No response


Associated WorkItem - 410919

Metadata

Metadata

Assignees

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.breaking-changeIndicates a .NET Core breaking changein-prThis issue will be closed (fixed) by an active pull request.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions