Skip to content

PrunePackageReference improvements - restore applies PrivateAssets=all and IncludeAssets=none to all prunable direct references #6545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

nkolev92
Copy link
Member

@nkolev92 nkolev92 commented Jul 10, 2025

Bug

Fixes: NuGet/Home#14196

Description

Builds on top of #6540.

Design in NuGet/Home#14325.
This PR ensures that pruning privatizes direct packages .

This is the final of the pruning for directs work. Note that we have a P7 urgency and we're trying

PR Checklist

@nkolev92 nkolev92 force-pushed the dev-nkolev92-differentIdea branch from 50c6e59 to 962db6f Compare July 10, 2025 18:37
@nkolev92 nkolev92 changed the title PrunePackageReference improvements - Pack applies PrivateAssets=all to prunable direct references PrunePackageReference improvements - restore applies PrivateAssets=all and IncludeAssets=none to all prunable direct references PrunePackageReference improvements - restore applies PrivateAssets=all and IncludeAssets=none to all prunable direct references Jul 10, 2025
@nkolev92 nkolev92 force-pushed the dev-nkolev92-differentIdea branch from 962db6f to 93e1489 Compare July 11, 2025 04:35
@nkolev92 nkolev92 added the Breaking-change Label for .NET SDK breaking changes. label Jul 11, 2025
@nkolev92 nkolev92 marked this pull request as ready for review July 11, 2025 18:40
@nkolev92 nkolev92 requested a review from a team as a code owner July 11, 2025 18:40
@nkolev92 nkolev92 requested review from jebriede, jeffkl and zivkan July 11, 2025 18:40
Copy link
Contributor

dotnet-policy-service bot commented Jul 11, 2025

Added needs-breaking-change-doc-created label because this PR has the breaking-change label.

When you commit this breaking change:

  1. Create and link to this issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.
  2. Ask a committer to mail the .NET SDK Breaking Change Notification email list.

You can refer to the .NET SDK breaking change guidelines

@nkolev92
Copy link
Member Author

@NuGet/nuget-client
Can I please get a review?

martinrrm
martinrrm previously approved these changes Jul 14, 2025
Comment on lines +80 to +78
static bool IsDependencyPruned(LibraryDependency dependency, IReadOnlyDictionary<string, PrunePackageReference> packagesToPrune)
{
if (packagesToPrune?.TryGetValue(dependency.Name, out PrunePackageReference packageToPrune) == true
&& dependency.LibraryRange.VersionRange.Satisfies(packageToPrune.VersionRange.MaxVersion))
{
return true;
}
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reduce verbosity and make it more concise:

Suggested change
static bool IsDependencyPruned(LibraryDependency dependency, IReadOnlyDictionary<string, PrunePackageReference> packagesToPrune)
{
if (packagesToPrune?.TryGetValue(dependency.Name, out PrunePackageReference packageToPrune) == true
&& dependency.LibraryRange.VersionRange.Satisfies(packageToPrune.VersionRange.MaxVersion))
{
return true;
}
return false;
}
static bool IsDependencyPruned(LibraryDependency dependency, IReadOnlyDictionary<string, PrunePackageReference> packagesToPrune)
{
return packagesToPrune?.TryGetValue(dependency.Name, out var packageToPrune) == true
&& dependency.LibraryRange.VersionRange.Satisfies(packageToPrune.VersionRange.MaxVersion);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was similar question in an previous PR, that implementation slows down debugging, since now you need to set a conditional breakpoint vs just setting a breakpoint.
Functionally they're the same, so no perf benefit.

From a readability perspective, I've always seen them as equivalent as well, especially in this case.

If you feel strongly I can change it, especially both you and Nigusu brought it up (although I'm guessing @zivkan will lean towards the current version, as that's something we've discussed previously)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I personally dislike return statements that do anything other than return a const or a variable, because I often want to inspect the value when debugging. The compiled code is the same, so I just see it as unhelpful code golf 😆

I'm fine with bool result = ... followed by return result; if we want to remove the if statement. But I just don't like combining the two into a single statement.

Comment on lines +408 to +416
static bool IsDependencyPruned(LibraryDependency dependency, IReadOnlyDictionary<string, PrunePackageReference> packagesToPrune)
{
if (packagesToPrune?.TryGetValue(dependency.Name, out PrunePackageReference packageToPrune) == true
&& dependency.LibraryRange.VersionRange.Satisfies(packageToPrune.VersionRange.MaxVersion))
{
return true;
}
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this code is a duplicate of IncludeFlagUtils.IsDependencyPruned. This indicates to me that we could benefit from a data structure that handles the pruned dependency case and reduce the if conditionals. In lieu of that, we should deduplicate the code.

Copy link
Member Author

@nkolev92 nkolev92 Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is duplicated, but there's no type that would naturally own this, since TargetFrameworkInformation itself is a model and it has a dict of prune package refs.

The other consideration why I didn't add a type is that the feature is in development, so I didn't have enough scenarios to come up with an API.

Maybe I do now, but as of right now I've landed in the camp of I'd rather have 2 copies than a bad API.

@nkolev92 nkolev92 force-pushed the dev-nkolev92-differentIdea branch from 1e1ed65 to f3375d3 Compare July 15, 2025 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking-change Label for .NET SDK breaking changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pruning should prune and not warn for a direct reference in a multi-targeting project that does not prune on all TargetFrameworks
4 participants