Sub topic 3
Virtual manifests and root packages
This chapter assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have used cargo build and cargo run. Here you will learn the two ways a workspace root can be written and how to choose between them.
Two shapes of workspace root
A workspace root is the Cargo.toml that contains a [workspace] table. That file can take one of two forms:
- A virtual manifest has
[workspace]but no[package]table. The root directory is only a container. - A root package has both
[package]and[workspace]. The root directory is itself a crate, and it is also the workspace root.
The workspace from the Topic Spine uses a virtual manifest:
[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"There is no [package] here, so nothing at the root compiles. All code lives in crates/textkit and crates/app.
A root package
If you add a [package] table to the same file, the root becomes a crate too:
[package]
name = "textkit-workspace"
version = "0.1.0"
edition = "2021"
[workspace]
members = ["crates/*"]This root package needs a src/lib.rs or src/main.rs. Cargo also adds the root package to the workspace automatically, so you do not list "." in members.
Layout comparison
What changes in practice
The shape affects which packages Cargo selects when you give no flags.
| Behavior | Virtual manifest | Root package |
|---|---|---|
| Code at the root | None | `src/` is a crate |
| `cargo build` at the root | Builds all members | Builds only the root package |
| `cargo test` at the root | Tests all members | Tests only the root package |
| Where metadata lives | `[workspace.package]` | `[package]`, plus optional `[workspace.package]` |
With a virtual manifest, Cargo has no single package to default to, so it acts on every member. With a root package, it acts on the root alone, unless you set default-members (covered in the next chapter) or pass --workspace.
cargo build --workspace
cargo test -p textkitUse --workspace when you want every member from a root-package workspace.
Common errors
- Setting
versionat a virtual root. Puttingversion = "0.1.0"directly under[workspace]is invalid. Shared values go under[workspace.package]. - Expecting
cargo runto work at a virtual root. With several binaries, Cargo cannot choose one. Usecargo run -p app. - Forgetting that the root package builds alone. In a root-package workspace, a plain
cargo testskips the other members, so CI can pass while a member is broken. - Listing the root in
members. It is already included. Listing it is redundant.
Choosing between them
A virtual manifest gives a symmetric layout. Every crate sits in a folder of the same depth, and workspace-wide commands act on all of them. A root package keeps a small project simple, since you can start with one crate and grow into a workspace by adding [workspace] later. Many teams migrate from the second shape to the first once the supporting crates become as important as the main one.
Exercise
You run cargo test at the root of a workspace whose root Cargo.toml has both [package] and [workspace], with members in crates/*. Only the root package's tests run. Name two ways to test every member.
Show answer
Run cargo test --workspace, or set default-members in [workspace] to include every member. Converting the root to a virtual manifest by removing [package] also makes cargo test act on all members.