Sub topic 13
Sharing dependency versions across a workspace
This chapter assumes you can create a project with cargo new, read a Cargo.toml file, and understand version requirements such as "1" and "=1.2.3". It also assumes you know what Cargo.lock records.
The problem with repeated versions
A workspace is a set of packages that share one Cargo.lock and one target directory. Each member package has its own Cargo.toml. If three members all use serde, you end up writing the same version and features three times. Over time the copies drift apart, and one member asks for "1.0.100" while another still asks for "1".
Cargo can keep one declaration in the workspace root and let each member refer to it. This is called workspace dependency inheritance.
Declaring a shared dependency
Put the shared declaration in a [workspace.dependencies] table in the root Cargo.toml. Then, in each member, write workspace = true instead of a version.
# Root Cargo.toml
[workspace]
members = ["app", "util"]
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
# app/Cargo.toml
[dependencies]
serde = { workspace = true }The [workspace.dependencies] table does not add anything to a build by itself. It only defines a named entry that members can opt into. A member that never writes serde = { workspace = true } does not depend on serde.
Adding features in a member
A member can add features on top of the shared declaration. The features are combined, so the member gets the root's features plus its own.
# util/Cargo.toml
[dependencies]
serde = { workspace = true, features = ["rc"] }Here util gets derive from the root and rc from its own entry. A member can also mark the dependency optional with optional = true. A member cannot remove features that the root enables, and it cannot set default-features = false if the root left default features on. Disable default features in the root declaration if any member needs them off.
Other dependency tables
Inheritance works in every dependency table. A member can write workspace = true under [dev-dependencies] and [build-dependencies] as well.
# app/Cargo.toml
[dev-dependencies]
pretty_assertions = { workspace = true }The shared entry still has to be declared once in the root's [workspace.dependencies]. The table it appears in for the member decides whether it is a normal, dev, or build dependency.
Sharing local packages
Path dependencies can be shared too. This is useful when several members depend on a sibling package such as util.
# Root Cargo.toml
[workspace.dependencies]
util = { path = "util", version = "0.1.0" }
# app/Cargo.toml
[dependencies]
util = { workspace = true }Paths in the root table are relative to the root Cargo.toml, not to the member that uses them.
Common errors
- Using
workspace = truewith no root entry. Cargo reports that the dependency was not found inworkspace.dependencies. Add it to the root table. - Adding a version next to
workspace = true. Cargo rejects keys such asversionin an inherited entry. Keep the version only in the root. - Expecting the root table to install the crate. Declaring a dependency there does nothing until a member inherits it.
- Running Cargo from outside the workspace. Inheritance only resolves inside a package that belongs to the workspace.
What stays the same
Inheritance changes where you write the requirement, not how Cargo resolves it. The version requirement follows the same SemVer rules as before. A single Cargo.lock at the workspace root records the resolved versions for all members. Commands such as cargo update -p serde and cargo tree -i serde work from anywhere in the workspace.
Exercise
A workspace root declares tokio = { version = "1", default-features = false, features = ["rt"] } in [workspace.dependencies]. A member app writes tokio = { workspace = true, features = ["macros"] }. Which tokio features does app get, and where would you change the version for every member at once?
Show answer
app gets the rt feature from the root plus macros from its own entry, and default features stay off. To change the version for every member, edit the version in the root's [workspace.dependencies] entry.