Sub topic 4
Choosing members: globs, exclude and default-members
This chapter assumes you can read a single-package Cargo.toml and that you know what a virtual manifest is: a root Cargo.toml with a [workspace] table and no [package] table. Here you decide which directories become workspace members, and which members Cargo acts on when you give it no package flags.
The members list
members is an array of paths relative to the workspace root. Each path must point to a directory that contains a Cargo.toml.
[workspace]
resolver = "2"
members = ["crates/textkit", "crates/app"]Listing every crate by hand gets tedious. Cargo accepts glob patterns, so one entry can cover a whole directory:
[workspace]
resolver = "2"
members = ["crates/*"]With this entry, a new crate under crates/ joins the workspace without any edit to the root manifest. The supported wildcards are *, ? and [abc]-style character sets. The pattern crates/* matches only direct children of crates/.
Path dependencies add members too. If a member depends on a crate by path, and that crate lives inside the workspace root's directory, Cargo treats it as a member automatically.
Excluding directories
exclude removes paths from the workspace. It is most useful when a glob matches more than you want.
[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/legacy-tool"]Without exclude, a crate that sits under the workspace root but is not a member causes an error. Cargo says the package believes it is in a workspace when it is not. An excluded crate is left alone. It builds on its own, with its own Cargo.lock and target directory.
Setting default-members
When you run cargo build in a workspace, Cargo has to decide which members to build. The rules are:
- In a virtual manifest, the default is all members.
- With a root package, the default is only that root package.
- If
default-membersis set, it overrides both rules.
[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/legacy-tool"]
default-members = ["crates/app"]Every entry in default-members must be a path to a member. Now a bare cargo build at the root builds only app and its dependencies. This helps when some members are slow to build or need extra tools, such as a crate with code generation or benchmarks.
Package flags override the default:
cargo build # default members only
cargo build --workspace # every member
cargo build -p textkit # one named memberThe following diagram shows how Cargo picks the set of packages when it runs from the root.
Running from inside a member
If your working directory is inside a member, Cargo selects that member and ignores default-members. Running cargo test in crates/textkit tests only textkit, though the shared Cargo.lock and target directory still come from the workspace root.
Common mistakes
- A glob such as
crates/*also picks up crates you meant to keep out. Add them toexclude. - A
default-membersentry that is not a member is an error. Check its spelling and that the path matches a member. - A crate placed under the workspace root, but not matched by
members, triggers the "believes it's in a workspace" error. Add it tomembersorexclude.
Exercise
A workspace has members = ["crates/*"] and default-members = ["crates/app"]. It also contains crates/textkit and crates/bench. You run cargo test at the root. Which crates are tested, and what do you type to test all three crates?
Show answer
Only app is tested, because default-members overrides the virtual manifest default of all members. To test all three crates, run cargo test --workspace.