Sub topic 2
The root Cargo.toml and the [workspace] table
This chapter assumes you can write a single-package Cargo.toml, tell a library crate from a binary crate, and declare dependencies and features. Here you learn what the [workspace] table does, how Cargo finds the root of a workspace, and which settings belong there.
What makes a manifest a workspace root
A workspace is defined by one file: a Cargo.toml that contains a [workspace] table. Cargo does not need a special command or a marker file. If a manifest has that table, it is the workspace root. Every package the workspace includes is a member.
The root manifest does four jobs:
- It selects which packages are members.
- It sets the
resolverversion used to unify features across members. - It holds shared values that members can inherit: package metadata, dependency versions and lints.
- It holds settings that Cargo only reads at the root, such as build profiles.
Here is a complete root manifest for a workspace with two crates.
[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 file has no [package] table. A root manifest with no [package] is a virtual manifest. The next chapter compares it with a root that is also a package. For now, note that this root builds nothing by itself. It only describes the workspace.
Directory layout
The manifest above expects this layout:
my-workspace/
├── Cargo.toml (the root manifest above)
├── Cargo.lock (created on first build)
├── target/ (created on first build)
└── crates/
├── textkit/
│ ├── Cargo.toml
│ └── src/lib.rs
└── app/
├── Cargo.toml
└── src/main.rsOnly the root contains Cargo.lock and target/. Members do not get their own.
The library member crates/textkit/Cargo.toml inherits from the root:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueIts source, crates/textkit/src/lib.rs:
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}The binary member crates/app/Cargo.toml depends on the library through the root's [workspace.dependencies] table:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = trueIts source, crates/app/src/main.rs:
fn main() {
println!("{}", textkit::shout("hello"));
}Run it from any directory inside the workspace:
cargo build --workspace
cargo test -p textkit
cargo run -p app
cargo build --workspace --exclude appcargo run -p app prints HELLO!. Later chapters explain the selection flags. This chapter only needs you to see that one root manifest drives all four commands.
The keys of the [workspace] table
The table accepts these keys. The members-related ones get a full chapter later.
| Key | Purpose |
|---|---|
| `members` | Paths or globs of the packages in the workspace. |
| `exclude` | Paths that must not be members, even if a glob matches them. |
| `default-members` | Packages that commands such as `cargo build` use when you run them at the root without `-p` or `--workspace`. |
| `resolver` | Feature resolver version: `"1"`, `"2"` or `"3"`. |
| `metadata` | Free-form data for external tools. Cargo ignores it. |
Three sub-tables hold shared values, and each has its own chapter:
[workspace.package]holds shared package metadata.[workspace.dependencies]holds shared dependency declarations.[workspace.lints]holds shared lint settings.
Sub-tables only define values. A member uses a value only when it opts in. It writes version.workspace = true, textkit.workspace = true or [lints] workspace = true. A member that does not opt in is unaffected.
How Cargo finds the root
You can run Cargo inside crates/app without telling it where the root is. Cargo reads the member's manifest, then searches parent directories for a Cargo.toml that has a [workspace] table. It uses the first one it finds.
Two details matter in practice.
First, being below the root directory is not enough. The package must be listed in members, matched by a glob, or be a path dependency of a member that lives inside the root's directory. Cargo adds those path dependencies as members automatically. If none of these holds, you get an error like this:
error: current package believes it's in a workspace when it's not:
current: /home/me/my-workspace/tools/scratch/Cargo.toml
workspace: /home/me/my-workspace/Cargo.toml
this may be fixable by adding `tools/scratch` to the `workspace.members` array of the manifest located at: /home/me/my-workspace/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.Cargo's message lists the fixes. Add the path to members if the package belongs. Add it to exclude if it should stay outside. Or give the package its own empty [workspace] table, which makes it a separate root.
Second, a package can name its root explicitly with package.workspace, for example workspace = "../..". This is rarely needed. It helps when the root is not in a parent directory that Cargo would search, or when you want to skip the search.
To check which root Cargo picked, run this from anywhere in the tree:
cargo locate-project --workspaceIt prints JSON such as {"root":"/home/me/my-workspace/Cargo.toml"}.
Only one root, no nesting
A workspace has exactly one root. If a member's manifest also contains [workspace], Cargo treats that package as the root of a different workspace. It then cannot also be a member of the outer one. Cargo reports an error about multiple workspace roots. When you see that error, search your members for a stray [workspace] table.
Settings that only work at the root
Some settings are read only from the root manifest. If you put them in a member, Cargo ignores them, usually with a warning. The main ones are:
[profile.*]tables, such as[profile.release]withlto = true.[patch]and[replace]tables, which override where dependencies come from.- The
resolverkey, because feature resolution is a property of the whole workspace.
Keep these at the root so the whole workspace builds the same way. Profiles get a full chapter later.
[profile.release]
lto = "thin"
codegen-units = 1Put this block in the root manifest and every member's release build uses it. If you put the same block in crates/app/Cargo.toml, Cargo prints a warning that the profiles are ignored.
Setting the resolver explicitly
The resolver key needs special attention. If the root has a [package], Cargo infers the resolver from that package's edition: edition 2021 uses resolver "2". A virtual manifest has no edition to read, so Cargo falls back to resolver "1" and prints a warning that you should choose one. The members' editions do not change this.
Resolver "2" stops features enabled for build scripts, proc macros or dev-dependencies from leaking into normal builds. The resolver chapter covers feature unification across members in detail.
Common mistakes
- Adding a
[workspace]table to a member by accident. It makes the member a second root. Keep exactly one. - Putting a
[profile]in a member. It is ignored. Move it to the root. - Expecting
[workspace.package]to apply automatically. Members must writefield.workspace = truefor each field they want. - Forgetting
resolverin a virtual manifest. You get resolver 1 and a warning. - Creating a crate under the root directory without listing it. When
membersdoes not match it, Cargo reports the "believes it's in a workspace" error.
Exercises
Exercise
You create crates/cli/ with its own Cargo.toml while the root has members = ["crates/textkit", "crates/app"]. Running cargo build in crates/cli fails with "current package believes it's in a workspace when it's not". Give two ways to fix it, one that makes cli a member and one that keeps it out.
Show answer
To make it a member, add "crates/cli" to members in the root manifest, or change the list to the glob "crates/*". To keep it out, add "crates/cli" to exclude in the root, or put an empty [workspace] table in crates/cli/Cargo.toml so it becomes its own root.
Exercise
Your root manifest is virtual and sets members = ["crates/*"] but no resolver. Every member uses edition = "2021". Which resolver does Cargo use, and what single line fixes it?
Show answer
Cargo uses resolver "1" and prints a warning, because a virtual manifest has no [package] edition to infer from. Add resolver = "2" under [workspace] in the root manifest.
Exercise
You add [profile.release] with lto = true to crates/app/Cargo.toml, but release builds are unchanged. Why, and where does the block go?
Show answer
Profiles are read only from the workspace root manifest. Cargo ignores the block in a member. Move it into the root Cargo.toml.