Sub topic 4
Choosing members: globs, exclude and default-members
This chapter assumes you can read a single-package Cargo.toml, you know the difference between library and binary crates, and you have seen a [workspace] table with a members list. Here you will control exactly which directories become workspace members and which of them Cargo builds when you give no package flag.
What counts as a member
A workspace member is a package that Cargo treats as part of the workspace. Members share the root Cargo.lock and the target directory, and they can use inherited settings. A package becomes a member in one of three ways:
- The root manifest is a package (not a virtual manifest). The root package is always a member.
- A path listed in
members, or matched by a glob inmembers, points to its directory. - A path dependency of another member lives inside the workspace root directory. It is added automatically.
A directory can then be removed from automatic inclusion with exclude.
Listing members explicitly
The simplest form names each directory. Paths are relative to the directory that holds the root Cargo.toml.
[workspace]
resolver = "2"
members = ["crates/textkit", "crates/app"]This is precise and easy to review, but you must edit the list every time you add a crate. A forgotten entry is a common mistake. The new crate builds on its own, yet cargo build --workspace never sees it, unless some member happens to depend on it by path.
Using globs
A glob pattern matches many directories at once. The usual layout puts every crate in one folder.
[workspace]
resolver = "2"
members = ["crates/*"]With this entry, adding crates/parser/ with its own Cargo.toml makes parser a member without touching the root manifest. Cargo supports the standard glob characters: * matches any run of characters within one path segment, ? matches one character, and [abc] matches a character set. You can also mix explicit paths and globs.
[workspace]
resolver = "2"
members = [
"crates/*",
"tools/xtask",
]Keep the globbed folder clean. Put only crate directories under crates/. A stray directory such as crates/notes/ that has no Cargo.toml is a match that is not a package, and depending on your Cargo version this can produce an error or a silent skip. Do not rely on either behavior.
Globs also select by depth. crates/* matches crates/app but not crates/group/inner. If you nest crates, list the second level explicitly or write crates/*/*. Keep in mind that crates/*/* matches every second-level directory, including any that are not crates.
Removing directories with exclude
exclude takes the same kind of paths and removes them from the member set. It covers two situations.
First, a glob matches something you do not want. Suppose crates/experiments is a scratch crate that should stay out of CI:
[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/experiments"]Second, a directory lives inside the workspace tree but must not be pulled in by a path dependency. This is the case that causes the best-known error. Say a vendored crate sits in vendor/legacy/ and has its own Cargo.toml. Cargo walks up the directory tree looking for a workspace root. It finds yours, sees that legacy is not a member, and refuses to build with a message like:
error: current package believes it's in a workspace when it's notAdding exclude = ["vendor/legacy"] tells Cargo the directory is deliberately outside the workspace. The excluded package then builds as a standalone project. It can have its own Cargo.lock and target directory, and it can even define its own [workspace].
The alternative is to make the crate a member. The choice depends on whether you want it to share the lock file and target directory.
| Goal | Setting |
|---|---|
| Include one specific crate | Path in `members` |
| Include every crate in a folder | Glob in `members` |
| Keep a crate in the tree but out of the workspace | Path in `exclude` |
| Pull in a crate only because another member depends on it | Path dependency (no entry needed) |
Path dependencies join automatically
When a member declares textkit = { path = "../textkit" } and that directory is inside the workspace root, textkit becomes a member even if members does not list it. This is convenient, but it makes the member set depend on the dependency graph. If someone later removes the dependency, the crate silently stops being a member. A path dependency that points outside the workspace root does not join the workspace.
Listing every crate through members or a glob keeps the set stable regardless of who depends on whom. Use that approach for any workspace shared by a team.
Choosing what runs by default: default-members
When you run cargo build or cargo test with no package flag, Cargo needs to know which packages to act on. The rules depend on where you run the command:
- Inside a member directory, Cargo acts on that one package.
- In the root of a workspace with a root package, Cargo acts on the root package only.
- In the root of a virtual workspace (no
[package]table), Cargo acts on all members.
default-members overrides the last two cases. It is a list of member paths that serve as the default selection.
[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/experiments"]
default-members = ["crates/app", "crates/textkit"]With this manifest, cargo build at the root builds app and textkit and skips any other member, for example a heavy benchmarks crate. Every path in default-members must also be a member. Cargo reports an error if it is not.
Several flags bypass default-members:
cargo build # default-members only
cargo build --workspace # every member
cargo build -p textkit # only textkit
cargo build --workspace --exclude app # every member except app--workspace is how CI usually reaches crates that developers do not build on every local run.
Inspecting the result
Do not guess which packages Cargo picked. Ask it. cargo metadata prints the member lists as JSON, and --no-deps keeps the output short.
cargo metadata --no-deps --format-version 1 \
| jq -r '.workspace_members[]'
cargo metadata --no-deps --format-version 1 \
| jq -r '.workspace_default_members[]'The first command lists every member. The second lists the default members. Each line is a package ID, which includes the package name and its path. Run both after you change members, exclude or default-members, and compare the output with what you intended.
Common mistakes
- A glob that matches too much.
members = ["*"]can sweep intarget/or other folders. Name the parent folder in the pattern. - Excluding a crate that a member depends on by path. The dependency still resolves because it is a path dependency, but the crate is not a member, so it does not inherit workspace settings. Inherited fields such as
version.workspace = truethen fail. Either make it a member or give it its own full settings. - Putting
default-membersentries that are not members. Cargo rejects the manifest. - Expecting
default-membersto affect--workspace. It does not. - Nested workspaces. A crate may belong to only one workspace. If a subdirectory has its own
[workspace]table, it is a separate workspace root, and the outer one must exclude it.
A complete example
This layout ties the pieces together:
.
├── Cargo.toml
├── crates/
│ ├── app/
│ ├── textkit/
│ ├── benchmarks/
│ └── experiments/
└── vendor/
└── legacy/[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/experiments", "vendor/legacy"]
default-members = ["crates/app", "crates/textkit"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
textkit = { path = "crates/textkit" }The result is as follows:
- Members:
app,textkitandbenchmarks, all matched by the glob. - Not members:
experimentsandlegacy, both excluded. cargo buildat the root: buildsappandtextkit.cargo build --workspace: also buildsbenchmarks.
Because crates/experiments is excluded, it must not use version.workspace = true or any other inheritance. It needs a complete [package] table of its own.
Exercise
A workspace root contains members = ["crates/*"] and default-members = ["crates/app"]. The crates/ folder holds app, textkit and benchmarks. (a) Which packages does cargo test build and test when run at the root? (b) Which does cargo test --workspace use? (c) You now add exclude = ["crates/benchmarks"]. Does cargo test --workspace change, and does default-members need an edit?
Show answer
(a) Only app, because default-members sets the default selection when no package flag is given. (b) All three: app, textkit and benchmarks, since --workspace ignores default-members. (c) Yes, --workspace now covers only app and textkit, because benchmarks is no longer a member. default-members needs no edit, since app is still a member. If benchmarks had been listed in default-members, Cargo would reject the manifest.
Exercise
A directory vendor/legacy/ inside your workspace has its own Cargo.toml. Running cargo build in it fails with "current package believes it's in a workspace when it's not". Give two ways to fix this and say what each one costs.
Show answer
Add "vendor/legacy" to exclude in the root manifest. The crate then builds as a standalone package with its own Cargo.lock and target directory, and it cannot use workspace inheritance. Or make it a member, either by listing it in members or by adding a glob that matches it. It then shares the root Cargo.lock and target directory and can inherit workspace settings, but its dependency versions now take part in the shared resolution.