Skip to content

Central package management

This page sets the rules for where and how ConnectSoft repositories declare NuGet package versions. It is written for engineers who add or upgrade packages, for template maintainers, and for reviewers of Directory.Packages.props changes. Following it gives every repository one place to read, review and upgrade its versions.

How central package management works, with a full walkthrough, is explained in Central Package Management. The upgrade policy and the package holds are decided in ADR-0103; licence holds are explained in Dependency licensing.

Rules

CS-DEP-007: Declare every package version in a root Directory.Packages.props

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual (target: ENF-063) · Status: active

Why

ADR-0103 puts versions in Directory.Packages.props. One file per repository is the only place a reviewer, an upgrade tool or a vulnerability audit has to read. Versions spread over project files drift apart.

Do

  • Keep a Directory.Packages.props file in the repository root, next to the solution file.
  • Add a PackageVersion line for every package that a project references.
  • Group the lines with ItemGroup labels by purpose, such as testing, logging or messaging.

Don't

  • Keep a second Directory.Packages.props deeper in the tree, except for a sample or package-consumer project that deliberately turns central package management off (see CS-DEP-008).

How to check

The repository root contains Directory.Packages.props, and the change adds no version outside it.

CS-DEP-008: Turn on central package management once, in Directory.Packages.props

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual (target: ENF-063) · Status: active

Why

Central package management is on only when the MSBuild property ManagePackageVersionsCentrally is true; the file alone does not turn it on. Setting the property in every project file repeats it hundreds of times, and a new project that misses it silently uses its own versions.

Do

  • In new repositories and in templates, set <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> once, in a PropertyGroup at the top of Directory.Packages.props.
  • In a sample or package-consumer project that must restore published packages as a customer would, set the property to false in that project, with a comment that gives the reason.

Don't

  • Add the property to each new project file of a new repository.

Existing repositories that set the property in every project file may keep doing so; the count is tracked in ENF-063.

Example

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup Label="Testing">
    <PackageVersion Include="MSTest" Version="4.5.3" />
  </ItemGroup>
</Project>

How to check

A new repository or template sets ManagePackageVersionsCentrally in Directory.Packages.props and not in its project files.

CS-DEP-009: Put no version on a PackageReference

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

With central package management on, a Version attribute on a PackageReference fails the restore with NU1008. Where the build does not fail, for example in a project that turned central management off, the version is invisible to upgrades and audits of Directory.Packages.props.

Do

  • Write <PackageReference Include="Serilog" /> and put the version in Directory.Packages.props.

Don't

  • Add a Version attribute to a PackageReference, in a project file or in Directory.Build.props.

How to check

Search the change for <PackageReference with a Version= attribute.

CS-DEP-010: Use VersionOverride only with a comment that gives the reason

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

VersionOverride lets one project use a different version from the central one. Without a reason next to it, nobody knows when it can be removed, and an upgrade of the central version leaves that project behind.

Do

  • Use VersionOverride only when one project genuinely needs another version, for example an analyzer test project whose test host fails at runtime with the central version.
  • Put an XML comment on the line before it that says why, and when it can be removed.

Don't

  • Use VersionOverride to avoid upgrading one project.

Example

<!-- Align with Microsoft.CodeAnalysis.Analyzer.Testing 1.1.x; NuGet 7.x causes MethodAccessException at runtime. -->
<PackageReference Include="NuGet.Packaging" VersionOverride="8.0.0" />

How to check

Every VersionOverride in the change has a comment on the line before it.

CS-DEP-011: Pin exact versions

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

A floating version such as 4.*, or a range such as [1.0.0,2.0.0), resolves to whatever the feed holds on the day of the restore. Two builds of the same commit can then use different code, and a broken release reaches every repository at once.

Do

  • Give every PackageVersion an exact version, such as 4.4.0.

Don't

  • Use * in a version.
  • Use a version range.

How to check

Search Directory.Packages.props for Version=" values that contain *, [ or (.

CS-DEP-012: Never move a stable pin to a prerelease

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

ADR-0103 sets the rule: previews follow only their own label. A prerelease can change its API or its behaviour in the next build, and an upgrade tool that sorts versions can propose one by mistake.

Do

  • Keep a stable pin on stable versions.
  • Move a prerelease pin only to a newer version with the same label, for example 1.21.0-beta.1 to 1.22.0-beta.1.
  • When a package exists only as a prerelease, add a comment that says why it is needed.

Don't

  • Replace a stable version with a -preview, -beta, -rc or -alpha version.
  • Move a prerelease pin to a different label without an owner decision.

How to check

  • No PackageVersion line in the change goes from a stable version to a version with a prerelease label.
  • A prerelease PackageVersion keeps its label, for example -beta stays -beta, unless the change cites an owner decision.

CS-DEP-013: Comment every held version with the ADR that holds it

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

A held version looks like an outdated one. Without a comment, the next person who upgrades packages moves it, and the reason for the hold is lost.

Do

  • Put an XML comment on the line before each held PackageVersion that names the hold and cites the ADR, for example ADR-0103.
  • For a group of packages held together, such as Swashbuckle.AspNetCore*, put the comment on the group.

Don't

  • Hold a version without saying why.

Example

<!-- Held at 10.3.1: Swashbuckle 11 needs Microsoft.OpenApi 3.x, which breaks gRPC Swagger (ADR-0103). -->
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.3.1" />
<!-- Held at 9.2.6: MassTransit 9 needs a commercial licence (ADR-0103). -->
<PackageVersion Include="MassTransit" Version="9.2.6" />

How to check

Every package listed as a hold in ADR-0103 has a comment that cites the ADR.

CS-DEP-014: Keep package source mapping and NuGet audit on

Severity: major · Applies to: service, library, api-client, blazor, template, pipelines · Enforced by: manual · Status: active

Why

Package source mapping decides which feed may supply which package, so a public package with the name of an internal one cannot be restored in its place. NuGet audit reports known vulnerabilities in direct and transitive packages at restore time.

Do

  • In nuget.config, map ConnectSoft.* to the ConnectSoft feed and * to nuget.org.
  • In Directory.Build.props, set NuGetAudit to true and NuGetAuditMode to all.

Don't

  • Remove the packageSourceMapping section, or add a feed without mapping it.
  • Turn NuGetAudit off, or suppress an advisory without a written reason.

Example

<!-- nuget.config -->
<packageSourceMapping>
  <packageSource key="nuget.org">
    <package pattern="*" />
  </packageSource>
  <packageSource key="ConnectSoft">
    <package pattern="ConnectSoft.*" />
  </packageSource>
</packageSourceMapping>
<!-- Directory.Build.props -->
<PropertyGroup>
  <NuGetAudit>true</NuGetAudit>
  <NuGetAuditMode>all</NuGetAuditMode>
</PropertyGroup>

How to check

  • nuget.config has a packageSourceMapping that covers every feed.
  • Directory.Build.props sets NuGetAudit to true and NuGetAuditMode to all.
  • Every NuGetAuditSuppress item has a comment that gives the reason.

CS-DEP-015: Keep per-framework version lines inside their major

Severity: minor · Applies to: library, template · Enforced by: manual · Status: active

Why

A multi-targeted repository can pin one package twice, with a Condition on the target framework, because the newer major supports only the newer framework. ADR-0103 records that the upgrade planner moves each such line only inside its own major. A line moved across majors makes the older framework resolve a package it does not support.

Do

  • Upgrade each per-framework line inside its own major, for example the net10.0 line from 10.0.14 to 10.0.15.
  • Keep runtime-family packages on one plain line, as ADR-0102 sets.

Don't

  • Move the net10.0 line of a package to the major of the newer framework.

Example

<PackageVersion Condition="'$(TargetFramework)' == 'net10.0'" Include="Microsoft.AspNetCore.HeaderPropagation" Version="10.0.15" />
<PackageVersion Condition="'$(TargetFramework)' != 'net10.0'" Include="Microsoft.AspNetCore.HeaderPropagation" Version="11.0.0" />

How to check

In a change to a conditioned PackageVersion line, the major version stays the same.

CS-DEP-016: Build and test every repository of a dependency wave in Release before review

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

ConnectSoft builds treat warnings as errors in Release, and CI builds Release. A package or analyzer bump checked only with a Debug build passes locally and fails in CI on code the change did not touch. Tests that pin version strings of the moved packages fail only when they run.

Do

  • Build each repository of the wave in Release and run its full test suites before review.
  • Search the tests for literal version strings of the packages that moved, and update them in the same change.

Don't

  • Send a dependency wave to review on a Debug build.

How to check

The pull request shows a Release build and test run for each repository of the wave.

CS-DEP-017: Remove a package from every file that names it

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

A package is named in more places than Directory.Packages.props: project references, logging enrichers and filters, settings sections and registration code. A removal that misses one of them fails at restore with NU1010, or at host start in a settings variant that the unit tests never load.

Do

  • Search the repository case-insensitively for the package name and its namespaces, settings files included.
  • Remove every reference, settings entry and registration in the same change.
  • Build the whole solution and start each host with each settings variant afterwards.

Don't

  • Remove a package only from the central versions file.

How to check

A case-insensitive search for the package name and its root namespace finds nothing after the change.

CS-DEP-018: Ship static web assets only through the SDK pipeline

Severity: major · Applies to: library, blazor, template · Enforced by: manual · Status: active

Why

The Razor SDK packs a Razor class library's wwwroot as static web assets. A custom pack target that also adds wwwroot as content files ships every asset twice, and every consumer fails to build with a duplicate asset identity.

Do

  • Let the SDK pack wwwroot.
  • Keep a package-content check that asserts the static web assets are present and that no contentFiles/**/wwwroot entries exist.

Don't

  • Add wwwroot/** as Content or None with Pack="true" in a Razor class library.

How to check

The packed .nupkg has static web assets and no contentFiles entries under wwwroot.

CS-DEP-019: Use MSBuild Exists() only in a condition

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

Exists() is a condition function. Inside a property value it evaluates to an empty string, so a property meant to record whether a folder exists is always empty, and every import or reference guarded by it is skipped.

Do

  • Set a detection property with a condition: <HasFolder Condition="Exists('…')">true</HasFolder>.
  • Build in each layout the property distinguishes, including the container build.

Don't

  • Write $(Exists('…')) inside a property value.

How to check

Search MSBuild files for $(Exists(.

CS-DEP-020: Keep analyzer and build-only packages out of the package's dependencies

Severity: major · Applies to: library, blazor, template · Enforced by: manual · Status: active

Why

A PackageReference without PrivateAssets="all" becomes a dependency of the NuGet package the project produces. An analyzer or build-only package then flows into every consumer: their builds gain new warnings, and a version conflict blocks their restore.

Do

  • Reference analyzers (StyleCop, Meziantou, Sonar, Roslynator, Microsoft.CodeAnalysis.*Analyzers) and build-only packages (Source Link, versioning tools) with PrivateAssets="all", or as GlobalPackageReference in Directory.Packages.props, which is private by default.

Don't

  • Add an analyzer or build-only package as a plain PackageReference in a packable project.

Example

<PackageReference Include="Meziantou.Analyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

How to check

  • Every analyzer or build-only PackageReference in a packable project sets PrivateAssets to all, or the package is a GlobalPackageReference.
  • The packed .nuspec lists no analyzer or build-only package as a dependency.