Sub topic 14
Layout conventions and CI commands for workspaces
This chapter assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have seen the workspace root, members and -p flags from earlier chapters. Here we put those pieces together into a layout you can hand to a team and a set of commands a CI server can run.
A conventional layout
Most workspaces use a virtual root manifest and keep every crate in one directory. The example used throughout this tutorial follows that shape:
.
├── Cargo.toml # virtual manifest: [workspace] only
├── Cargo.lock # committed
└── crates/
├── textkit/ # library
│ ├── Cargo.toml
│ └── src/lib.rs
└── app/ # binary
├── Cargo.toml
└── src/main.rsThe glob members = ["crates/*"] means a new crate is picked up as soon as its directory exists. You do not edit the root manifest to add a member. Shared metadata, dependencies and lints live in the root, so each member manifest stays short.
[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"Some conventions are worth following:
- Name the directory after the crate, so
crates/textkitholds the packagetextkit. - Commit
Cargo.lockwhen the workspace contains a binary or you want reproducible CI. One lock file covers every member. - Keep profiles in the root manifest, because member profiles are ignored.
- Put the libraries other crates depend on in
[workspace.dependencies], and writename.workspace = truein members.
Commands for CI
The package selection flags decide what each CI step touches. The commands below build every member, test one member, run the binary, and build everything except the binary.
cargo build --workspace
cargo test -p textkit
cargo run -p app
cargo build --workspace --exclude appA typical pipeline adds format, lint and test steps over the whole workspace:
cargo fmt --all --check
cargo clippy --workspace --all-targets -- -D warnings
cargo test --workspacecargo fmt --all is the formatting counterpart of --workspace. Passing -D warnings turns every warning into an error, so the build fails when a lint from [workspace.lints] fires at the warn level.
Order the cheap checks first. Formatting fails in seconds, so a formatting mistake never waits behind a full compile.
Running only what changed
Large workspaces can make a full run slow. Two habits help:
- Cache the
targetdirectory and the registry between CI runs. All members share onetarget, so a single cache entry covers the whole workspace. - Test only the crates affected by a change. Pass one
-pflag per crate, for examplecargo test -p textkit -p app. Cargo does not work out which crates changed, so your CI script has to decide which names to pass.
Setting default-members in the root manifest changes what a bare cargo build does at the root. It does not change --workspace, so CI should state --workspace explicitly instead of relying on the default.
Checking the result
Run the build command from the root and confirm that the binary prints the expected line:
cargo run -p appThe output is HELLO!, produced by textkit::shout("hello"). If CI runs this same command, it exercises the path dependency, the shared lock file and the inherited settings in one step.
Exercise
Your CI should build every crate except the app binary, and also fail on any Clippy warning in the whole workspace. Which two commands do this?
Show answer
cargo build --workspace --exclude app builds every member except app. cargo clippy --workspace --all-targets -- -D warnings lints every member and turns warnings into errors.
Exercise
A teammate adds a new crate directory crates/cli with its own Cargo.toml. Do they need to edit the root Cargo.toml to include it in cargo build --workspace, and why?
Show answer
No. The glob members = ["crates/*"] matches every directory under crates/, so the new crate becomes a member automatically.