Sub topic 6
Cargo.lock and reproducible builds
This chapter assumes you can run cargo build and read a [dependencies] table. It also assumes you know what a SemVer requirement such as "1" means. Here you will see how Cargo turns those loose requirements into one fixed set of versions, and how to keep that set stable.
Requirements versus exact versions
A requirement like serde = "1" accepts any 1.x.y release. Two builds on different days could pick different versions if a new release appears in between. Cargo.lock removes that uncertainty. It records the exact version of every package in the dependency graph, including indirect ones.
Cargo creates the file the first time you build, check, or run a project. It then reuses the recorded versions on later builds. You do not edit it by hand.
What the file contains
Each package gets a [[package]] entry. The following shape is typical (abbreviated):
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "json_demo"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "serde"
version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index"Real entries for registry packages also carry a checksum line. Cargo uses it to detect a downloaded crate that differs from the one it recorded. The top-level version is the lock file format, not a package version. Its value depends on your Cargo release.
Entries without a source line are local packages. Git dependencies record the exact commit in source, so a git dependency without a rev still stays fixed until you update it.
When to commit it
Commit Cargo.lock for any project that produces a binary, such as a command-line tool, a service, or an application. Everyone who clones the repository, and your CI, then builds with the same versions you tested.
For libraries, the lock file is ignored by projects that depend on yours. Those projects resolve versions from their own lock file. Older guidance said to leave it out of version control. Current Cargo guidance says committing it is fine and often useful, because it makes your own CI reproducible. Either way, a library must still be tested against the newest versions its requirements allow, since that is what downstream users will get.
Enforcing the lock
By default, Cargo quietly rewrites Cargo.lock if Cargo.toml changed. In CI you usually want a failure instead:
cargo build --locked
cargo test --lockedWith --locked, Cargo errors if the lock file would need changes, for example when someone edited Cargo.toml and forgot to commit the new lock file. --frozen is stricter: it also forbids network access. --offline only forbids network access and still lets Cargo update the lock file from already-downloaded crates.
Reproducible here means the same dependency versions. It does not guarantee bit-identical binaries, because the compiler version, target, and build flags also matter. Pin the toolchain with a rust-toolchain.toml file if you need that.
Changing the lock
The lock file changes only when you ask, or when your manifest forces it:
cargo add,cargo remove, or a manualCargo.tomledit can add or drop entries.cargo updatere-resolves to the newest versions your requirements allow. The next chapters cover this.
Merge conflicts in Cargo.lock are common on busy branches. Resolve them by taking either side, then running any Cargo command such as cargo check. Cargo regenerates a consistent file.
Exercise
Your CI runs cargo build, and a teammate's pull request edits Cargo.toml to add a dependency without committing the updated Cargo.lock. How can you make CI catch this, and why does it matter for a binary project?
Show answer
Run cargo build --locked (and cargo test --locked) in CI. Cargo then fails because the lock file would need to change. This matters because a binary project should build with the exact versions that were reviewed and committed, rather than versions resolved fresh on each CI run.