agentleFS
Jeff-TianJeff-Tian/agent-skills/dotnet-cpm/SKILL.md

dotnet-cpm

Skill1 starsChanged 7 months agoMITView on GitHub

Adopt and maintain Central Package Management (CPM) for .NET / MSBuild solutions: enable `ManagePackageVersionsCentrally`, consolidate all NuGet versions in `Directory.Packages.props`, strip `Version=` from every `<PackageReference>`, and document the convention so future contributors (and AI agents) do not regress. Use when the user mentions "Central Package Management", "CPM", "Directory.Packages.props", "centralize NuGet versions", "ManagePackageVersionsCentrally", "NU1008", "version drift across csproj", "consolidate package versions", "PackageVersion vs PackageReference", "集中包管理", "统一 NuGet 版本", or sees the same NuGet package pinned to different versions across multiple `.csproj` files in one repo.

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 1 is distinctive, and 8 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 8

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Stop pinning NuGet versions in every .csproj.

    line 20
  2. Very specific:

    Every <PackageReference> in every csproj has no Version attribute.

    line 46
  3. Distinctive:

    Place at repo root (or merge into existing one):

    line 72
  4. Very specific:

    If packages.lock.json files exist, they will be regenerated — commit them.

    line 124
  5. Very specific:

    Add a "Centralized Package Management (mandatory)" section to whatever file agents/contributors are guaranteed to read …

    line 135
  6. Very specific:

    Never: put Version="..." on a <PackageReference>.

    line 147
  7. Very specific:

    Adding a package: add <PackageVersion Include="X" Version="Y" /> to Directory.Packages.props first, then reference with bare …

    line 149
  8. Very specific:

    Never: add a nested Directory.Build.props that disables ManagePackageVersionsCentrally.

    line 156
  9. Very specific:

    After any package change run dotnet restore and commit refreshed packages.lock.json files.

    line 158

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

.NET Central Package Management (CPM)

Overview

Stop pinning NuGet versions in every .csproj. Move every version to a single Directory.Packages.props at the repo root and let MSBuild apply it to every project in the tree.

Core principle: One version per package, repo-wide. Csproj files declare what they depend on; Directory.Packages.props declares which version.

When to Use

  • A multi-project .NET solution has the same NuGet package pinned to different versions across src/** and tests/**.
  • Dependabot keeps opening N PRs to bump the same package across N csproj files.
  • A CVE / security advisory forces a coordinated bump of a transitive (e.g. System.Text.Json) and you don't want to edit every csproj.
  • The user explicitly asks for CPM / Directory.Packages.props.

Skip / adapt:

  • Single-project repos (CPM works but the payoff is small).
  • Repos that intentionally pin different versions per project (justify every override with VersionOverride=).

Outcome

  • Directory.Build.props at repo root enables CPM.
  • Directory.Packages.props at repo root holds every <PackageVersion>.
  • Every <PackageReference> in every csproj has no Version attribute.
  • dotnet restore succeeds; packages.lock.json files refreshed and committed.
  • A short rules section in the repo's agent/contributor docs prevents future regressions.

Procedure

Execute in order.

1. Inventory

Find every csproj and every <PackageReference> version currently pinned:

fd -e csproj | xargs grep -hE 'PackageReference.*Version=' | \
  sed -E 's/.*Include="([^"]+)".*Version="([^"]+)".*/\1 \2/' | \
  sort -u

Spot conflicts: the same package on two different versions. Decide on one version per package (usually the highest that all projects tolerate). Note these decisions before editing anything.

2. Create Directory.Build.props

Place at repo root (or merge into existing one):

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
</Project>

MSBuild auto-imports this for every project in the directory tree.

3. Create Directory.Packages.props

<Project>
  <ItemGroup>
    <PackageVersion Include="Serilog" Version="3.1.1" />
    <PackageVersion Include="xunit" Version="2.9.0" />
    <!-- one entry per distinct package, alphabetized -->
  </ItemGroup>
</Project>

Include every package referenced anywhere in the solution. Missing entries fail restore with NU1010.

4. Strip Version= from every <PackageReference>

For each csproj, change:

<PackageReference Include="Serilog" Version="3.1.1" />

to:

<PackageReference Include="Serilog" />

Preserve other child elements/attributes like IncludeAssets, PrivateAssets, GeneratePathProperty. Leaving Version= in place triggers NU1008.

5. Restore and verify

dotnet restore
dotnet build

If packages.lock.json files exist, they will be regenerated — commit them. Common failures:

CodeMeaningFix
NU1008Version= still on a <PackageReference>Strip the attribute.
NU1010Project references a package with no <PackageVersion>Add the entry to Directory.Packages.props.
NU1605Downgrade detectedBump <PackageVersion> to satisfy the highest transitive requirement.

6. Document the convention

Add a "Centralized Package Management (mandatory)" section to whatever file agents/contributors are guaranteed to read (.github/copilot-instructions.md, CONTRIBUTING.md, or the README). State the hard rules — see references/agent-rules-snippet.md.

Optionally add a lessons-learned note explaining why CPM was adopted (version drift, Dependabot noise, CVE coordination).

Hard Rules (post-migration)

For any future csproj edit:

  1. Never put Version="..." on a <PackageReference>. NU1008 will fail the build.
  2. Adding a package: add <PackageVersion Include="X" Version="Y" /> to Directory.Packages.props first, then reference with bare <PackageReference Include="X" /> in the csproj.
  3. Bumping a package: edit Directory.Packages.props only. All projects move together.
  4. Per-project override (rare, comment why): <PackageReference Include="X" VersionOverride="Y" />.
  5. Never add a nested Directory.Build.props that disables ManagePackageVersionsCentrally.
  6. After any package change run dotnet restore and commit refreshed packages.lock.json files.

Gotchas

  • Test SDKs drift first. xunit, Moq, Microsoft.NET.Test.Sdk, coverlet.collector are the most common offenders — check tests/** projects carefully during inventory.
  • Dockerfiles must COPY Directory.Build.props and Directory.Packages.props into the build context before any dotnet restore. If the build context only includes src/ and global.json, MSBuild inside the container silently runs without CPM, every <PackageReference> becomes version-less, and NuGet restores the lowest available version of each package (e.g. MimeKit 0.5.0, Npgsql 2.0.11, OneOf 1.0.5). The build then fails with a flood of NU1604 warnings ("Project dependency X does not contain an inclusive lower bound") and NU1701 framework warnings. Either COPY . . or list the two props files explicitly.
  • Global usings via <PackageReference> (e.g. Microsoft.AspNetCore.App framework reference) don't take a version — leave them alone.
  • Analyzers / source generators still work under CPM; keep PrivateAssets="all" on those <PackageReference> elements.
  • Private/mirror NuGet feeds (e.g. Azure China nuget.cdn.azure.cn) can flake during the big initial restore. Fall back with dotnet restore --source https://api.nuget.org/v3/index.json.
  • packages.lock.json churn is large on the migration commit — expected; review project-by-project, not as one blob.
  • VersionOverride is not transitive. It only affects the project it's declared in. Use sparingly and document why.
  • GlobalPackageReference in Directory.Packages.props applies to all projects — useful for analyzers like Microsoft.SourceLink.GitHub. Only reach for it when you genuinely want every project to take the dep.

Anti-Patterns

  • ❌ Leaving one or two csproj files with Version= "just for now" — defeats the whole point and confuses future contributors.
  • ❌ Per-project Directory.Build.props that re-disables CPM.
  • ❌ Bumping <PackageVersion> and adding VersionOverride= in the same PR — pick one mechanism.
  • ❌ Migrating without committing refreshed packages.lock.json — the next CI run will produce a noisy diff.

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.