skip to content

In NuGet's Central Package Management, when would you use VersionOverride, CentralPackageTransitivePinningEnabled or GlobalPackageReference, and what does each one cost you?

level: seniorimportance: should knowfreq 28%

answer

  1. one project's exception to the central version
  2. CentralPackageVersionOverrideEnabled false forbids it
  3. pinning promotes transitive packages to top level
  4. NU1109: pinning cannot downgrade
  5. global references: no compile assets, PrivateAssets All

basics

~20 s

VersionOverride lets one PackageReference ignore the central version. CentralPackageTransitivePinningEnabled makes PackageVersion entries also raise transitive packages, at the price of NU1109 on downgrades and extra pack dependencies. GlobalPackageReference adds a build-only package to every project.

solid answer

~40 s

`VersionOverride="x"` on a `PackageReference` beats the central `PackageVersion` for that project alone — the escape hatch for one legacy project, which reintroduces drift and can be banned with `CentralPackageVersionOverrideEnabled=false`. By default a `PackageVersion` only versions **direct** references; with `CentralPackageTransitivePinningEnabled=true` a central entry also pins a package that arrives transitively, promoting it to top level. It cannot pin **down** (restore fails with **NU1109**), and when you pack, pinned packages become explicit dependencies in the `.nuspec`. `GlobalPackageReference` in `Directory.Packages.props` adds a package to every project with `IncludeAssets` limited to runtime, build, native, contentFiles and analyzers and `PrivateAssets="All"` — right for a versioning tool or analyzer, wrong for a library you compile against.

code

xml · 13 lines
xml
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
    <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
    <CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
    <!-- referenced by no project directly: pinned only because pinning is on -->
    <PackageVersion Include="Npgsql" Version="10.0.3" />
    <GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.5.109" />
  </ItemGroup>
</Project>

go deeper

for a junior

Recall that CPM has a per-project escape hatch called VersionOverride and a way to add a package to all projects called GlobalPackageReference.

for a middle

Explain that central versions apply to direct references only until transitive pinning is enabled, and what GlobalPackageReference does to included and private assets.

for a senior

Choose between override, pinning and nested props files for real cases, and anticipate NU1109 and the extra nuspec dependencies pinning creates when a library is packed.

for a principal

Set the exception policy: whether overrides are allowed at all, how pins are reviewed and retired, and how libraries and applications in one repository are treated differently.

## Three tools, three different problems Once a repository runs on Central Package Management (CPM), the default behaviour is strict: each project's `PackageReference` takes the version from the matching `PackageVersion` in `Directory.Packages.props`, and nothing else. Real repositories need three kinds of exception, and CPM has one switch for each. | Need | Mechanism | Scope | |---|---|---| | one project must differ | `VersionOverride` on its `PackageReference` | that project | | a transitive package must be raised | `CentralPackageTransitivePinningEnabled` | every project that opts in | | every project needs the same build-time package | `GlobalPackageReference` | every project under the props file | ## VersionOverride — the per-project exception ```xml <PackageReference Include="Npgsql" VersionOverride="9.0.0" /> ``` The override takes precedence over the central `PackageVersion` for that one reference. It is the honest way to keep a legacy reporting project on an older major while the rest of the suite moves on. The costs: - It reintroduces the drift CPM was adopted to remove, and nothing reminds anyone to delete it later. - `dotnet package add` respects it: if a reference already has a `VersionOverride`, the command updates the override rather than the central entry. - A team that wants no exceptions sets `CentralPackageVersionOverrideEnabled` to `false`; any `VersionOverride` then fails restore. ## Transitive pinning — raising what you do not reference Without pinning, a `PackageVersion` for a package that no project references directly is simply unused; the transitive version still comes from the lowest-applicable rule. Setting `CentralPackageTransitivePinningEnabled` to `true` (it defaults to off) changes that: a central entry now applies to the package wherever it appears in the graph, by promoting it to a top-level dependency on your behalf. That is how a suite lifts a transitive package to a patched release without adding a fake direct reference to 25 projects. The costs are specific: 1. **No downgrades.** If the pinned version is lower than a dependency requires, restore fails with **NU1109 Detected package downgrade**, showing both paths. Pinning only raises. 2. **Pack changes.** When a project with pinned packages is packed, NuGet writes the pinned packages as explicit dependencies in the `.nuspec`, so a library can ship dependencies its code never mentions. Evaluate it carefully in library projects. 3. **Floating overrides defeat it.** NuGet's NU1011 guidance warns that floating versions with pinning let different projects end up on different versions of the supposedly pinned package. ## GlobalPackageReference — one line for every project ```xml <GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.5.109" /> ``` `NuGet.targets` turns each `GlobalPackageReference` into a `PackageReference` with `IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"` and `PrivateAssets="All"`, plus a matching `PackageVersion`. **Compile** assets are left out, so a global reference to a runtime library would not give your code its types; and `PrivateAssets="All"` keeps it from flowing to consumers or into packed dependencies. It suits versioning tools, analyzers and build extensions. It is active only under CPM and can be switched off with `RestoreEnableGlobalPackageReference=false`. ## A nested props file — the fourth option When a whole area of the repository needs different versions — the tax tools that must stay on an older major for a regulator's certification, say — neither a per-project override nor a global pin fits. A `Directory.Packages.props` inside that folder becomes the nearest file for its projects; it imports the root file with `GetPathOfFileAbove` and changes only the entries it must with `<PackageVersion Update="..." />`. The exception is then visible in one file, scoped to one subtree, and reviewed like any other change. These mechanisms combine: a repository can run pinning everywhere, forbid `VersionOverride`, and allow differences only through nested props files, so every exception has an owner and a location. ## Choosing in an interview - A single project stuck on an old major: `VersionOverride`, with a tracked reason. - A vulnerable transitive package with a fixed patch: transitive pinning, ideally in applications rather than published libraries. - An analyzer or versioning tool everywhere: `GlobalPackageReference`. - A whole subtree that needs different versions: a nested `Directory.Packages.props` that imports the root and uses `PackageVersion Update`.

  • What does NU1109 tell you and how do you clear it?
    That a centrally pinned version is lower than one a dependency requires, so pinning would downgrade it. NuGet refuses rather than honouring the pin. Raise the `PackageVersion` to at least the dependency's floor, or remove the entry if the pin no longer serves a purpose.
  • Why be careful with transitive pinning in a library you publish?
    When the project is packed, every transitively pinned package is promoted to an explicit dependency in the nuspec. Consumers then inherit a floor for packages your code never references, which can force upgrades or conflicts on them.
  • Could you put Serilog in GlobalPackageReference so no project has to reference it?
    Not usefully: global references exclude compile assets, so projects would not see Serilog's types, and `PrivateAssets=All` stops it flowing to consumers. Libraries you code against belong in ordinary `PackageReference` items with central versions.

  • A PackageVersion pins a package even when only a dependency pulls it in.
  • Transitive pinning can force a dependency down to an older version.
  • VersionOverride changes the version for every project in the repository.
  • GlobalPackageReference is a shortcut for adding a runtime library to every project.
  • Transitive pinning has no effect on the package you pack from the project.