Skip to content

Fix dotnet add package --no-restore ignoring Central Package Management - #7148

Merged
jeffkl merged 3 commits into
NuGet:devfrom
slang25:fix/cpm-no-restore
Mar 6, 2026
Merged

jeffkl merged 3 commits into
NuGet:devfrom
slang25:fix/cpm-no-restore

Conversation

@slang25

@slang25 slang25 commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Bug

Fixes: NuGet/Home#12552

Description

When --no-restore is passed to dotnet add package in a CPM-enabled project, the command writes a versioned <PackageReference Version="..."> directly to the .csproj, ignoring Central Package Management. This happens because CPM detection relies on the DependencyGraphSpec built during restore, which --no-restore skips. The result is NU1008 on the next restore.

This fix adds a new IsCentralPackageManagementEnabled() method to MSBuildAPIUtility that checks the MSBuild-evaluated ManagePackageVersionsCentrally property (available via GetProject() without restore), and sets VersionCentrallyManaged on the LibraryDependency in the --no-restore path. The existing downstream CPM handling then correctly produces a versionless <PackageReference> in the .csproj and a <PackageVersion> in Directory.Packages.props.

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.

…ment

When --no-restore is passed, CPM detection is skipped because it relies
on the DependencyGraphSpec built during restore. This causes a versioned
PackageReference to be written directly to the .csproj, resulting in
NU1008 on the next restore. Fix by checking the MSBuild-evaluated
ManagePackageVersionsCentrally property, which is available via
GetProject() without restore.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@slang25
slang25 requested a review from a team as a code owner February 17, 2026 22:38
@slang25
slang25 requested review from nkolev92 and zivkan February 17, 2026 22:38
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Feb 17, 2026
slang25 and others added 2 commits February 22, 2026 19:48
AddPackageReference and AddPackageReferencePerTFM now determine CPM
status from the project they already load, eliminating the separate
IsCentralPackageManagementEnabled call that loaded the project a second
time. This simplifies the API so callers don't need to pre-set
VersionCentrallyManaged on LibraryDependency.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
VersionCentrallyManaged is init-only, so it cannot be assigned after
construction. Move the CPM check into the private AddPackageReference
method which reads it directly from the already-loaded Project object.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Mar 2, 2026
@Frulfump

Frulfump commented Mar 2, 2026

Copy link
Copy Markdown

Not stale, waiting for reviews

@dotnet-policy-service dotnet-policy-service Bot removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Mar 2, 2026
@nkolev92

nkolev92 commented Mar 2, 2026

Copy link
Copy Markdown
Member

We normally wait for a re-request for review to know it's ready for another look.

We'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CPM] dotnet add package with --no-restore causes NU1008

5 participants