Sub topic 9
Building and testing selected members
This chapter assumes you can write a single-package Cargo.toml, know the difference between library and binary crates, and have already set up the workspace from earlier chapters: a virtual root manifest with crates/textkit (a library) and crates/app (a binary that depends on it).
Default behavior depends on where you are
Cargo decides which packages a command acts on from two inputs: your current directory and the flags you pass.
- In a member's directory, a command acts on that member only.
- In the root of a virtual manifest, a command acts on all members, unless
default-memberslimits it.
So cargo build run inside crates/textkit builds only textkit and its dependencies. The same command at the root builds everything. All members still share one Cargo.lock and one target directory, so work is reused either way.
Selection flags
Three flags control selection from any directory in the workspace.
| Flag | Meaning |
|---|---|
| `-p <name>` or `--package <name>` | Act on the named package. You can repeat it. |
| `--workspace` | Act on every member. |
| `--exclude <name>` | Remove a package from the selection. It requires `--workspace`. |
The flags take the package name from the name field, not the directory name. In our layout they happen to match.
cargo build --workspace
cargo test -p textkit
cargo run -p app
cargo build --workspace --exclude appThe first command builds every member. The second compiles and runs the tests of textkit only. The third builds and runs the app binary. The fourth builds everything except app.
A worked example
With the earlier textkit and app crates, run the app from the root:
cargo run -p appCargo compiles textkit first because app depends on it, then compiles app and prints:
HELLO!Now add a test to crates/textkit/src/lib.rs:
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shout_uppercases_and_adds_bang() {
assert_eq!(shout("hello"), "HELLO!");
}
}Run only that crate's tests:
cargo test -p textkitCargo does not build or test app here. Selecting a package still compiles its dependencies, so any workspace crate it depends on is built too.
Common errors
package ID specification ... did not match any packages: you passed a directory name or a typo. Runcargo metadata --no-deps --format-version 1or check eachnamefield.--excludewithout--workspace: Cargo rejects it. Add--workspace.- Running
cargo runat the root with several binaries: Cargo cannot choose and asks you to pick one. Use-p, or--bin. - Assuming
cargo testat the root covers everything: ifdefault-membersis set, it covers only those. Use--workspacein CI.
Why this matters for features
Selecting one package can change which features get built for shared dependencies. With resolver "2", features are unified only across the packages you are building in that command. A later chapter covers this in detail. For now, remember that cargo build -p app and cargo build --workspace may compile a dependency with different features.
Exercise
In the example workspace, you want to build every member except app and then run the tests of textkit only. Which two commands do this, and why does cargo build --exclude app alone fail?
Show answer
Run cargo build --workspace --exclude app and then cargo test -p textkit. The --exclude flag removes packages from a selection that --workspace creates, so used alone it is rejected by Cargo.
Exercise
You run cargo test at the root of a virtual workspace that sets default-members = ["crates/app"]. Which packages are tested, and what flag tests all of them?
Show answer
Only app is tested, because default-members limits root commands. Add --workspace to test every member.