Sub topic 9
Updating dependencies safely
This chapter assumes you can run cargo new, edit Cargo.toml, and have read the chapters on SemVer requirements and Cargo.lock. Here you will learn what cargo update changes, what it leaves alone, and how to review an update before you commit it.
What an update changes
Two files describe your dependencies. Cargo.toml holds the version requirements you wrote, such as serde = "1". Cargo.lock holds the exact versions Cargo picked, such as serde 1.0.200.
cargo update rewrites Cargo.lock only. It picks the newest versions that still satisfy the requirements in Cargo.toml. It never edits Cargo.toml, so it cannot move you across a boundary such as 1.x to 2.x.
# Update every package within its allowed range
cargo update
# Update one package within its allowed range
cargo update -p serdeThe second command moves serde and only the other packages that must change to stay compatible with it. Prefer this form. A small update is easier to review and easier to undo.
Preview before you change anything
Add --dry-run to see what would change without writing Cargo.lock:
cargo update --dry-runThe output lists lines such as Updating serde v1.0.199 -> v1.0.200. Read this list before you accept it. A patch bump in a crate you know is routine. A bump in a crate that you did not expect, or a long list of changes, deserves a closer look.
Crossing a major version
To adopt a new major version, you must change the requirement yourself. cargo update will not do it.
# Rewrite the requirement in Cargo.toml and update the lock file
cargo add [email protected]Then build. A major release may rename functions or change types, so expect compile errors and read the crate's changelog. You can see which crates have newer releases with cargo tree or by checking crates.io. The cargo outdated subcommand does this too, but it is a separate install.
The --precise flag does the opposite and pins a package to a version you name. It is useful when a new release breaks your build:
cargo update -p serde --precise 1.0.199The version must still satisfy the requirement in Cargo.toml.
A safe update routine
- Start from a clean working tree, so
git diffshows only the update. - Run
cargo update --dry-runand read the list. - Run
cargo update, orcargo update -p namefor one crate. - Run
cargo buildandcargo test. - Review
git diff Cargo.lock. - Commit
Cargo.lockwith a message that says what was updated.
If the tests fail, run git checkout Cargo.lock to restore the old versions. You can then update packages one at a time to find the one that broke things.
Common errors
- "failed to select a version": Two dependencies need incompatible versions of a third. Run
cargo tree -i nameto see who requires it, then relax or change a requirement. - Nothing updates: The newest release lies outside your requirement. Edit
Cargo.tomlto allow it. - A
0.xcrate does not move: For0.2.3, the requirement allows only0.2.x. A move to0.3counts as a breaking change. - Update fails offline:
cargo updateneeds the registry index. Reconnect, or keep the current lock file.
Exercise
Your Cargo.toml says serde = "1" and Cargo.lock pins 1.0.199. Version 1.0.200 and version 2.0.0 have been released. What does cargo update -p serde do, and how would you move to 2.0.0?
Show answer
It rewrites Cargo.lock to 1.0.200, because that is the newest version allowed by "1". It does not touch Cargo.toml and ignores 2.0.0. To adopt 2.0.0, change the requirement yourself, for example with cargo add serde@2, then fix any compile errors and run the tests.
Exercise
After an update, your tests fail. Name two ways to find and undo the cause.
Show answer
Restore the old lock file with git checkout Cargo.lock, then update crates one at a time with cargo update -p name and test after each. Alternatively, keep the new lock file and pin the suspect crate back with cargo update -p name --precise <old version>.