Sub topic 9
Building and testing selected members
This chapter assumes you can run cargo build, cargo test and cargo run on a single package, and that you have read the earlier chapters on the root manifest, members and path dependencies. It uses the workspace from the earlier chapters: a virtual root manifest with two members, the library textkit and the binary app.
A workspace can contain dozens of crates, and you rarely want to build all of them. Cargo has flags that pick which packages a command applies to. This chapter covers those flags, the default selection rules, and the mistakes people make with them.
How Cargo decides which packages to use
Cargo picks packages in this order:
- If you pass
-p/--package, Cargo uses exactly those packages. - If you pass
--workspace, Cargo uses every member. - If you run the command inside a member's directory, Cargo uses that member.
- If you run it at a virtual root, Cargo uses
default-memberswhen the key is set, and every member when it isn't. - If you run it at a root that is also a package (not virtual), Cargo uses only that root package.
--exclude then removes packages from a selection made by --workspace.
The fifth rule surprises people who add a [workspace] table to an existing package. After that change, cargo build at the root builds only the root package, not the members. Use --workspace when you want everything.
Selecting packages with -p
-p takes a package name, which is the name in the member's [package] table. It does not take a directory name. A member in crates/text-kit with name = "textkit" is selected with -p textkit.
Here is the library with a unit test, so there is something to run:
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adds_exclamation_mark() {
assert_eq!(shout("hello"), "HELLO!");
}
}These commands run from the workspace root:
cargo build -p textkit
cargo test -p textkit
cargo run -p app
cargo check -p app -p textkitSome details are worth knowing:
-pcan be repeated, as in the last line above.-p appalso buildsapp's dependencies, sotextkitis compiled too. Selecting a package never skips its path dependencies.-palso accepts a glob such as-p 'text*'. Quote it so your shell doesn't expand it.- If two versions of the same crate name exist in the dependency graph,
-pcan be ambiguous. Use the full spec shown bycargo pkgid, for examplecargo pkgid textkit.
Run cargo test -p textkit and you get three groups of results: unit tests from src/, integration tests from tests/, and doc tests from the examples in your /// comments. Doc tests run only for library targets, so cargo test -p app never runs them.
Selecting everything with --workspace
--workspace selects all members. It replaces the older --all flag, which still works but is deprecated.
cargo build --workspace
cargo test --workspace
cargo check --workspace --all-targets--all-targets is a separate flag and not a selection of packages. It adds tests, examples and benchmarks to the targets built. cargo check --workspace --all-targets is a quick way to find type errors anywhere in the repository, including code that only compiles under #[cfg(test)].
Removing packages with --exclude
--exclude removes one or more packages from a --workspace selection:
cargo build --workspace --exclude app
cargo test --workspace --exclude app --exclude slow-integrationIt is useful when one member needs something the machine doesn't have, such as a system library or a GPU, and you still want to check the rest.
--exclude only works together with --workspace. Cargo rejects cargo build --exclude app on its own, with an error saying that --exclude can only be used together with --workspace. Don't confuse the flag with the exclude key in [workspace]. The key is a manifest setting that stops a directory under a member glob from becoming a member. The flag is a per-command filter.
Working from inside a member
Because of rule 3, you can cd crates/textkit and run cargo test. Cargo walks up the directory tree, finds the workspace root, and runs the command for textkit only. Two things stay the same as at the root:
- Output goes to the workspace's shared
target/directory, not tocrates/textkit/target. - Dependency versions come from the workspace's single
Cargo.lock.
Running from inside a member is the quickest way to work on one crate. Use -p when you are writing scripts or CI steps, because the result then doesn't depend on the current directory.
Changing the default with default-members
For a virtual manifest, cargo build with no flags would normally build every member. If some members are slow or rarely needed, set default-members in [workspace]:
[workspace]
resolver = "2"
members = ["crates/*"]
default-members = ["crates/app", "crates/textkit"]Entries here are paths, not package names. They must also be members. After this change, cargo build at the root builds only those two crates, while cargo build --workspace still builds everything. default-members affects commands such as build, check, test and run, but it has no effect when you pass -p or --workspace.
Running binaries
cargo run needs to know which single binary to start. If the selection contains exactly one binary, Cargo runs it. If it contains several, Cargo stops with an error telling you to use the --bin option. There are two ways to resolve this:
cargo run -p app
cargo run -p app --bin app-cliThe second form is for a package that has more than one binary target. -p picks the package, and --bin picks the target inside it. cargo run --workspace is not allowed, because it would have to start several programs.
To pass arguments to your program, put them after --:
cargo run -p app -- --verbose input.txtFeatures and -p
The --features flag also depends on the selected packages. When you select one package, --features foo enables foo on that package. When you select several, name the package explicitly with the package/feature form:
cargo build -p app --features serde
cargo build --workspace --features textkit/unicodeWhich features are active also depends on which packages are in the build. With resolver 2, Cargo unifies features only among the packages you selected, so cargo build -p textkit may compile textkit's dependencies with fewer features than cargo build --workspace does. A test that passes in one mode can therefore fail in the other. The resolver chapter covers this in detail. For now, remember that CI should test with the same package selection that you intend to ship.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `package ID specification 'x' did not match any packages` | `-p` was given a directory name or a misspelled name | Use the `name` from `[package]`; check with `cargo pkgid` |
| `--exclude can only be used together with --workspace` | `--exclude` used alone | Add `--workspace` |
| `cargo run` asks for `--bin` | Several binaries in the selection | Add `-p` and, if needed, `--bin` |
| `cargo build` at the root builds only one crate | Root is a package, or `default-members` is set | Add `--workspace` |
| A change to `textkit` is not tested | You ran `cargo test -p app`, which tests only `app` | Run `cargo test -p textkit` or `--workspace` |
The last row matters: -p app compiles textkit as a dependency but does not run its tests.
Exercise
A workspace has the members core, cli and gpu. The gpu crate needs a graphics driver that your CI machine lacks. Write the one command that runs the tests of every member except gpu, and explain why cargo test --exclude gpu alone would fail.
Show answer
Run cargo test --workspace --exclude gpu. --exclude only filters a selection made by --workspace, so using it alone makes Cargo report that --exclude can only be used together with --workspace.
Exercise
You run cargo test -p app and everything passes, but you just edited textkit, which app depends on. Does this run prove that textkit's own tests still pass? Name two commands that would.
Show answer
No. -p app compiles textkit as a dependency but runs only app's tests. Either cargo test -p textkit or cargo test --workspace runs textkit's unit, integration and doc tests.
Exercise
The root manifest sets default-members = ["crates/app"]. What do cargo build and cargo build --workspace build when run at the root?
Show answer
cargo build builds only crates/app and its dependencies, such as textkit. cargo build --workspace ignores default-members and builds every member.