Sub topic 8
Centralizing dependency versions with [workspace.dependencies]
This chapter assumes you can read a single-package Cargo.toml, know how library and binary crates differ, and have already created a workspace with a virtual root manifest and path dependencies between members. Here you will move dependency declarations into one place so every member uses the same version.
The problem: version drift
Suppose three member crates each depend on serde. If each one writes its own version, they can drift apart. One crate pins 1.0.150, another asks for 1.0, and a third enables different features. Cargo can still resolve this, because there is one Cargo.lock. But the manifests become hard to audit, and upgrades mean editing many files.
[workspace.dependencies] fixes this. You declare a dependency once in the root manifest. Members then opt in by name.
Declaring dependencies at the root
The root table accepts the same forms as a normal [dependencies] entry: a version string, a path, a git source, and so on.
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.dependencies]
textkit = { path = "crates/textkit" }
serde = { version = "1.0", features = ["derive"] }Entries in [workspace.dependencies] do not add anything to any crate by themselves. They are a catalog. A member must still list the dependency to use it.
Using an entry in a member
A member refers to a catalog entry with workspace = true. The short form name.workspace = true is equivalent to name = { workspace = true }.
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = trueHere textkit resolves to the path given at the root. Because crates/app and crates/textkit are both under the workspace, the path is resolved relative to the root manifest, not relative to the member. This is why you write crates/textkit at the root even though app lives in crates/app.
Adding features and optional dependencies
A member can add to the features declared at the root, but it cannot remove them. Features are additive.
[dependencies]
serde = { workspace = true, features = ["rc"] }This member gets derive from the root plus rc. Another member that writes serde.workspace = true gets only derive. You can also mark an inherited dependency optional in the member with optional = true. The optional key is not allowed at the root, because it only makes sense inside a package.
Inheritance works for [dependencies], [dev-dependencies] and [build-dependencies]. The same root entry can be used in each section.
[dev-dependencies]
serde = { workspace = true }Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `dependency.serde was not found in workspace.dependencies` | The member uses `workspace = true`, but the root has no entry with that name. | Add the entry to the root `[workspace.dependencies]`. |
| `workspace = true` with `version = "1"` is rejected | A member cannot override the version of an inherited dependency. | Remove the version, or stop inheriting and declare it locally. |
| Path points to the wrong place | You wrote the path relative to the member. | Write it relative to the root manifest. |
| Dependency not available in code | The root entry exists, but the member never listed it. | Add `name.workspace = true` to the member. |
Check your work
Build and test selected members to confirm resolution:
cargo build --workspace
cargo test -p textkit
cargo run -p appExercise
A root manifest has serde = { version = "1.0", features = ["derive"] } under [workspace.dependencies]. A member writes serde = { workspace = true, features = ["rc"] }. Which features does that member get, and what happens if it writes version = "1.0.100" in the same entry?
Show answer
The member gets both derive and rc, because member features add to the inherited ones. Writing version alongside workspace = true is an error, since a member cannot override the inherited version.
Exercise
Why does textkit = { path = "crates/textkit" } at the root work for the app crate located in crates/app?
Show answer
Paths in [workspace.dependencies] are resolved relative to the root manifest, so crates/textkit points to the right directory no matter which member inherits it.