Sub topic 7
Inheriting version, edition and license from the workspace
This chapter covers the [workspace.package] table and the workspace = true syntax. It assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have a root manifest with a [workspace] table.
The problem: repeated metadata
Every member crate needs version, edition and license. In a workspace with ten crates, you would type the same three lines ten times. When you move to a new edition or change the license, you would edit ten files and could easily miss one.
Cargo solves this with inheritance. You define a value once in the root manifest, and each member opts in to using it.
Defining shared values in the root
Put the shared values in a [workspace.package] table in the root Cargo.toml:
[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" }
[workspace.lints.rust]
unsafe_code = "forbid"This root is a virtual manifest. It has no [package] table, so [workspace.package] is the only place these values live. Defining them there does nothing by itself. A member must ask for each one.
Opting in from a member
A member inherits a field by writing field.workspace = true in its own [package] table. This is shorthand for field = { workspace = true }.
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueThe name is not inherited. Each package needs its own name. The [lints] table at the bottom pulls in the shared lints, which a later chapter covers.
Here is the second member, the binary crate:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = trueThe sources for the two crates show what the inherited setup builds:
// crates/textkit/src/lib.rs
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}// crates/app/src/main.rs
fn main() {
println!("{}", textkit::shout("hello"));
}Running cargo run -p app prints HELLO!. Both crates compile with edition 2021 and report version 0.1.0, though neither manifest states them.
How inheritance resolves
Which fields can be inherited
Not every field can be shared. The [workspace.package] table accepts these keys, among others:
| Field | Typical use |
|---|---|
| `version` | One release number for all crates |
| `edition` | One Rust edition across the workspace |
| `license` | The same license for every crate |
| `authors` | Shared author list |
| `description` | Rarely shared, since crates differ |
| `repository`, `homepage`, `documentation` | One repository for all members |
| `rust-version` | Minimum supported Rust version |
| `publish` | Block publishing of all crates |
Fields that identify a single package, such as name, cannot be inherited.
Common errors
- The member forgets to opt in. Without
edition.workspace = true, the member does not inherit the edition. Cargo falls back to its default for a missing edition, which is 2015. Always check that each member lists the fields you expect. - The root does not define the field. If a member writes
license.workspace = truebut[workspace.package]has nolicense, Cargo reports an error that the field was not found in the workspace. - The member is not in the workspace. A crate outside
memberscannot inherit anything. Cargo reports that the crate is not a workspace member. - Mixing styles. Writing
version = "0.2.0"andversion.workspace = truein one manifest is a duplicate key. Pick one per field.
Exercise
A member's Cargo.toml has edition.workspace = true, but the root manifest has only [workspace] with members and no [workspace.package] table. What happens, and how do you fix it?
Show answer
Cargo fails with an error that the edition field could not be inherited because it is not defined in the workspace. Add a [workspace.package] table to the root manifest with edition = "2021" (or the edition you want), or replace the member's line with a literal edition = "2021".
Exercise
Why does name have to be written in each member instead of inherited?
Show answer
The name identifies one package, so each package needs its own. Inheriting it would give several packages the same name, which a workspace cannot support.