Sub topic 8
Centralizing dependency versions with [workspace.dependencies]
This chapter assumes you can read a single-package Cargo.toml, know how [dependencies] and features work, and have already set up a workspace with a virtual manifest and path dependencies between members. It covers the [workspace.dependencies] table: what it does, how members use it, and the errors you are most likely to hit.
The problem it solves
In a workspace with several crates, the same external crate often appears in more than one member. Suppose serde_json = "1.0" is declared in three Cargo.toml files. When you want to change the requirement, you edit three files. If you miss one, that member quietly asks for a different version range.
Cargo has only one Cargo.lock for the whole workspace. It resolves each crate to a single version where the requirements allow, so a mismatch rarely breaks the build. It does make the manifests harder to read and review.
[workspace.dependencies] fixes this. You declare each dependency once in the root manifest, with its version, source and shared options. Members then refer to it by name.
Cargo has supported this since Rust 1.64.
Declaring dependencies in the root
The table lives in the root Cargo.toml, next to [workspace.package]. Each entry uses the same syntax as an ordinary dependency, including version, path, git, features and default-features.
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
textkit = { path = "crates/textkit", version = "0.1.0" }
serde = "1.0"
serde_json = "1.0"
anyhow = "1.0"
[workspace.lints.rust]
unsafe_code = "forbid"Entries in this table do not add dependencies to anything. They are a catalog. A crate such as anyhow is only compiled and locked if at least one member opts in to it. You can list a crate here that nobody uses yet, and the build is unchanged.
The textkit entry has both path and version. The path is relative to the root manifest, not to the member that uses it. The version field is not needed for local builds. It matters when you publish, as the publishing section below explains.
Using a dependency in a member
A member opts in with workspace = true. There are two equivalent spellings.
[dependencies]
serde_json.workspace = true
serde_json = { workspace = true }The first is a dotted key. The second is an inline table, which you need when you add other keys. Here is the library member from the earlier chapters, now using serde. It also adds a feature on top of the shared declaration.
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
serde = { workspace = true, features = ["derive"] }
[lints]
workspace = trueThe binary member uses textkit through the same mechanism, and adds serde_json.
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
serde_json.workspace = true
[lints]
workspace = trueNeither member writes a version number. The root decides the version, and the members only choose which crates they need.
Here is code that uses both. First crates/textkit/src/lib.rs:
use serde::Serialize;
#[derive(Serialize)]
pub struct Shout {
pub original: String,
pub loud: String,
}
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}
pub fn describe(input: &str) -> Shout {
Shout {
original: input.to_string(),
loud: shout(input),
}
}Then crates/app/src/main.rs:
fn main() {
let report = textkit::describe("hello");
let json = serde_json::to_string(&report).expect("Shout always serializes");
println!("{json}");
}Running cargo run -p app prints {"original":"hello","loud":"HELLO!"}.
The app crate never lists serde. It only calls serde_json::to_string, and serde_json depends on serde itself. The Serialize trait implementation comes from textkit's derive.
What a member may and may not change
An inherited dependency is limited on purpose. The version and source come from the root. A member can only add to the declaration, not rewrite it.
| Key in the member | Allowed with `workspace = true` | Effect |
|---|---|---|
| `features = [...]` | Yes | Added to the features declared in the root. The sets are unioned. |
| `optional = true` | Yes | Makes the dependency optional for this member only. |
| `version`, `path`, `git`, `registry` | No | The source is fixed by the root. |
| `default-features` | No | Fixed by the root. Cargo warns if you set it. |
| `package` (rename) | No | Rename it in the root entry instead. |
Two consequences follow.
- Features only grow. If the root says
features = ["derive"], no member can removederive. If you want a feature on for only some members, leave it out of the root and add it in those members. - If the root sets
default-features = false, members cannot turn defaults back on. A member that needs them must enable the specific features. So a crate that may or may not need default behavior is usually better declared with plain defaults in the root.
Dependency kinds and target tables
workspace = true works in every dependency table, not only [dependencies].
[dev-dependencies]
anyhow.workspace = true
[build-dependencies]
serde_json.workspace = true
[target.'cfg(windows)'.dependencies]
serde.workspace = trueThe root entry does not say what kind of dependency it is. The same anyhow entry can be a normal dependency in one member and a dev-dependency in another. The member's table decides the kind.
Path dependencies between members
Listing your own crates in the catalog is the common way to link members. Any member can write textkit.workspace = true without knowing where textkit lives. If you move the crate, you edit one path in the root instead of every ../textkit path.
Publishing and the version field
Cargo removes path from a dependency when it packages a crate for crates.io, because the path means nothing to someone downloading it. A dependency with only a path and no version cannot be published, and cargo publish rejects it.
Putting version = "0.1.0" next to path in the root entry solves this once for all members. Local builds use the path. The published package uses the version.
This also means you must keep the version in the root entry in step with the member's real version. When you bump textkit to 0.2.0, change the workspace.package version if textkit inherits it, and change the version in the textkit catalog entry. Forgetting the second step leaves a stale requirement. Later chapters on publishing return to this.
Common errors
- Name not in the catalog. A member writes
rand.workspace = truebut the root has norandentry. Cargo stops while parsing the manifest and says the dependency was not found inworkspace.dependencies. Add the entry to the root. - Using the key outside a workspace.
workspace = trueonly works when the package is a member of a workspace whose root has the table. Running Cargo in a crate that is not a member, for example a directory listed inexclude, fails with an error about a missing workspace root. - Specifying a version in the member. Writing
serde = { workspace = true, version = "1" }is rejected. Remove the extra key. - Putting the table in a member's manifest.
[workspace.dependencies]is read only from the root manifest. Cargo does not use it from other files. - A member that bypasses the catalog. Nothing forces members to use
workspace = true. A member can still writeserde = "0.9"directly. Cargo then resolves two versions into the lock file and compiles both. Usecargo tree -dto list crates that appear at more than one version, and review manifests for plain version strings.
Updating a dependency
To change a requirement, edit the single root entry, for example serde_json = "1.0.100". Every member that uses it follows. To move to the newest compatible release without changing the requirement, run:
cargo update -p serde_jsonCargo changes Cargo.lock only. The manifest requirement stays as written. Commit both files in the same change, so reviewers see the whole effect.
Summary of the model
- The root
[workspace.dependencies]table is a catalog. It adds nothing to the build by itself. - A member selects an entry with
name.workspace = truein any dependency table. - Members can add
featuresand setoptional, nothing else. - Include
versionnext topathfor any member you plan to publish. - The catalog sets the requirement. Only
Cargo.locksets the exact version used.
Exercise
A root manifest declares serde = { version = "1.0", features = ["derive"] } under [workspace.dependencies]. The member crates/app writes serde = { workspace = true, features = ["rc"] }. Which features of serde does app get? Then: the root also lists rand = "0.8", but no member mentions rand. Is rand compiled, and does it appear in Cargo.lock?
Show answer
app gets both derive and rc, because the member's features are added to the root's. The member cannot remove derive. rand is not compiled and does not appear in Cargo.lock, because entries in [workspace.dependencies] only take effect when a member writes rand.workspace = true.
Exercise
You want to publish textkit and app to crates.io, and app depends on textkit through textkit.workspace = true. The root has textkit = { path = "crates/textkit" }. What goes wrong when you publish app, and what is the fix?
Show answer
Cargo strips path when packaging, so the published app would have no version for textkit, and cargo publish rejects it. Add version = "0.1.0" (matching textkit's real version) to the root entry: textkit = { path = "crates/textkit", version = "0.1.0" }. Publish textkit first, then app.