Sub topic 7
Inheriting version, edition and license from the workspace
This chapter covers [workspace.package] and the field.workspace = true syntax. It assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have a workspace root like the one built in the earlier chapters. Cargo 1.64 or newer is required for inheritance.
The problem inheritance solves
In a workspace with five crates, every Cargo.toml repeats the same edition, license, repository and often version. Changing the edition or license means editing every file. Missed edits cause drift: one crate on edition 2018, another on 2021, and a license field that disagrees with your LICENSE file.
Inheritance lets you write each value once in the root manifest. Members opt in field by field.
Defining shared values in the root
The root manifest holds a [workspace.package] table. Its keys look like the keys in a normal [package] table.
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["Dana Example <[email protected]>"]
repository = "https://example.com/acme/textkit"
rust-version = "1.74"A virtual manifest has no [package] table, so [workspace.package] is the only place these values live at the root. A root that also is a package can use both tables. They are independent: [workspace.package] does not change the root package's own fields.
Defining a value here does nothing by itself. A member receives it only if it asks for it.
Opting in from a member
A member replaces the literal value with key.workspace = true.
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
[lints]
workspace = trueversion.workspace = true is TOML dotted-key syntax. It is equivalent to this longer form:
[package]
name = "textkit"
version = { workspace = true }Both forms work. The dotted form is the convention because it keeps each field on one line.
Cargo resolves the value when it reads the manifest. If you run cargo metadata or cargo package, you see the resolved value, not the workspace = true marker.
Which fields can be inherited
Only a fixed list of keys is allowed in [workspace.package]:
| Field | Notes |
|---|---|
| `version` | Shared version for all opting-in members |
| `authors` | A list of strings |
| `description` | Rarely shared, since each crate does something different |
| `documentation` | URL string |
| `readme` | Path relative to the workspace root |
| `homepage` | URL string |
| `repository` | URL string |
| `license` | SPDX expression |
| `license-file` | Path relative to the workspace root |
| `keywords` | List of strings |
| `categories` | List of strings |
| `publish` | `true`, `false` or a list of registries |
| `edition` | Language edition |
| `rust-version` | Minimum supported Rust version |
| `exclude` | Glob list of files to leave out of the package |
| `include` | Glob list of files to put in the package |
name is not on the list. Every crate needs its own name. Dependencies are shared through a different table, [workspace.dependencies], covered in the next chapter.
How paths resolve
Fields that hold file paths need care. readme and license-file in [workspace.package] are relative to the workspace root. When a member inherits them, Cargo adjusts the path so it still points to the root file. You do not rewrite it per member.
# root Cargo.toml
[workspace.package]
readme = "README.md"
license-file = "LICENSE"# crates/textkit/Cargo.toml
[package]
name = "textkit"
version.workspace = true
readme.workspace = true
license-file.workspace = trueThe member crate then reports the root README.md as its readme. When you publish, Cargo copies the file into the package so it exists on the registry side.
What the whole workspace looks like
A complete working example
This layout matches the running example for the series.
# Cargo.toml (workspace root, virtual manifest)
[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"# crates/textkit/Cargo.toml
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = true# crates/app/Cargo.toml
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = true// 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"));
}Verify that inheritance took effect:
cargo run -p app
cargo metadata --no-deps --format-version 1cargo run -p app prints HELLO!. In the cargo metadata JSON, both packages show "version":"0.1.0", "edition":"2021" and "license":"MIT OR Apache-2.0".
Mixing inherited and local values
Inheritance is per field. A member can inherit edition and license while keeping its own version and description.
[package]
name = "textkit-cli"
version = "0.3.2"
description = "Command-line front end for textkit"
edition.workspace = true
license.workspace = trueThis is common when crates in one repository release independently. Keep the version shared only if the crates are always released together.
Shared version: benefits and costs
A single workspace version has one clear benefit. One edit bumps every crate, and internal path dependencies stay consistent.
It also has costs:
- Every member that opts in gets the new number even if its code did not change. Published crates get a new release with no changes.
- You cannot release one crate as a patch while another gets a breaking change, unless that crate keeps its own version.
- Tools such as
cargo releasehandle both layouts, but the workflow differs.
Choose a shared version when the crates form one product. Choose separate versions when they have separate audiences or release schedules.
Common errors
Inheriting a field the root does not define
If a member writes license.workspace = true and the root has no license in [workspace.package], Cargo refuses to load the manifest. The error reads like this:
error: failed to parse manifest at `crates/textkit/Cargo.toml`
Caused by:
error inheriting `license` from workspace root manifest's `workspace.package.license`
Caused by:
`workspace.package.license` was not definedFix: add the field to [workspace.package] or remove the inheritance line.
Using `workspace = true` outside a workspace
A crate with edition.workspace = true that is not under a workspace root fails with a message that no workspace was found. This happens when you copy a member out of the repository, or when the root's members list does not include the crate and it is not reachable as a path dependency. Check that the crate's directory matches members or add it explicitly.
Wrong key placement
workspace = true for lints goes under [lints], not [package]. For dependencies it goes on the dependency entry. Writing lints.workspace = true inside [package] is an error.
Setting a literal and inheriting at once
You cannot write both edition = "2021" and edition.workspace = true. TOML rejects the duplicate key. Pick one.
Expecting inheritance to propagate down
Inheritance reads only from the workspace root. A member cannot inherit from another member, and nested workspaces do not chain.
Edge cases
publish: Settingpublish = falsein[workspace.package]and inheriting it is a safe way to keep internal crates off crates.io. A single member can override it with its ownpublish = true.rust-version: A shared MSRV makes Cargo's resolver prefer dependency versions compatible with it, and it makes older toolchains fail early with a clear message. Keep it equal to what your CI tests.versionwith path dependencies: A path dependency that will be published needs its ownversionrequirement. The next chapters on dependency inheritance and publishing show how to combinepathwithversion.includeandexclude: Globs are relative to each member's directory when the member inherits them, not the workspace root. Test withcargo package --listbefore relying on them.
Review
[workspace.package]stores values once at the root.- Members opt in with
field.workspace = true, one field at a time. - Only a fixed set of package fields can be shared.
namecannot. - Missing root values produce a hard error, and a member that omits a field does not inherit it.
Exercise
A member crate's manifest has [package] with name = "parser" and license.workspace = true, but nothing else about edition. The root [workspace.package] defines edition = "2021" and license = "MIT". Which edition does parser use, and what happens if you delete license from the root table?
Show answer
parser uses edition 2015, because inheritance is opt-in per field and the member never wrote edition.workspace = true. If license is deleted from the root [workspace.package], Cargo fails to load the manifest with an error that workspace.package.license was not defined, since the member still asks for it.
Exercise
You have three crates released together and one crate, legacy-bridge, that must stay at version 1.4.0 while the others move to 0.2.0. How do you set version in the root and in each member?
Show answer
Put version = "0.2.0" in [workspace.package]. In the three crates, write version.workspace = true. In legacy-bridge, write version = "1.4.0" directly and do not use inheritance for that field. It can still inherit edition and license.