Skip to content

CpsPackageReferenceProject uses PackageSpecFactory - #7379

Merged
zivkan merged 5 commits into
devfrom
dev-zivkan-PackageSpecFactory-CPS
May 14, 2026
Merged

zivkan merged 5 commits into
devfrom
dev-zivkan-PackageSpecFactory-CPS

Conversation

@zivkan

@zivkan zivkan commented May 8, 2026

Copy link
Copy Markdown
Member

Bug

working towards: NuGet/Home#14160

Description

CpsPackageReferenceProject now joins LegacyPackageReferenceProject and MSBuildStaticGraphRestore in using PackageSpecFactory. These only leaves "normal" msbuild restore not using PackageSpecFactory yet.

Since the point of PackageSpecFactory is to make all PackageReference restores works the same way, it would have been ideal if the only changes required were adding the environment variable check to opt-out of PackageSpecFactory, and making existing tests check both PackageSpecFactory enabled and disabled code paths. Unfortunately, PackageSpecFactory needed substantial changes, for the following reason:

  • With SDK style projects in VS (the CPS nomination workflow), NuGet gets notified when projects changed, but we don't get nominations from the project system when nuget.config settings change. So, the nomination code caches an intermediate PackageSpec, and then each time our VS PackageReferenceProject wants to get the PackageSpec, CpsPackageReferenceProject can clone the intermediate PackageSpec and then modify it with the current nuget.config settings.
    • The parts of the PackageSpec that are modified when applying nuget.config settings are package sources, fallback folders, and the global packages folder. For sources and fallback folders, it's possible to either provide locations in addition to the nuget.config values, or override anything in nuget.config and use only the values from the project properties.
  • It seems like LegacyPackageReferenceProject would benefit from the same thing, except legacy projects don't have a way to notify NuGet when properties or items change. Therefore, LegacyPackageReferenceProject has to regenerate the full PackageSpec every time, so there's no benefit to having the intermediate PackageSpec.
  • Similarly, command line restores are short lived processes and don't benefit from caching an intermediate PackageSpec.
  • So, moving the concept of an intermediate PackageSpec that gets settings applied is a leaky abstraction. However, I couldn't think of any other approach than to make it part of the common API. Using NullSettings doesn't work because sometimes nuget.config sources are used in addition to project property sources, but sometimes not.

Otherwise, this feature has very minimal impact on existing code. The changes to VsSolutionRestoreServiceTests.cs is a lot of lines, but it's only to enable all the tests to run on both the PackageSpecFactory and legacy code paths, to make sure they both work the same, with just one exception.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc. Customers will only notice if there are bugs, in which case we should fix the bugs. No expected change in behavior.

@zivkan
zivkan requested a review from a team as a code owner May 8, 2026 23:13
@zivkan
zivkan requested review from jeffkl and nkolev92 May 8, 2026 23:13
jeffkl
jeffkl previously approved these changes May 11, 2026

@jeffkl jeffkl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks right, but there are lots of changes. Are any of the test failures related?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates Visual Studio CPS PackageReference restore nomination to build PackageSpec instances via PackageSpecFactory, aligning it with other restore entry points and enabling consistent behavior behind an environment-variable switch.

Changes:

  • Add PackageSpecFactory.GetIntermediatePackageSpec(...) + PackageSpecFactory.ApplySettings(...) to support caching an “intermediate” PackageSpec and applying current nuget.config settings later.
  • Update VsSolutionRestoreService to optionally generate nominations via PackageSpecFactory (opt-out via NUGET_USE_NEW_PACKAGESPEC_FACTORY=false) and add a VS-nomination-to-IProject adapter layer.
  • Expand VsSolutionRestoreServiceTests to run most scenarios in both code paths (PackageSpecFactory enabled/disabled) and adjust the test nomination builder to better mimic MSBuild behavior.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/VsSolutionRestoreServiceTests.cs Parameterizes tests to validate both PackageSpecFactory and legacy nomination paths; adds env-var test wiring.
test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/TestProjectRestoreInfoBuilder.cs Adds common MSBuild-like properties and synthesizes ProjectReference FullPath metadata.
src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/PackageSpecFactory.cs Introduces intermediate PackageSpec creation + settings application helpers and adjusts source/fallback-folder handling accordingly.
src/NuGet.Core/NuGet.Commands/PublicAPI/net8.0/PublicAPI.Unshipped.txt Declares new public API surface for PackageSpecFactory.ApplySettings and GetIntermediatePackageSpec.
src/NuGet.Core/NuGet.Commands/PublicAPI/net472/PublicAPI.Unshipped.txt Declares new public API surface for PackageSpecFactory.ApplySettings and GetIntermediatePackageSpec.
src/NuGet.Core/NuGet.Build.Tasks.Console/TargetFrameworkAdapter.cs Enables nullable and updates ITargetFramework.GetProperty signature to nullable.
src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs Adds env-var controlled switch and routes nomination spec creation through PackageSpecFactory via adapters.
src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs Widens helper visibility needed by adapters (internal).
src/NuGet.Clients/NuGet.SolutionRestoreManager/PackageSpecAdapter/OuterBuildAdapter.cs New adapter to represent “outer build” behavior for CPS nominations.
src/NuGet.Clients/NuGet.SolutionRestoreManager/PackageSpecAdapter/NominationProjectAdapter.cs New adapter implementing IProject over VS nomination objects.
src/NuGet.Clients/NuGet.SolutionRestoreManager/PackageSpecAdapter/ItemAdapter.cs New adapter implementing IItem over VS reference items.
src/NuGet.Clients/NuGet.SolutionRestoreManager/PackageSpecAdapter/InnerBuildAdapter.cs New adapter implementing ITargetFramework over per-TFM nomination data.
src/NuGet.Clients/NuGet.SolutionRestoreManager/NuGet.SolutionRestoreManager.csproj Includes HashCodeCombiner shared source for adapter hashing logic.
src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/CpsPackageReferenceProject.cs Switches settings application from VSRestoreSettingsUtilities to PackageSpecFactory.ApplySettings.
src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/GlobalSuppressions.cs Removes suppression for a deleted helper method.

@zivkan

zivkan commented May 12, 2026

Copy link
Copy Markdown
Member Author

@zivkan
zivkan marked this pull request as draft May 12, 2026 01:18
@zivkan
zivkan force-pushed the dev-zivkan-PackageSpecFactory-CPS branch from 470fc90 to 55e1a61 Compare May 13, 2026 06:19
@zivkan
zivkan marked this pull request as ready for review May 13, 2026 10:59
@zivkan
zivkan requested review from jeffkl and nkolev92 May 14, 2026 07:43
@zivkan
zivkan merged commit 4286cb0 into dev May 14, 2026
17 of 18 checks passed
@zivkan
zivkan deleted the dev-zivkan-PackageSpecFactory-CPS branch May 14, 2026 22:34
Copilot AI added a commit that referenced this pull request May 15, 2026
nkolev92 added a commit that referenced this pull request May 16, 2026
…7397)

This reverts commit 4286cb0.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: nkolev92 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants