What is a NuGet lock file (packages.lock.json), and what problem does it solve for reproducible builds?
Short answer
A lock file records the exact resolved version of every direct and transitive dependency at restore time, ensuring that subsequent restores (across different machines, CI runs, or dates) produce bit-for-bit identical dependency trees rather than potentially resolving to newer…
Advertisement
AModel answer
A lock file records the exact resolved version of every direct and transitive dependency at restore time, ensuring that subsequent restores (across different machines, CI runs, or dates) produce bit-for-bit identical dependency trees rather than potentially resolving to newer compatible versions that happen to be available -- critical for reproducible builds and catching unexpected dependency changes in code review.
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
# Generates packages.lock.json, committed to source control
# dotnet restore --locked-mode fails the build if resolution would differ from the lock fileReal-world example
A CI pipeline runs `dotnet restore --locked-mode` to guarantee that a build today resolves the exact same dependency versions as it did when the lock file was last committed, catching any unexpected drift immediately.
Likely follow-ups
- How do you update the lock file when you intentionally want a new version?
- How does this compare to npm's package-lock.json conceptually?
Related topics