All jobs

Add NuGet Lock Files to a .NET Solution

Yes. An agent turns on NuGet lock files for every project in your .NET solution, makes CI restore in locked mode, and opens a PR with the lock files and the proof.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how the solution restores before it pins anything. In a solution like jasontaylordev/CleanArchitecture, CleanArchitecture.slnx lists seven projects under src (AppHost, ServiceDefaults, Application, Domain, Infrastructure, Shared, Web) and six under tests. Directory.Build.props sets TargetFramework to net10.0, TreatWarningsAsErrors to true and ArtifactsPath to an artifacts folder, so every project already inherits one file. Directory.Packages.props turns on ManagePackageVersionsCentrally and CentralPackageTransitivePinningEnabled and holds every version in one place: MediatR 14.2.0, Microsoft.EntityFrameworkCore 10.0.14, Scalar.AspNetCore 2.18.3, and a System.Security.Cryptography.Pkcs entry pinned to 10.0.14 with a comment naming the two advisories the transitive 10.0.12 carried. global.json asks for SDK 10.0.403 with rollForward set to latestFeature. There is no packages.lock.json anywhere; .gitignore excludes the old project.lock.json but says nothing about the current name.

Then it reads CI. .github/workflows/build.yml caches ~/.nuget/packages under a key built from hashFiles('**/Directory.Packages.props', '**/*.csproj'), runs dotnet restore, dotnet build --no-restore --configuration Release and dotnet test --no-build. test-templates.yml is different: it installs the repository as a dotnet new template, generates a fresh solution per client-framework and database pair, and builds that. renovate.json groups NuGet updates as .NET dependencies on Mondays and automerges minor and patch bumps. The agent also notices that the csproj files hold template conditionals such as <!--#if (UsePostgreSQL)--> as XML comments, which MSBuild ignores, so the template repository's own restore sees the Npgsql, SQL Server and SQLite packages at once while a generated solution sees one of them.

The change is small and lands in the file every project already imports. Directory.Build.props gains <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>, one dotnet restore writes a packages.lock.json beside each of the thirteen csproj files, and with transitive pinning on, the lock records the pinned 10.0.14 Pkcs assembly rather than the version the graph would otherwise pick. In build.yml the restore step becomes dotnet restore --locked-mode, so a Directory.Packages.props edit that nobody restored fails the job with NU1004 instead of silently resolving something new, and the cache key adds **/packages.lock.json so a lock change invalidates it. test-templates.yml is left alone on purpose: a generated solution starts without lock files, and its first restore now writes them, which is the behaviour a template user wants. The pull request says why the template repository's lock files list all three database providers.

The proof is the restore's own output. The agent runs dotnet restore --locked-mode on the clean tree to show it passes, then edits one version in Directory.Packages.props without restoring and runs it again to show the NU1004 failure, then reverts. It builds in Release with --no-restore and runs the Domain.UnitTests and Application.UnitTests projects with --no-build; the functional and acceptance tests need the Aspire app host and a browser, so the description says they were left to CI. It also checks that Renovate's nuget manager updates packages.lock.json when it bumps a version, so the grouped Monday pull requests keep passing locked mode.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add NuGet lock files to this .NET solution. Read the solution file, Directory.Build.props, Directory.Packages.props, global.json, every csproj, renovate.json and the workflows under .github/workflows first. Set RestorePackagesWithLockFile in Directory.Build.props so every project gets a packages.lock.json, run dotnet restore to generate them, and make the main build workflow restore with --locked-mode and include the lock files in its NuGet cache key. If the repository is a dotnet new template, leave the template-testing workflow restoring fresh and explain why in the pull request. Prove it: show dotnet restore --locked-mode passing on the clean tree and failing with NU1004 after an unrestored version edit, build in Release, run the unit test projects, and include that output. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request with one new property in Directory.Build.props, a packages.lock.json beside each csproj, thirteen in a solution shaped like this one, and two edits in build.yml: --locked-mode on the restore step and the lock files in the cache key. No package version changes; the lock files record what the current Directory.Packages.props already resolves to.

The description quotes the passing locked restore, the NU1004 message from the drift check, and the build and unit test summaries. It names which test projects ran in the sandbox and which it left to CI. The commands and their full output are in the transcript at GET /o/{org}/sessions/{id}/messages.

Before merging, a reviewer scans one lock file to confirm the pinned transitive versions are the intended ones, decides whether the template-testing workflow should also run in locked mode, and checks that the next Renovate pull request updates the lock files alongside Directory.Packages.props. A follow-up message to the same session can add RestoreLockedMode as an MSBuild property for local builds too.

Run it on every .NET repository you maintain

A team with several .NET services starts one run per repository with one POST /o/{org}/sessions each, the repository in resources and this prompt as input. Each run reads its own project layout, so a repository without central package management gets lock files from its csproj versions, and each opens its own pull request.

A key runs at most max_concurrent runs at once, four by default; a run past that is still created, answers 202 with status: "queued" and starts when a slot frees, so a loop over repositories needs no retry logic. Send a request_id, a UUID v5 of the repository's full name, so a repeated call returns the run it already started.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Lock files are only worth having if CI enforces them, and enforcing them means touching the restore step, the cache key and, in a template repository, knowing which workflow must not be touched. The agent reads all of that, generates the lock files with the real SDK the global.json asks for, and shows the enforcement working by breaking it once on purpose.

Everything happens in a sandbox of its own with the output kept, so the pull request carries the NU1004 failure a reviewer would otherwise have to reproduce. Several repositories can be done at once, each in its own sandbox, each with its own pull request.

Frequently asked questions

Does locked mode work with central package management?

Yes. RestorePackagesWithLockFile works with ManagePackageVersionsCentrally, and the lock file records the resolved graph including any CentralPackageTransitivePinningEnabled overrides, such as a transitive package pinned to a patched version. Editing Directory.Packages.props without restoring is exactly what --locked-mode then catches.

Why would a lock file list PostgreSQL, SQL Server and SQLite packages at once?

Because in a dotnet new template the conditional blocks around package references are XML comments, which MSBuild does not evaluate. The template repository's own restore therefore includes every provider, while a solution generated from the template includes one. The pull request explains this so the extra entries do not look like a mistake.

Will Renovate keep the lock files up to date?

Renovate's NuGet manager updates packages.lock.json when it changes a package version, so a grouped update pull request still passes a locked restore. The agent checks the repository's renovate.json and says in the description whether anything there needs to change.

What happens when someone edits a version and forgets to restore?

The CI restore fails with NU1004, which names the project and the mismatch, instead of resolving a version nobody reviewed. The fix is one local dotnet restore and a commit of the updated lock file. The pull request shows this failure once so the team knows what it looks like.

More .NET jobs

More jobs like this: Hand off a long task

Keep reading