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.
In this repository11 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .github/copilot-instructions.mdCopilot instructions
- app-store-version-checkSkill
- cohosted-frontend-backendSkill
- gitmojiSkill
- markdown-outputSkill
- oidc-integrationSkill
- repo-memorySkill
- skoruba-adminui-on-duendeSkill
- specflow-to-reqnrollSkill
- tddSkill
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
- Very specific: line 20
Stop pinning NuGet versions in every .csproj.
- Very specific: line 46
Every <PackageReference> in every csproj has no Version attribute.
- Distinctive: line 72
Place at repo root (or merge into existing one):
- Very specific: line 124
If packages.lock.json files exist, they will be regenerated — commit them.
- Very specific: line 135
Add a "Centralized Package Management (mandatory)" section to whatever file agents/contributors are guaranteed to read …
- Very specific: line 147
Never: put Version="..." on a <PackageReference>.
- Very specific: line 149
Adding a package: add <PackageVersion Include="X" Version="Y" /> to Directory.Packages.props first, then reference with bare …
- Very specific: line 156
Never: add a nested Directory.Build.props that disables ManagePackageVersionsCentrally.
- Very specific: line 158
After any package change run dotnet restore and commit refreshed packages.lock.json files.
≈ “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.
Files
.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/**andtests/**. - 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.propsat repo root enables CPM.Directory.Packages.propsat repo root holds every<PackageVersion>.- Every
<PackageReference>in every csproj has noVersionattribute. dotnet restoresucceeds;packages.lock.jsonfiles 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 -uSpot 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 buildIf packages.lock.json files exist, they will be regenerated — commit
them. Common failures:
| Code | Meaning | Fix |
| NU1008 | Version= still on a <PackageReference> | Strip the attribute. |
| NU1010 | Project references a package with no <PackageVersion> | Add the entry to Directory.Packages.props. |
| NU1605 | Downgrade detected | Bump <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:
- Never put
Version="..."on a<PackageReference>. NU1008 will fail the build. - Adding a package: add
<PackageVersion Include="X" Version="Y" />toDirectory.Packages.propsfirst, then reference with bare<PackageReference Include="X" />in the csproj. - Bumping a package: edit
Directory.Packages.propsonly. All projects move together. - Per-project override (rare, comment why):
<PackageReference Include="X" VersionOverride="Y" />. - Never add a nested
Directory.Build.propsthat disablesManagePackageVersionsCentrally. - After any package change run
dotnet restoreand commit refreshedpackages.lock.jsonfiles.
Gotchas
- Test SDKs drift first.
xunit,Moq,Microsoft.NET.Test.Sdk,coverlet.collectorare the most common offenders — check tests/** projects carefully during inventory. - Dockerfiles must COPY
Directory.Build.propsandDirectory.Packages.propsinto the build context before anydotnet restore. If the build context only includessrc/andglobal.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 ofNU1604warnings ("Project dependency X does not contain an inclusive lower bound") andNU1701framework warnings. EitherCOPY . .or list the two props files explicitly. - Global usings via
<PackageReference>(e.g.Microsoft.AspNetCore.Appframework 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 withdotnet restore --source https://api.nuget.org/v3/index.json. packages.lock.jsonchurn is large on the migration commit — expected; review project-by-project, not as one blob.VersionOverrideis not transitive. It only affects the project it's declared in. Use sparingly and document why.GlobalPackageReferenceinDirectory.Packages.propsapplies to all projects — useful for analyzers likeMicrosoft.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.propsthat re-disables CPM. - ❌ Bumping
<PackageVersion>and addingVersionOverride=in the same PR — pick one mechanism. - ❌ Migrating without committing refreshed
packages.lock.json— the next CI run will produce a noisy diff.
References
- references/agent-rules-snippet.md —
ready-to-paste rules block for
.github/copilot-instructions.mdorCONTRIBUTING.md. - Microsoft docs: Central Package Management
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
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.

