Sub topic 14
Layout conventions and CI commands for workspaces
This chapter assumes you can already create a virtual manifest with several members. It also assumes you know -p, --workspace and --exclude, and that you understand [workspace.package], [workspace.dependencies] and [workspace.lints]. Here you will put those pieces together into a layout that stays readable as it grows, and a set of CI commands that catch problems early.
A layout that scales
Cargo does not require any directory structure. It only needs the root Cargo.toml to list members. Most teams still converge on the same shape, because it makes the workspace easy to scan and easy to glob.
my-project/
├── Cargo.toml # virtual manifest: [workspace] only
├── Cargo.lock # committed
├── rust-toolchain.toml # optional: pins the compiler for everyone
├── crates/
│ ├── textkit/
│ │ ├── Cargo.toml
│ │ └── src/lib.rs
│ └── app/
│ ├── Cargo.toml
│ └── src/main.rs
└── .github/workflows/ci.ymlThe conventions behind this tree:
- Use a virtual manifest. The root has no
[package], so nothing in the root directory competes with the members. Every crate sits at the same level, andcargo buildin the root builds all of them. - Put members under
crates/and glob them. Withmembers = ["crates/*"], adding a crate means creating a directory. You never edit the root list. - Name the directory after the package.
crates/textkitholds the packagetextkit. Then-p textkit, the directory name and the dependency key all match. - Commit
Cargo.lock. CI and your teammates then build the same dependency versions you tested. This matters most when the workspace contains a binary, but it is good practice for libraries too. - Keep shared settings in the root. Put package metadata, dependency versions, lints and profiles in the root manifest. Members should contain only what is specific to them.
The table below compares the two root styles.
| Root style | `cargo build` in the root builds | Typical use |
|---|---|---|
| Virtual manifest | All members, unless `default-members` is set | Projects with several peer crates |
| Root package plus `[workspace]` | Only the root package, unless `default-members` is set | A main crate with helper crates beside it |
Both styles are valid. The point is that the rule differs, so CI should not rely on a bare cargo build.
Make CI explicit
A bare cargo build or cargo test depends on where you run it and on whether default-members is set. CI should say what it means. Always pass --workspace for whole-project checks.
cargo fmt --all -- --check
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo build --workspace --locked
cargo test --workspace --locked
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --lockedEach command does a specific job:
cargo fmt --all -- --checkchecks formatting in every member and fails instead of rewriting files.--all-targetsmakes Clippy andcargo buildalso cover tests, examples and benchmarks. Without it, Clippy skips code that only compiles undercfg(test).--lockedfails ifCargo.lockwould need to change. A dependency edit that someone forgot to commit shows up as a CI failure instead of a silent difference.-D warningsafter--turns Clippy warnings into errors. It is passed to Clippy, not to Cargo.RUSTDOCFLAGS="-D warnings"does the same for broken documentation links.--no-depsskips building documentation for third-party crates.
Because -p, --workspace and --exclude all work with build, test, clippy and doc, you can reuse the same selection rules everywhere. --exclude only works together with --workspace. If one member needs a system library that CI lacks, skip it with this command:
cargo test --workspace --exclude app --lockedLints as a CI contract
Workspace lints are only enforced in members that opt in. A new member without this table is not covered:
[lints]
workspace = trueWhen you add a crate, copy this table from an existing member. A cheap check for it in CI is to count the member manifests that lack the line:
grep -L 'workspace = true' crates/*/Cargo.tomlgrep -L prints files that do not match. This pattern is loose, because it also matches textkit.workspace = true style lines in other tables. Treat it as a quick smoke test, not a proof. A stricter option is to search for the exact [lints] table with a short script.
You can set Clippy lints in the root too:
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
dbg_macro = "warn"
todo = "warn"With -D warnings in CI, the warn level becomes a failure there. On a developer machine it stays a warning, so local work is not blocked.
Features can differ between commands
The shared build graph means feature resolution depends on which packages you select. With resolver = "2", features are unified only across the packages in the current build. Dev-dependency features are not applied unless you build something that needs them, such as tests. This leads to a known trap. cargo build -p textkit can succeed while cargo build --workspace fails, or the reverse, because a sibling crate enables a feature that changes what textkit compiles.
Suppose textkit has an optional serde feature, and app enables it. Building the whole workspace turns it on for textkit. Building textkit alone leaves it off. If textkit code under #[cfg(feature = "serde")] has a bug, only the whole-workspace build finds it.
To cover this in CI:
- Run the whole-workspace commands above. They test the combination users of your binary get.
- Also test each library crate by itself, for example
cargo test -p textkit --locked, so a library never relies on features it does not enable itself. - For crates with many features, use the third-party tool
cargo-hack, for examplecargo hack check --each-feature --workspace, to try each feature on its own. It is not part of Cargo and must be installed first.
Put the cheap checks first. A formatting failure then stops the pipeline in seconds, before any compilation starts.
A complete workflow
This GitHub Actions file runs the checks above. The cache step stores the shared target directory and the downloaded registry files, keyed on Cargo.lock. Because the workspace has one lock file and one target directory, a single cache entry covers all members.
name: ci
on:
push:
branches: [main]
pull_request:
env:
CARGO_TERM_COLOR: always
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --all -- --check
- run: cargo clippy --workspace --all-targets --locked -- -D warnings
- run: cargo build --workspace --locked
- run: cargo test --workspace --locked
- run: cargo test -p textkit --locked
- run: cargo doc --workspace --no-deps --locked
env:
RUSTDOCFLAGS: "-D warnings"If the workspace grows large, split the work into parallel jobs, for example one for lints and one for tests, each with its own cache. Splitting costs extra setup time per job, so do it only once a single job becomes the slowest part of your pipeline.
Checking before you publish
Publishing is not part of every CI run, but you can check it on every pull request. For each crate you intend to publish, package it and verify that it builds from the packaged contents:
cargo publish -p textkit --dry-run --lockedA dry run builds the packaged copy, not your working tree. It catches a missing version next to a path dependency, files excluded by .gitignore, and missing metadata such as license. Crates that depend on other unpublished workspace crates can only be fully verified once their dependencies are on the registry. Publish in dependency order: textkit first, then app.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| CI passes but a new crate has no lints applied | The member lacks `[lints] workspace = true` | Add the table, and check for it in review |
| `cargo build` builds fewer crates than expected | `default-members` is set, or the root is a package | Use `--workspace` in CI |
| `--exclude` is rejected | It was used without `--workspace` | Add `--workspace` |
| Build passes in CI, fails for one crate locally | Feature unification differs per selection | Test that crate with `-p` |
| `--locked` fails in CI | `Cargo.lock` was not committed after a dependency change | Run `cargo build` locally and commit the lock file |
| Tests pass but doctests never run | `--all-targets` skips doctests | Add `cargo test --workspace --doc` |
Exercise
A workspace has a virtual manifest with default-members = ["crates/app"] and three member crates. A developer reports that cargo test passes locally, but CI, which runs cargo test --workspace --locked, fails in a library crate. Give two possible reasons related to this chapter, and say how to reproduce each locally.
Show answer
First, plain cargo test only covers default-members, so the library crate was never tested locally. Reproduce with cargo test --workspace. Second, building the whole workspace unifies features across packages, so a sibling crate may enable a feature on the library that changes what it compiles. Reproduce by running cargo test --workspace and then cargo test -p <library> and comparing the results. A third possibility is that --locked fails because Cargo.lock is out of date. Fix that by running a build locally and committing the lock file.
Exercise
Write the single command that runs Clippy on every member except app, including tests and examples, and turns all warnings into errors.
Show answer
cargo clippy --workspace --exclude app --all-targets -- -D warnings