Cargo.lock, cargo update & --precise
The previous lesson showed that units = "1" means "the newest 1.x". Newest when? If the answer were "whenever someone builds", two developers could compile different code from the same commit. Cargo.lock fixes that: the first resolution is written down, and every later build reuses it until you deliberately change it.
What the lockfile records
cargo new app --vcs none -q
cd app
cargo add anyhow serde -q
cat Cargo.lock# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anyhow"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "app"
version = "0.1.0"
dependencies = [
"anyhow",
"serde",
]
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
version = 4is the lockfile format, not your project's version.- Every package in the graph is listed once per version, with its exact
version, itssource(the registry URL; thecrates-ioindex here, even though a local mirror served the files) and a SHA-256checksumof the.cratearchive. Cargo verifies the checksum on download, so a tampered or re-uploaded archive fails the build. dependencieslists edges of the graph. A bare name means "the only version of it in the lockfile"; when two versions coexist, entries carry the version too.- Your own package appears without a source or checksum.
The file is generated. Don't edit it by hand; use the commands below.
A lockfile in action
Here a library units has version 1.0.0 when the app first builds. Then 1.1.0 and 2.0.0 are published:
crate-registry start
cargo new --lib units --vcs none -q
cd units
sed -i 's/^edition.*/&\ndescription = "Unit helpers"\nlicense = "MIT"\ndocumentation = "https:\/\/example.com"/' Cargo.toml
publish() {
sed -i "s/^version = .*/version = \"$1\"/" Cargo.toml
echo "pub const VERSION: &str = \"$1\";" > src/lib.rs
cargo publish --registry local --no-verify -q
}
publish 1.0.0
cd ..
cargo new app --vcs none -q
cd app
echo 'units = { version = "1", registry = "local" }' >> Cargo.toml
echo 'fn main() { println!("units {}", units::VERSION); }' > src/main.rs
cargo run -q
echo "--- 1.1.0 and 2.0.0 are published"
(cd ../units && publish 1.1.0 && publish 2.0.0)
cargo run -q
echo "--- what would an update do?"
cargo update --dry-run --verbose 2>&1
echo "--- update for real"
cargo update 2>&1
cargo run -q
echo "--- go back to 1.0.0 on purpose"
cargo update -p units --precise 1.0.0 2>&1
cargo run -q
echo "--- 2.0.0 is outside the requirement"
cargo update -p units --precise 2.0.0 2>&1units 1.0.0
--- 1.1.0 and 2.0.0 are published
units 1.0.0
--- what would an update do?
Updating `local` index
Locking 1 package to highest Rust 1.99.1 compatible version
Updating units v1.0.0 (registry `local`) -> v1.1.0 (available: v2.0.0)
note: to see how you depend on a package, run `cargo tree --invert <dep>@<ver>`
warning: not updating lockfile due to dry run
--- update for real
Updating `local` index
Locking 1 package to highest Rust 1.99.1 compatible version
Updating units v1.0.0 (registry `local`) -> v1.1.0 (available: v2.0.0)
units 1.1.0
--- go back to 1.0.0 on purpose
Updating `local` index
Downgrading units v1.1.0 (registry `local`) -> v1.0.0
units 1.0.0
--- 2.0.0 is outside the requirement
Updating `local` index
error: failed to select a version for the requirement `units = "^1"`
candidate versions found which didn't match: 2.0.0
location searched: `local` index
required by package `app v0.1.0 (/home/learner/app)`
The sequence is the whole lockfile story:
- The first build locked 1.0.0.
- After 1.1.0 appeared,
cargo runstill used 1.0.0: the lockfile wins over "newest". cargo update --dry-run --verbosepreviews the change and also mentions that 2.0.0 exists but is not compatible with"1".cargo updatere-resolved everything to the newest compatible versions and rewroteCargo.lock.cargo update -p units --precise 1.0.0moved one package to an exact version (up or down), for example to step back from a release with a regression.--precisecan't break the requirement inCargo.toml: to move to 2.x you change the manifest (cargo add units@2 --registry local) and fix your code.
Useful update commands
| Command | Effect |
|---|---|
cargo update |
re-resolve every dependency to the newest compatible version |
cargo update -p serde (or cargo update serde) |
update one package (and whatever must move with it) |
cargo update -p serde --precise 1.0.210 |
set one package to an exact version |
cargo update --dry-run |
show what would change, write nothing |
cargo update --workspace |
only refresh the workspace's own entries (after renaming or re-versioning members) |
cargo generate-lockfile |
create Cargo.lock without building |
Keeping CI honest: --locked, --offline, --frozen
By default, cargo build updates the lockfile silently when Cargo.toml asks for something the lockfile doesn't satisfy (a new dependency, a changed requirement). On your laptop that's convenient. In CI it means the build isn't using the lockfile you reviewed. Three flags stop that:
| Flag | Meaning |
|---|---|
--locked |
fail if Cargo.lock would have to change |
--offline |
never touch the network; resolve with what's already downloaded |
--frozen |
both: --locked + --offline |
cargo new app --vcs none -q
cd app
cargo add anyhow -q
cargo generate-lockfile -q
echo "--- someone adds a dependency but doesn't commit the new Cargo.lock"
echo 'thiserror = "2"' >> Cargo.toml
cargo build --locked 2>&1--- someone adds a dependency but doesn't commit the new Cargo.lock
error: cannot update the lock file /home/learner/app/Cargo.lock because --locked was passed to prevent this
help: to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
cargo install deserves a mention here: it ignores a published crate's lockfile unless you pass --locked, so cargo install --locked some-tool is the reproducible way to install tools in CI.
Should you commit Cargo.lock?
Yes, for almost everything. Since 2023 the Cargo team's guidance is to commit it by default, for libraries too; cargo new no longer adds it to .gitignore. A committed lockfile makes CI builds reproducible and git bisect meaningful.
For a library, the lockfile only affects your builds (tests, CI): when someone depends on your library, Cargo ignores your Cargo.lock and resolves from your requirements. So a library's CI should also test against fresh versions now and then (a scheduled job that runs cargo update before testing), or you'll miss a dependency release that breaks your users while your locked CI stays green.
Test yourself
Cargo.lock has units 1.0.0, Cargo.toml says units = "1", and 1.1.0 is published. What does cargo build use?
- 1.1.0, the newest compatible
- 1.0.0, the locked version
- It fails
- Whichever is cached
Which command moves only units back to exactly 1.0.0?
- cargo update units@1.0.0
- cargo update -p units --precise 1.0.0
- cargo add units@=1.0.0 --lock
- cargo build --precise units=1.0.0
What does cargo build --locked do when Cargo.toml has a new dependency missing from Cargo.lock?
- Adds it to Cargo.lock
- Fails, because the lockfile would have to change
- Ignores the new dependency
- Downloads it without locking