Lock Files & Locked Mode
NuGet's "lowest applicable version" rule already makes restores repeatable for direct references. It doesn't protect you against everything: floating versions move, transitive resolution changes when a package is unlisted or added to a feed, and a package can be re-uploaded with the same version and different content on a misconfigured private feed. A lock file records the complete resolved graph, with a content hash for every package, and locked mode makes restore fail instead of silently resolving something else.
Creating packages.lock.json
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
<PackageReference Include="xunit.assert" Version="2.9.3" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q
ls
cat packages.lock.json; echoapp.csproj
obj
packages.lock.json
{
"version": 1,
"dependencies": {
"net10.0": {
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[13.*, )",
"resolved": "13.0.4",
"contentHash": "pdgNNMai3zv51W5aq268sujXUyx7SNdE2bj1wZcWjAQrKMFZV260lbqYop1d2GM67JI1huLRwxo9ZqnfF/lC6A=="
},
"xunit.assert": {
"type": "Direct",
"requested": "[2.9.3, )",
"resolved": "2.9.3",
"contentHash": "/Kq28fCE7MjOV42YLVRAJzRF0WmEqsmflm0cfpMjGtzQ2lR5mYVj1/i0Y8uDAOLczkL3/jArrwehfMD0YogMAA=="
}
}
}
}
RestorePackagesWithLockFile=truemakes every restore write (or update)packages.lock.jsonnext to the project. Commit it.- Each entry has its
type(Direct,Transitive, orCentralTransitiveunder transitive pinning), therequestedrange for direct ones, the exactresolvedversion and acontentHash: the SHA-512 of the.nupkg. - The floating
13.*is recorded as requested[13.*, )and resolved 13.0.4. From now on, restores reuse 13.0.4 even when 13.0.5 appears, until you deliberately update the lock file.
Locked mode in CI
A normal restore treats the lock file as a cache: if the project changed, it re-resolves and rewrites the file. In CI you want the opposite: the build must use exactly what was reviewed and committed. That's dotnet restore --locked-mode, or <RestoreLockedMode>true</RestoreLockedMode> (typically set only when $(ContinuousIntegrationBuild) is true):
mkdir app && cd app
git init -q
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
XML
printf 'bin/\nobj/\n' > .gitignore
dotnet restore -v:q && git add -A && git commit -qm "lock"
echo "--- a teammate bumps the version but forgets the lock file"
sed -i 's/13.0.3/13.0.4/' app.csproj
dotnet restore --locked-mode 2>&1 | grep -E 'NU1004' | head -1 | sed 's|/home/learner/app/||'
echo "exit: ${PIPESTATUS[0]}"
echo "--- the fix: update the lock file deliberately"
dotnet restore --force-evaluate -v:q
git diff --stat
git diff -U0 packages.lock.json | grep '^[-+] '--- a teammate bumps the version but forgets the lock file
app.csproj : error NU1004: The package reference Newtonsoft.Json version has changed from [13.0.3, ) to [13.0.4, ).The packages lock file is inconsistent with the project dependencies so restore can't be run in locked mode. Disable the RestoreLockedMode MSBuild property or pass an explicit --force-evaluate option to run restore to update the lock file.
exit: 1
--- the fix: update the lock file deliberately
app.csproj | 2 +-
packages.lock.json | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
- "requested": "[13.0.3, )",
- "resolved": "13.0.3",
- "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
+ "requested": "[13.0.4, )",
+ "resolved": "13.0.4",
+ "contentHash": "pdgNNMai3zv51W5aq268sujXUyx7SNdE2bj1wZcWjAQrKMFZV260lbqYop1d2GM67JI1huLRwxo9ZqnfF/lC6A=="
The locked restore failed with NU1004 and a non-zero exit code. The fix is a normal restore (--force-evaluate re-resolves even when NuGet thinks nothing changed, which you also need after a floating version should move), and the lock file change goes into the same pull request as the project change, where reviewers see exactly which versions moved.
Content hashes catch tampered packages
Versions on nuget.org are immutable, but a private feed may allow re-pushing 1.0.0 with different content, or a cache may hold a corrupt copy. The lock file's hash notices:
mkdir lib && cd lib
cat > lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>Ada.Lib</PackageId>
<Version>1.0.0</Version>
</PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Lib { public static int Answer => 42; }' > Lib.cs
dotnet pack -o out -nologo -v:q && dotnet nuget push out/Ada.Lib.1.0.0.nupkg -s local > /dev/null
cd .. && mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ada.Lib" Version="1.0.0" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q
echo "--- someone re-publishes 1.0.0 with different code"
cd ../lib && sed -i 's/42/41/' Lib.cs && dotnet pack -o out -nologo -v:q
dotnet nuget push out/Ada.Lib.1.0.0.nupkg -s local
cd ../app && rm -rf ~/.nuget/packages/ada.lib obj
dotnet restore --locked-mode 2>&1 | grep -E 'NU1403' | head -1 | sed 's|/home/learner/app/||'--- someone re-publishes 1.0.0 with different code
Pushing Ada.Lib.1.0.0.nupkg to '/opt/nuget-feed'...
Your package was pushed.
app.csproj : error NU1403: Package content hash validation failed for Ada.Lib.1.0.0. The package is different than the last restore.
The new archive has a different SHA-512, so restore refuses it with NU1403. Without a lock file the tampered package would have been installed silently. Note that the second dotnet nuget push of the same version succeeded: a folder feed simply overwrites the file (--skip-duplicate isn't supported for folder feeds either). Hosted feeds such as nuget.org, Azure Artifacts and GitHub Packages reject a duplicate version, which is one more reason to prefer them over a file share for anything shared.
Lock files and Central Package Management
The two features combine well: CPM gives one place to choose versions; lock files record the whole resolved graph per project, including transitive packages. With CPM, set RestorePackagesWithLockFile once in Directory.Build.props (or Directory.Packages.props) so every project gets one.
Test yourself
What does each entry in packages.lock.json record besides the resolved version?
- The download URL
- A contentHash (SHA-512 of the package) and its type: Direct, Transitive or CentralTransitive
- The package's license
- The build time
CI runs dotnet restore --locked-mode and fails with NU1004. What happened?
- The feed is down
- The project's dependencies no longer match the committed lock file
- A package was tampered with
- The lock file format is too old
Which error indicates that a package's content differs from what the lock file recorded?
- NU1101
- NU1605
- NU1403
- NU1008