skip to content

In NuGet, what do RestorePackagesWithLockFile and dotnet restore --locked-mode do, and why would a bank's CI pipeline restore in locked mode?

level: middleimportance: should knowfreq 45%

answer

  1. packages.lock.json at the project root
  2. once the file exists it is always used
  3. --locked-mode or RestoreLockedMode fails on change
  4. NU1004 inconsistent, NU1403 content hash
  5. --force-evaluate refreshes floating versions

basics

~10 s

RestorePackagesWithLockFile makes restore write packages.lock.json with every resolved package and its content hash. --locked-mode (RestoreLockedMode) restores exactly that file and fails with NU1004 when the project's dependencies no longer match it.

solid answer

~40 s

Setting `RestorePackagesWithLockFile` to `true` (or `dotnet restore --use-lock-file`) makes restore write **`packages.lock.json`** at the project root: the full closure per target framework, each package's resolved version and its content hash. Once that file exists it is always used, property or not. In normal mode restore reuses it when dependencies are unchanged and **rewrites** it when they changed. In **locked mode** — `dotnet restore --locked-mode` or `RestoreLockedMode=true` — restore installs exactly what the file lists or fails: **NU1004** when the project's dependencies or target frameworks no longer match, **NU1403** when a package's bytes differ from the recorded hash. For a bank's CI that turns "the graph changed" into a reviewed commit instead of a silent drift, and catches a re-pushed package. Floating versions only move with `--force-evaluate`.

code

bash · 7 lines
bash
# developer machine: change dependencies, regenerate, commit the lock file
dotnet package add Polly.Core --version 8.8.0
git add Payments.Api/packages.lock.json

# CI: refuse any graph that differs from the committed lock file
dotnet restore Payments.sln --locked-mode
dotnet build Payments.sln --no-restore -c Release

go deeper

for a junior

Know that RestorePackagesWithLockFile writes packages.lock.json, that you commit it with dependency changes, and that CI runs restore with --locked-mode.

for a middle

Explain normal versus locked mode, what NU1004 and NU1403 report, and how --force-evaluate interacts with floating versions.

for a senior

Make lock files work across a solution: Directory.Build.props, SDK pinning against implicit references, pruning diffs in .NET 10, and applications versus libraries.

for a principal

Decide how a regulated organisation proves build inputs: lock files, controlled feeds and SDK pinning together, and the cost of refreshing them on a schedule.

## The problem the lock file solves in NuGet NuGet's lowest-applicable rule is already deterministic for fixed versions, so why lock? Because the inputs can change underneath you: a floating `8.*` picks up a new release, a floor that was missing appears on the feed ("day 2: version 4.0.0 gets published"), or a private feed that allows deletions loses a version and NuGet resolves the next best match. A regulated payments build needs to prove that the binaries built today used the same packages as the ones that passed review last week. The lock file records that closure. ## Turning it on ```xml <PropertyGroup> <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> </PropertyGroup> ``` Restore then writes `packages.lock.json` beside the project file. It lists, per target framework, every direct, transitive and project dependency with its resolved version and a **content hash** of the package. Important details: - Once a `packages.lock.json` exists in the project root, restore **always** uses it, even if the property is later removed. - Several projects in one folder can use `NuGetLockFilePath` (`--lock-file-path`) to get project-specific files. - Put the property in `Directory.Build.props` to cover the whole solution. ## Normal mode versus locked mode | Mode | Dependencies unchanged | Dependencies changed | |---|---|---| | normal restore | restores what the lock file lists, no re-evaluation | re-evaluates the graph and **rewrites** the lock file | | `--locked-mode` / `RestoreLockedMode=true` | restores what the lock file lists | **fails** with NU1004 | Normal mode is what developers run: add a package, restore, commit the updated lock file with the change. Locked mode is for CI and release builds: nobody may change the graph on the build agent. The NU1004 message names the cause — for example that a package reference's range changed or that the project's target frameworks differ from the lock file's. Independently of the mode, restore compares each package's bytes with the recorded hash. If the package in the global packages folder differs from the value in the lock file, restore fails with **NU1403**, "The package is different than the last restore." That is the case of a feed that let someone delete and re-push the same version with different contents. ## Floating versions and refreshes With a lock file, a floating version stops floating: restore keeps the locked version as long as the project's declared dependencies are unchanged. To move it deliberately, run `dotnet restore --force-evaluate` (`RestoreForceEvaluate`), which re-resolves everything and rewrites the file. The same command is the documented fix for an NU1403 once you have confirmed which bytes are correct. ## Applications versus libraries NuGet's guidance is specific: - **Commit** the lock file for applications and executables — the projects at the start of the dependency chain. - **Do not rely on** a library's lock file: when an application references that library, the application's own restore decides versions, so the library's locked choices have little say. ## Wiring it into a pipeline A workable setup for a payments solution: 1. Set `RestorePackagesWithLockFile` to `true` in `Directory.Build.props` and run one restore so every application project gets its `packages.lock.json`. 2. Commit the lock files; treat any diff in them as part of the code review of a dependency change. 3. In CI, run `dotnet restore --locked-mode` once, then `build --no-restore`, so no later step can quietly re-resolve. 4. Refresh on a schedule — or when an audit finding needs a fixed version — with `--force-evaluate` on a branch, and review the resulting diff like any other change. When NU1004 appears, read its reasons: the message states what no longer matches, such as a reference whose range changed from `[2.0.0, )` to `[1.2.3, )`, or target frameworks that differ between the project and the lock file. Either the project change was intended and the developer forgot to commit the regenerated lock file, or something changed that nobody meant to change. ## Traps in practice 1. **Implicit SDK references.** Some project types get implicit `PackageReference` items from the SDK, so a different SDK version changes the graph and locked mode fails. The docs suggest pinning the SDK with `global.json` and `rollForward` set to `disable` so developers and CI move together. 2. **Package pruning in .NET 10.** Projects targeting .NET 10 prune framework-provided packages; the locked-mode check is pruning-aware, but the next regeneration drops pruned packages and produces a larger diff than usual. 3. **Forgetting to commit.** A developer who adds a package but not the regenerated lock file gets a red CI build with NU1004 — which is the point.

  • Why can a library's packages.lock.json not guarantee what its consumers restore?
    Consumers restore their own graph. If an application references the library and also needs a higher version of a shared package, the application's lock file records that higher version; the library's file only governed the library's own build. NuGet's docs therefore recommend committing lock files for applications rather than relying on them in shared libraries.
  • Locked mode fails with NU1403 on a build agent but not on your laptop. What happened?
    The package in the agent's global packages folder has a different content hash from the lock file, typically because the same version was re-pushed with different contents on a private feed. Find which bytes are authoritative, fix the feed or the cache, and only then refresh the lock file with `--force-evaluate`.

  • Removing RestorePackagesWithLockFile stops NuGet using an existing packages.lock.json.
  • In normal mode restore fails whenever the lock file is out of date.
  • --locked-mode silently updates the lock file when the project's references change.
  • A floating 8.* keeps moving to new releases even with a lock file present.
  • A library's committed lock file decides the versions its consumers get.