Sub topic 13
Sharing dependency versions across a workspace
This chapter assumes you can create a project with cargo new, read a [dependencies] table, and understand SemVer requirements such as "1" and "0.2.3". It also assumes you know what Cargo.lock records. Earlier chapters covered these. Here you apply them to a project made of several packages.
Why workspaces need shared versions
A workspace is a set of packages that Cargo builds together. The packages share one Cargo.lock file and one target directory. A typical workspace has an application package and one or more library packages.
Without any sharing, each package writes its own version requirement for each dependency. Suppose app asks for serde = "1.0.100" and util asks for serde = "1". Cargo can still resolve both to a single serde version, because the lock file is shared and the requirements overlap. The problem is not the build. The problem is the manifests, which drift apart over time:
- One package gets a bump to a newer minimum version and the others do not.
- Feature lists differ from package to package.
- A new package copies an old requirement from another manifest.
- Updating a dependency means editing several files.
Workspace dependency inheritance fixes this. You declare each shared dependency once in the root manifest, and members refer to that declaration.
The workspace root
The root Cargo.toml has a [workspace] table. This one lists two members:
# Root Cargo.toml
[workspace]
members = ["app", "util"]
resolver = "2"
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }The members list holds paths to the member packages. The [workspace.dependencies] table is a catalog of dependencies that members may use. Declaring a dependency in this table does not add it to any package. Nothing is compiled because of this table alone. A member has to opt in.
The resolver = "2" line is needed here because a root manifest with only a [workspace] table (a virtual manifest) has no edition to pick the resolver from. Without it, Cargo warns and falls back to the older resolver.
Inheriting in a member
A member opts in with workspace = true:
# app/Cargo.toml
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { workspace = true }
util = { path = "../util" }The member's serde entry now has the version and features from the root: version = "1" and features = ["derive"]. Cargo reads the root entry and merges in whatever the member adds.
The same works in util:
# util/Cargo.toml
[package]
name = "util"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { workspace = true }Both packages now depend on the same requirement. To change the requirement, edit one line in the root.
What a member can add
A member cannot change the version or the source of an inherited dependency. It can add to it in two ways.
- It can add features. Features from the member are combined with the features in the root entry.
- It can mark the dependency optional with
optional = true.
# app/Cargo.toml
[dependencies]
serde = { workspace = true, features = ["rc"] }Here app gets derive from the root plus rc from the member. Other members that do not list rc only get derive from their own declaration. When Cargo builds the whole workspace, though, it unifies features for a single package, so serde is compiled once with every feature that any member requested. Do not rely on a feature being absent just because one member did not ask for it.
A member cannot turn features off. If the root declares default-features = true (the default), a member's default-features = false is ignored for that entry, and newer Cargo versions warn about it. To let members choose, declare the root entry with default-features = false and let each member enable what it needs:
# Root Cargo.toml
[workspace.dependencies]
tokio = { version = "1", default-features = false }
# app/Cargo.toml
[dependencies]
tokio = { workspace = true, features = ["rt", "macros"] }
# util/Cargo.toml
[dependencies]
tokio = { workspace = true, features = ["time"] }This pattern keeps the root entry minimal and moves the decision about features to the place that uses them.
Dependency kinds
Inheritance works in every dependency table. A member can inherit into [dev-dependencies] and [build-dependencies], and into target-specific tables:
# Root Cargo.toml
[workspace.dependencies]
pretty_assertions = "1"
cc = "1"
# util/Cargo.toml
[dev-dependencies]
pretty_assertions = { workspace = true }
[build-dependencies]
cc = { workspace = true }The root table has no notion of kind. The same entry can be a normal dependency in one member and a dev-dependency in another. The kind is chosen by the member's table.
Workspace dependencies for local packages
You can also list the workspace's own packages in [workspace.dependencies], with a path and a version:
# Root Cargo.toml
[workspace.dependencies]
util = { path = "util", version = "0.1.0" }
# app/Cargo.toml
[dependencies]
util = { workspace = true }Paths in the root entry are relative to the root manifest, not to the member. This is why the entry says path = "util" while a member-level declaration said path = "../util". The version is needed if you plan to publish the package, because crates.io rejects a dependency that has only a path.
Sharing package metadata too
Cargo also lets members inherit fields from [workspace.package], such as version, edition, license, and rust-version:
# Root Cargo.toml
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT"
# app/Cargo.toml
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = trueThis is separate from dependency inheritance but follows the same idea. Both need Cargo 1.64 or newer.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `error: failed to parse manifest ... dependency.serde was not found in workspace.dependencies` | The member says `workspace = true` but the root has no entry with that name. | Add the entry to the root `[workspace.dependencies]`. |
| `error: ... cannot specify both workspace = true and version` | The member repeats `version`, `path` or `git` next to `workspace = true`. | Remove it from the member. Only `features`, `optional` and `workspace` are allowed. |
| The package does not seem to be in the workspace | The package directory is missing from `members`, or it sits outside the root directory. | Add its path to `members`, or use a glob such as `members = ["crates/*"]`. |
| A feature you did not enable is active | Another member enabled it, and features are unified within the workspace build. | Run `cargo tree -e features` to find who enabled it. |
| Warning about an unused `default-features` | The member set `default-features = false` on an entry the root declared with defaults on. | Set `default-features = false` in the root entry. |
Checking the result
After editing, confirm that every member resolves to the same version:
cargo tree -i serde
cargo tree -e features -p app
cargo metadata --format-version 1 --no-deps > /dev/nullThe first command shows every package that depends on serde. All of them should appear under one serde version. The second shows the enabled features for app. The third fails if any manifest is invalid, so it works as a quick check.
To update the shared dependency within its allowed range, run cargo update -p serde from anywhere in the workspace. There is one lock file, so one command updates it for all members. To raise the requirement itself, change the line in the root manifest, then run cargo update -p serde.
When not to use it
Workspace inheritance adds an indirection. To see what a package depends on, you must read two files. For a workspace with two small members and one shared crate, that cost can outweigh the benefit. It pays off when you have several members, when versions change often, or when you want a single place to review and approve dependency changes.
Inheritance also does not make members publish together. Each package is published to crates.io on its own, and the published manifest has the inherited values filled in. Consumers of your crate never see workspace = true.
Exercise
A workspace root declares tokio = { version = "1", default-features = false } in [workspace.dependencies]. The member app declares tokio = { workspace = true, features = ["rt"] }, and the member util declares tokio = { workspace = true, features = ["time"] }. Which tokio features are compiled when you run cargo build at the workspace root, and why? What command would show you?
Show answer
Both rt and time are compiled, and no default features. Each member adds its features to the root entry, and Cargo unifies features across the workspace build so tokio is built once with the union. cargo tree -e features (or cargo tree -e features -i tokio) shows which package enabled each feature.
Exercise
You add rand = { workspace = true, version = "0.8" } to a member manifest and Cargo reports an error. What is wrong and how do you fix it?
Show answer
A member that uses workspace = true may not set version, because the version comes from the root. Remove version = "0.8" from the member and put it in the root [workspace.dependencies] entry for rand.