Skip to content

Upgrade literal GlobalPackageReference versions without dotnet add - #851

Merged
slang25 merged 1 commit into
masterfrom
fix-681-global-package-reference
Nov 12, 2026
Merged

slang25 merged 1 commit into
masterfrom
fix-681-global-package-reference

Conversation

@slang25

@slang25 slang25 commented Nov 12, 2026

Copy link
Copy Markdown
Member

Fixes #681.

Problem

dotnet outdated --upgrade fails for a GlobalPackageReference with a literal version:

error: PackageVersion for package 'StyleCop.Analyzers' defined in incorrect location, PackageVersion should be defined in Directory.Package.props.

Since #697 these upgrades went through dotnet add package (unless --no-restore was set). That command can't update a GlobalPackageReference: it adds a PackageReference to the project, which central package management rejects. A GlobalPackageReference whose version came from an MSBuild property was already written directly, so only the literal case was broken.

Fix

  • IProjectEvaluator.GetGlobalPackageReferenceFile reports which file's GlobalPackageReference sets a package's version. MSBuildEvaluator already tracked these items. As in GetVersionVariables, a PackageReference version takes precedence.
  • VersionLocator returns a new GlobalPackageReferenceVersion location for this case. DotNetPackageService writes the version in that file (formatting and encoding unchanged) and then restores.
  • The restore-once logic now lives in a shared RestoreOnce helper. A multi-targeted project, or several projects sharing the reference, restores once per project rather than once per target framework.
  • The version is written in the file that declares it, so a GlobalPackageReference in Directory.Build.props is upgraded too. Before, only Directory.Packages.props was searched, even with --no-restore.

Tests

  • New tests cover upgrading without --no-restore (attribute layouts, child elements, conditional ItemGroup), restoring once across target frameworks, and a GlobalPackageReference in Directory.Build.props with and without --no-restore. All 6 fail without the fix.
  • Removed the GlobalPackageReference case from CpmProjectWithRestore_PreservesPackageAttributeFormatting. It mocked dotnet add package successfully updating a GlobalPackageReference, which real dotnet doesn't do, and is how this bug got past the tests.
  • Full suite passes on net8.0, net9.0 and net11.0.
  • End-to-end: a multi-targeted CPM repro with StyleCop.Analyzers 1.2.0-beta.556 failed on master. With this branch it upgrades to 1.3.180, and a second run reports no outdated dependencies.

🤖 Generated with Claude Code

`dotnet add package` can't update a GlobalPackageReference: it adds a
PackageReference to the project, which central package management
rejects ("PackageVersion ... defined in incorrect location"). Since
#697 a GlobalPackageReference with a literal version went through it
unless --no-restore was set, so `--upgrade` failed (#681).

The version locator now asks the evaluator whether a package's version
comes from a GlobalPackageReference and in which file, and the upgrade
writes that file directly and restores the project once (shared with
the variable path's restore-once logic). This also handles a
GlobalPackageReference declared outside Directory.Packages.props.

Fixes #681

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@slang25
slang25 merged commit 4d722c8 into master Nov 12, 2026
12 checks passed
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.

Running dotnet outdated detects available update for a global package but running --upgrade doesn't update it

1 participant