Sub topic 9
Updating dependencies safely
This chapter assumes you can already declare dependencies in Cargo.toml. It also assumes you know how SemVer requirements such as "1.2.3" and "~1.2.3" work, and that you know Cargo.lock records the exact versions Cargo chose. If any of that is unfamiliar, read the chapters on version requirements and Cargo.lock first.
By the end you will be able to:
- Say what
cargo updatechanges and what it leaves alone. - Update one package, pin one package, and preview changes before making them.
- Move across a breaking version boundary on purpose.
- Review and test an update before you commit it.
Two files, two jobs
An update touches two different files, and they answer different questions.
| File | Question it answers | Who edits it |
|---|---|---|
| `Cargo.toml` | Which versions am I willing to accept? | You |
| `Cargo.lock` | Which exact versions did Cargo pick? | Cargo |
cargo update only rewrites Cargo.lock. It picks the newest versions that still satisfy every requirement in Cargo.toml. It never edits Cargo.toml, so it cannot move you across a boundary you did not allow.
This is what makes routine updates low-risk. With serde = "1", cargo update can take you from 1.0.190 to 1.0.210 but never to 2.0.0.
When Cargo updates the lockfile by itself
Cargo does not change locked versions on every build. It leaves a package alone as long as the lockfile entry still satisfies the manifest. The lockfile changes without cargo update in three cases:
- You add a new dependency. Cargo locks the new package, and its dependencies, and leaves the other entries alone.
- You edit a requirement so the locked version no longer fits, for example changing
"1"to"2". - You remove a dependency. Cargo drops the entries that nothing needs any more.
A fresh cargo build on a machine that has your committed Cargo.lock reproduces the same versions. If Cargo.lock is not committed, a new checkout resolves again and may get newer versions. For binaries and applications, commit the lockfile.
Previewing an update
Run a dry run first. It resolves versions and prints what would change, but it does not write the lockfile.
cargo update --dry-runThe output lists each package that would move. The exact wording varies by Cargo version, but it looks like this:
Updating crates.io index
Locking 3 packages to latest compatible versions
Updating serde v1.0.190 -> v1.0.210
Updating serde_derive v1.0.190 -> v1.0.210
Updating syn v2.0.38 -> v2.0.77
note: pass `--verbose` to see 2 unchanged dependencies behind latestThe last line is useful. It tells you some packages are behind their newest release but held back by a requirement in your manifest, or in a dependency's manifest. Those are the candidates for a deliberate, manual upgrade, covered below.
Updating everything, or one package
Update everything within the allowed ranges:
cargo updateUpdate a single package and leave the rest of the lockfile as it is:
cargo update serdeThe older spelling cargo update -p serde still works and appears in many guides. Updating one package may also move its own dependencies, because the new version can require newer ones. It does not move unrelated packages.
To update only your workspace members' entries and leave external crates at their locked versions, use --workspace:
cargo update --workspacePinning a package to an exact version
--precise sets one package to a specific version. The version must satisfy your manifest requirement, and it can be older than the current one. This is the quickest way to roll back a bad release without touching Cargo.toml:
cargo update serde --precise 1.0.200If you later run a plain cargo update, the package moves forward again. A --precise pin lasts only until the next update. If you need the pin to be permanent, write it in the manifest instead:
[dependencies]
serde = "=1.0.200"Use = sparingly in libraries. An exact requirement in a library makes it hard for downstream projects to combine your crate with others that need a different version of the same dependency.
Crossing a breaking boundary on purpose
Newer major versions are not picked up by cargo update. To adopt one, you change the requirement yourself. cargo add can do this, because it rewrites the entry for a crate that is already present:
cargo add serde_json@2You can also edit the version string in Cargo.toml and run cargo build. Cargo re-resolves and rewrites the lockfile.
The boundary depends on the leftmost non-zero component:
| Current requirement | `cargo update` stops before | Manual change needed for the next step |
|---|---|---|
| `"1.4"` | 2.0.0 | `"2"` |
| `"0.12"` | 0.13.0 | `"0.13"` |
| `"0.0.7"` | 0.0.8 | `"0.0.8"` |
Pre-1.0 crates are the usual surprise. A move from 0.12 to 0.13 is allowed to break your code, so Cargo treats it like a major bump.
After a major upgrade, expect compile errors. Read the crate's changelog or migration guide before you start, and fix the errors one at a time rather than updating several major versions together.
A tool outside Cargo, cargo-outdated, lists dependencies that have newer versions than your manifest allows. cargo-edit provides cargo upgrade, which can rewrite the requirements in bulk. Both are optional extras installed with cargo install. They are not part of Cargo itself.
A safe update workflow
Treat an update like any other code change: make it on a branch, check it, and review the diff.
The same steps as commands:
git switch -c update-deps
cargo update --dry-run
cargo update
cargo build --all-targets
cargo test
git diff --stat Cargo.lock
git commit -am "Update dependencies within semver ranges"Notes on each step:
--all-targetsbuilds tests, examples and benchmarks too, so you catch breakage outsidesrc/.- If you use features, build with the feature combinations you ship. A new version of a dependency can change what a feature enables.
- Commit lockfile updates separately from feature work. If something breaks later,
git bisectcan point at the update commit.
Finding what broke
When tests fail after an update, narrow the cause by bisecting the update itself:
- Run
git restore Cargo.lockto return to the known-good state. - Update one package at a time with
cargo update <name>, testing after each. - When one package causes the failure, pin it with
--preciseto the last good version, and file an issue or read the release notes.
cargo tree helps here. cargo tree -i <name> shows which of your dependencies pull in the failing package, so you know whether the fix belongs in your code or in a dependency you can also update.
cargo tree -i synSecurity fixes and yanked versions
Updates are also how you receive security fixes. The RustSec advisory database lists known vulnerabilities, and the third-party cargo-audit tool checks your Cargo.lock against it:
cargo install cargo-audit
cargo auditIf an advisory names a version you have locked, a patch-level cargo update <name> is usually enough. If the fixed version is outside your requirement, you need a manual bump as described above.
Crate authors can yank a release on crates.io. A yanked version stays downloadable for projects whose Cargo.lock already contains it, so your build keeps working. New resolutions skip it, though. If you run cargo update and a locked version has been yanked, Cargo moves you to a non-yanked version.
Common mistakes
- Expecting
cargo updateto changeCargo.toml. It does not. If a package is stuck behind its latest release, check the requirement. - Running a bare
cargo updatejust before a release. You then ship versions that nobody has tested. Update early in a cycle, with time to test. - Ignoring dependencies behind the latest version. The
--verbosenote andcargo-outdatedshow them. Staying far behind makes each future jump larger. - Not committing the lockfile for an application. Then different machines build different versions, and a rollback with
--precisehas nothing to record. - Using
*as a requirement. It accepts any version, including a breaking major release on a fresh resolve. crates.io also rejects wildcard-only requirements when you publish.
Summary
cargo updatere-resolves within the ranges inCargo.tomland rewrites onlyCargo.lock.- Use
--dry-runto preview, a package name to limit the scope, and--preciseto pin or roll back. - Major versions, and 0.x minor versions, need a manual edit to the requirement.
- Update on a branch, test with all targets, and review the lockfile diff before merging.
Exercise
Your manifest has rand = "0.8" and crates.io has just released rand 0.9.0. You run cargo update. Does the lockfile move to 0.9.0? What do you do if you want it to, and what do you do if a later 0.8 release breaks your tests and you want to go back to 0.8.5?
Show answer
No. For a 0.x version the leftmost non-zero component is the minor number, so "0.8" allows only 0.8.x releases and cargo update will not choose 0.9.0. To adopt it, change the requirement to "0.9" (for example with cargo add [email protected]) and fix any compile errors. To go back to 0.8.5 without editing the manifest, run cargo update rand --precise 0.8.5. That pin lasts only until the next update.
Exercise
A teammate says: "I ran cargo update -p serde and Cargo.toml didn't change, so the update did nothing." What is wrong with that conclusion, and which file should they check?
Show answer
cargo update never edits Cargo.toml. Its effect is recorded in Cargo.lock. They should run git diff Cargo.lock to see whether serde, or packages it depends on, moved to newer versions within the existing requirement.