What is packages.lock.json in .NET and why enable it?
IntermediateConcept.NET builds (dotnet CLI)
Answer
Short answer
packages.lock.json is .NET's NuGet lockfile. When enabled, it records the exact resolved version (and content hash) of every direct and transitive package, so dotnet restore produces the same result every time — like package-lock.json for Node.
Enable it
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
dotnet restore # creates/updates packages.lock.json → commit it
dotnet restore --locked-mode # CI: fail if the lockfile would change
Why it matters
- Floating versions (
Version="8.*") and transitive dependencies can resolve differently over time. --locked-modein CI catches un-committed dependency changes, exactly likenpm ci.- The lockfile is also a great cache key for
~/.nuget/packages.
What to say in the interview
"packages.lock.json pins the full NuGet graph with hashes. We commit it and restore with --locked-mode in CI so builds are reproducible and dependency changes always go through review."