|
| 1 | +--- |
| 2 | +title: "Breaking change - PrunePackageReference automatically marks direct prunable references with PrivateAssets=all and IncludeAssets=none" |
| 3 | +description: "Learn about the breaking change in .NET 10 Preview 7 where PrunePackageReference automatically marks directly prunable PackageReference with PrivateAssets=all and IncludeAssets=none." |
| 4 | +ms.date: 01/03/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +--- |
| 7 | + |
| 8 | +# PrunePackageReference automatically marks direct prunable references with PrivateAssets=all and IncludeAssets=none |
| 9 | + |
| 10 | +Starting in .NET 10 Preview 7, the PrunePackageReference feature automatically marks directly prunable PackageReference items with `PrivateAssets=all` and `IncludeAssets=none` attributes. This prevents these packages from appearing in generated dependency lists for multi-targeting packages. |
| 11 | + |
| 12 | +## Version introduced |
| 13 | + |
| 14 | +.NET 10 Preview 7 |
| 15 | + |
| 16 | +## Previous behavior |
| 17 | + |
| 18 | +In .NET 10 Preview 6 and earlier, directly prunable PackageReference items would generate an [`NU1510` warning](/nuget/reference/errors-and-warnings/nu1510) but would still appear in the generated *.nuspec* dependencies for all target frameworks, even those where the package is provided by the platform. |
| 19 | + |
| 20 | +For example, a multi-targeting project with the following configuration: |
| 21 | + |
| 22 | +```xml |
| 23 | +<PropertyGroup> |
| 24 | + <TargetFramework>net9.0;net472</TargetFramework> |
| 25 | +</PropertyGroup> |
| 26 | + |
| 27 | +<ItemGroup> |
| 28 | + <PackageReference Include="System.Text.Json" Version="9.0.4" /> |
| 29 | +</ItemGroup> |
| 30 | +``` |
| 31 | + |
| 32 | +Would generate a *.nuspec* file with dependencies for both target frameworks: |
| 33 | + |
| 34 | +```xml |
| 35 | +<dependencies> |
| 36 | + <group targetFramework=".NETFramework4.7.2"> |
| 37 | + <dependency id="System.Text.Json" version="9.0.4" /> |
| 38 | + </group> |
| 39 | + <group targetFramework="net9.0"> |
| 40 | + <dependency id="System.Text.Json" version="9.0.4" /> |
| 41 | + </group> |
| 42 | +</dependencies> |
| 43 | +``` |
| 44 | + |
| 45 | +## New behavior |
| 46 | + |
| 47 | +Starting in .NET 10 Preview 7, directly prunable PackageReference items are automatically marked with `PrivateAssets=all` and `IncludeAssets=none`, which excludes them from the generated dependencies for target frameworks where they're provided by the platform. |
| 48 | + |
| 49 | +The same project configuration now generates a *.nuspec* file with the prunable dependency removed from target frameworks that provide it: |
| 50 | + |
| 51 | +```xml |
| 52 | +<dependencies> |
| 53 | + <group targetFramework=".NETFramework4.7.2"> |
| 54 | + <dependency id="System.Text.Json" version="9.0.4" /> |
| 55 | + </group> |
| 56 | + <group targetFramework="net9.0"> |
| 57 | + </group> |
| 58 | +</dependencies> |
| 59 | +``` |
| 60 | + |
| 61 | +## Type of breaking change |
| 62 | + |
| 63 | +This is a [behavioral change](../../categories.md#behavioral-changes). |
| 64 | + |
| 65 | +## Reason for change |
| 66 | + |
| 67 | +This change ensures that package dependencies accurately reflect the actual requirements for each target framework. It prevents unnecessary package references from appearing in generated packages when those APIs are already provided by the target framework, reducing maintenance burden and avoiding false dependency issues. |
| 68 | + |
| 69 | +## Recommended action |
| 70 | + |
| 71 | +If you're creating packages and the generated *.nuspec* dependencies are missing a PackageReference for certain target frameworks: |
| 72 | + |
| 73 | +1. **If the package is no longer needed for any target framework**: Remove the PackageReference entirely from your project file. |
| 74 | + |
| 75 | +2. **If the package is still needed for some target frameworks**: Use conditional PackageReference to include it only where necessary. For example: |
| 76 | + |
| 77 | + ```xml |
| 78 | + <ItemGroup> |
| 79 | + <!-- Include System.Text.Json only for frameworks older than .NET 8 --> |
| 80 | + <PackageReference Include="System.Text.Json" Version="9.0.4" |
| 81 | + Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" /> |
| 82 | + </ItemGroup> |
| 83 | + ``` |
| 84 | + |
| 85 | +3. **If you need to override the automatic pruning behavior**: Set `RestoreEnablePackagePruning` to `false` in your project file or *Directory.Build.props* file: |
| 86 | + |
| 87 | + ```xml |
| 88 | + <PropertyGroup> |
| 89 | + <RestoreEnablePackagePruning>false</RestoreEnablePackagePruning> |
| 90 | + </PropertyGroup> |
| 91 | + ``` |
| 92 | + |
| 93 | +For more information about package pruning, see [PrunePackageReference](/nuget/consume-packages/package-references-in-project-files#prunepackagereference). |
| 94 | + |
| 95 | +## Affected APIs |
| 96 | + |
| 97 | +None. |
0 commit comments