Sub topic 6
Cargo.lock and reproducible builds
This chapter assumes you can already declare a dependency in Cargo.toml, read a SemVer requirement such as "1.2.3" or "~1.2.3", and run cargo build. Here you will learn what Cargo.lock records, how Cargo keeps it in sync with the manifest, when to commit it, and which flags make a build fail instead of silently changing it.
Requirements describe a range, the lock file picks a version
A requirement like serde = "1" allows every release from 1.0.0 up to, but not including, 2.0.0. Many versions satisfy it. Cargo has to choose one, and a later build could choose a different one if a newer release has been published.
Cargo.lock records that choice. It is a generated file in the project root, next to Cargo.toml. Cargo creates it the first time you run a command that needs to resolve dependencies, such as cargo build, cargo check or cargo test. After that, Cargo reuses the recorded versions instead of looking for newer ones.
The two files have different jobs:
| File | Written by | Contains | Meaning |
|---|---|---|---|
| `Cargo.toml` | You | Version requirements (ranges) | What versions your code is willing to work with |
| `Cargo.lock` | Cargo | Exact versions, sources, checksums | What versions were actually used |
Do not edit Cargo.lock by hand. Change Cargo.toml or run a Cargo command, and Cargo rewrites the lock file for you.
What the file looks like
Create the project from earlier in this tutorial and build it once:
cargo new json_demo
cd json_demo
cargo add serde --features derive
cargo add serde_json
cargo buildOpen the generated Cargo.lock. An abbreviated excerpt looks like this. Your version numbers and checksums will differ, because they depend on what was newest when you ran the command.
# 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.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "<sha-256 hex digest>"
dependencies = [
"serde_derive",
]Each [[package]] entry describes one crate in the resolved graph. The file lists your own package and every transitive dependency, not only the ones you named in Cargo.toml.
The fields mean:
nameandversionidentify the exact release.sourcesays where the crate came from. For crates.io it is the registry index.checksumis a SHA-256 digest of the downloaded.cratearchive. Cargo checks it on download, so a tampered or corrupted archive is rejected.dependencieslists the other packages this one uses, by name. If two versions of a crate exist in the graph, the entry also includes the version so the reference is unambiguous.
The top-level version = 4 is the lock file format, not a package version. Newer Cargo releases can write newer formats, and older Cargo releases may refuse to read them. If a teammate on an older toolchain reports a parse error on Cargo.lock, compare toolchain versions first.
How different sources appear
| Dependency kind | `source` field | `checksum` field |
|---|---|---|
| crates.io | `registry+https://github.com/rust-lang/crates.io-index` | Present |
| git | `git+<url>#<full commit hash>` | Absent |
| path | Absent | Absent |
A git dependency is pinned to a specific commit hash, even if Cargo.toml names only a branch. The branch can move, but the lock file keeps pointing to the recorded commit until you update it. Path dependencies live on your disk, so there is nothing to pin or verify.
How Cargo uses the lock file
On every build, Cargo compares Cargo.toml with Cargo.lock. If every recorded version still satisfies the manifest, Cargo uses the lock file as is and does not contact the registry for newer versions. If the manifest changed in a way the lock no longer satisfies, Cargo re-resolves only what it needs and rewrites the file.
Three common events cause a rewrite:
- You add a dependency, for example with
cargo add. Cargo adds the new crate and its dependencies and leaves existing entries alone. - You change a requirement so the locked version no longer fits, for example from
"1"to"2". Cargo picks a new version for that crate. - You ask for newer versions with
cargo update. That command is covered in its own chapter.
The important point is what does not cause a rewrite. Publishing serde 1.0.999 on crates.io changes nothing for your project. Your build keeps using the version in the lock file until you ask for an update.
Making builds fail instead of drift
Sometimes you want a build to prove that the lock file is already correct. Continuous integration is the usual case. Cargo has flags for that:
| Flag | Effect |
|---|---|
| `--locked` | Fails if `Cargo.lock` would need to change, or does not exist |
| `--offline` | Never touches the network. Uses only crates already downloaded |
| `--frozen` | Same as `--locked` and `--offline` together |
A typical CI script looks like this:
cargo build --locked
cargo test --lockedIf a developer edits Cargo.toml and forgets to commit the matching Cargo.lock, --locked fails the pipeline with a message that the lock file needs to be updated. Without the flag, CI would quietly resolve its own versions and pass, and the code that passed would differ from the code on the developer's machine.
cargo install has a related trap. By default it ignores the Cargo.lock that the crate's author published and resolves fresh versions within the allowed ranges. Pass --locked to use the author's tested set:
cargo install --locked ripgrepIf a tool fails to install because a transitive dependency released a breaking change, retrying with --locked is often the fix.
Should you commit Cargo.lock?
Commit Cargo.lock for any project that produces a program: binaries, services, command-line tools. The lock file is what lets another developer, a CI runner or a deploy server build the same dependency versions you tested.
For libraries, the situation differs in one way. When someone adds your library as a dependency, Cargo ignores your Cargo.lock completely. It resolves versions using the requirements in your Cargo.toml together with everything else in their project. The lock file still helps you: your own tests and CI run against a fixed set, and bug reports are easier to reproduce. Current Cargo guidance is to commit it for libraries too, unless you have a specific reason to test against the newest versions on every run.
This has a consequence for library authors. Your Cargo.toml requirements must be correct on their own. A library that compiles only because your lock file pins foo 1.4.2, while its manifest says foo = "1", will break for users who resolve foo 1.0.0. Testing the lowest allowed versions occasionally is the way to catch that.
What the lock file does not pin
A lock file makes the dependency versions repeatable. A fully reproducible build needs more than that. These inputs are outside the lock file:
- The Rust compiler version. Different
rustcreleases can produce different binaries and can add new warnings. Pin it with arust-toolchain.tomlfile. - Enabled features. The lock file records which crates are in the graph, but features are chosen by your manifest and by the command line flags such as
--featuresand--no-default-features. - The target platform. Platform-specific dependencies appear in the lock file for all targets, but only some are compiled on a given machine.
- Build scripts and system libraries. A
build.rsthat links against a library on the host can behave differently on two machines with the same lock file. - Yanked crates. The author of a crate can yank a release on crates.io. Yanking does not delete it, and existing lock files that name it still build. New resolution will not pick it.
Inspecting what the lock file chose
You do not need to read Cargo.lock by eye. cargo tree reads it and prints the resolved graph, which is covered in a later chapter. For a quick look, a grep shows the selected version of one crate:
grep -A1 '^name = "serde"' Cargo.lockIf the same crate appears twice with different versions, two of your dependencies require incompatible ranges, for example one wants 0.7 and another wants 0.8. Cargo allows one version per SemVer-compatible range and links both into the program. That is legal, but it increases build time and binary size.
Exercise
Your teammate adds regex = "1" to Cargo.toml and pushes without committing Cargo.lock. Your CI runs cargo build --locked. What happens, and what should the teammate do?
Show answer
The build fails, because Cargo would have to add regex and its dependencies to Cargo.lock, and --locked forbids changing the file. The teammate should run cargo build locally to update Cargo.lock, then commit both Cargo.toml and Cargo.lock together.
Exercise
A published library has foo = "1" in its manifest, and its own Cargo.lock pins foo 1.4.2. A user adds the library to an application whose lock file already pins foo 1.1.0. Which version of foo does the application build use, and why?
Show answer
It uses foo 1.1.0. Cargo ignores the library's Cargo.lock when the library is a dependency, and the application's own lock file keeps the existing version as long as it satisfies the requirement "1".