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 run cargo build and cargo run. It covers the two shapes a workspace root can take, how Cargo treats each, and the errors you will meet when you mix them up.
Two kinds of root manifest
Every workspace has one root manifest. It is the Cargo.toml that contains the [workspace] table. That file can take one of two forms:
- A virtual manifest has a
[workspace]table and no[package]table. The root directory is not a crate. It only describes the workspace. - A root package has both
[workspace]and[package]. The root directory is itself a crate and also the head of the workspace.
The shared-metadata example from the plan 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 there is no src/main.rs or src/lib.rs at the root. Every crate lives under crates/.
A root package adds a [package] table to the same file:
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
textkit = { path = "crates/textkit" }
[workspace]
members = ["crates/*"]Here the root directory holds a crate named app with its own src/main.rs. The crates under crates/ are additional members.
What a virtual manifest changes
A virtual manifest has no package, so some things that work in a single crate do not work at the root.
Commands act on the whole workspace
In a root package, cargo build run at the root builds only the root package. This is because Cargo uses the package in the current directory as the default selection.
In a virtual manifest, there is no current package. Cargo falls back to building every member. So cargo build at the root of a virtual workspace behaves like cargo build --workspace. You can change this with default-members, covered in a later chapter.
The table summarizes the difference:
| Command at the root | Root package | Virtual manifest |
|---|---|---|
| `cargo build` | Builds the root package only | Builds all members, or `default-members` if set |
| `cargo test` | Tests the root package only | Tests all members, or `default-members` if set |
| `cargo run` | Runs the root binary | Fails if more than one binary exists, unless you pass `-p` |
| `cargo build --workspace` | Builds all members, including the root package | Builds all members |
`cargo run` needs a target
With a root package that has src/main.rs, cargo run runs it. With a virtual manifest, Cargo has to pick a binary from the members. If exactly one member has a binary, cargo run may work. When the workspace has several binaries, it reports an error and asks you to choose. Use -p:
cargo run -p appBeing explicit also keeps scripts working when someone adds a second binary later.
Package-level tables are not allowed
Tables that belong to a package cannot appear in a virtual manifest. If you add [dependencies] to a file that has no [package], Cargo rejects it. Put dependencies in the member crates. If you want to share versions, use [workspace.dependencies], which is allowed because it only declares candidates that members opt into.
A common mistake is to write this:
[workspace]
members = ["crates/*"]
[dependencies]
serde = "1"Cargo will report that the manifest has no package or project section, or that dependencies is not allowed there. The fix is to move serde into the member that uses it, or into [workspace.dependencies] and then inherit it with serde.workspace = true.
Metadata in a virtual manifest
Since there is no [package], the root has nowhere to put a version or an edition for itself. That is why [workspace.package] exists. It holds values that members can inherit. A member writes version.workspace = true and Cargo copies the value in.
Only some keys can be inherited. These include version, edition, license, authors, description, repository, homepage, documentation, readme, keywords, categories, rust-version, publish, exclude and include. Keys that identify a single crate, such as name, cannot be shared.
A member that inherits looks like this:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueIf a member writes version.workspace = true but the root has no [workspace.package] with a version, Cargo stops with an error that the workspace does not define it. The check works the same way in both root shapes.
What a root package changes
A root package is convenient when one crate is clearly the main product and the others are helpers. The layout is familiar:
app/
Cargo.toml (package "app" + [workspace])
src/main.rs
crates/
textkit/
Cargo.toml
src/lib.rsThings to know:
cargo build,cargo testandcargo runat the root act onapponly. Helper crates are built becauseappdepends on them, but their tests are not run unless you pass--workspaceor-p.- A root package is also a member. You do not list it in
members. Cargo adds it automatically. - Root package settings and workspace settings share one file, which can grow long.
- The root crate can inherit from its own
[workspace.package]. Both tables live in one file, so you can writeversion.workspace = truethere.
The second point catches people out. Running cargo test at the root can look like it passes while helper crates have failing tests. Use cargo test --workspace in CI.
How Cargo finds the workspace root
When you run a command in a member directory, Cargo needs to find the root. It walks up the directory tree, looking at each parent Cargo.toml for a [workspace] table. The first one found that lists the current crate as a member (directly, or through a glob) becomes the root.
You can also point from the member to the root with package.workspace:
[package]
name = "tool"
version = "0.1.0"
edition = "2021"
workspace = "../.."This is rarely needed. It helps when a crate sits in a location Cargo cannot reach by walking upward, or when you want to be explicit.
If Cargo finds a root that does not include the crate, you get an error saying the current package believes it is in a workspace when it is not. Two fixes work: add the crate to members, or add it to exclude. A third option is an empty [workspace] table in the crate's own manifest, which makes it its own workspace root and stops the upward search.
Seeing the structure
The diagram shows how Cargo resolves a command run in a member directory.
One detail matters for the diagram: when you run a command from inside a member directory, that member is the default selection in either shape. The rules above apply when you run from the workspace root.
Choosing between the two
| Question | Virtual manifest | Root package |
|---|---|---|
| Is there one clear main crate? | Works, but the main crate lives in a subdirectory | Natural fit |
| Do you have several equal crates? | Natural fit | The root crate looks special |
| Do you want root commands to touch everything? | Default behavior | Needs `--workspace` |
| Is the root file long? | Short, workspace only | Mixed package and workspace content |
| Will the layout grow? | Scales well | Often migrated to virtual later |
Many teams choose a virtual manifest for anything with more than two or three crates. Moving from a root package to a virtual manifest later means moving src/ into a member directory and updating paths. That is mechanical, but it changes paths in CI scripts and documentation.
Common errors
- "this virtual manifest specifies a
[dependencies]section, which is not allowed". You added package-level tables to a root with no[package]. Move them into a member or into[workspace.dependencies]. - "current package believes it's in a workspace when it's not". A parent manifest has
[workspace]but does not list this crate. Add it tomembers, add it toexclude, or give the crate its own empty[workspace]. - "multiple workspace roots found in the same workspace". Two manifests both contain
[workspace]and both claim the same crate. Keep one[workspace]per tree. error: manifest path ... is a virtual manifest, but this command requires running against an actual package. Some commands, such ascargo install --path .orcargo publishwithout-p, need a package. Run them against a member directory or pass-p.cargo runsays it could not determine which binary to run. Several members define binaries. Use-pand, if a package has several binaries,--bin.
Checking your layout
You can confirm what Cargo sees with cargo metadata, which prints JSON. These commands show the root and the members:
cargo metadata --no-deps --format-version 1 | grep -o '"workspace_root":"[^"]*"'
cargo metadata --no-deps --format-version 1 | grep -o '"workspace_members":\[[^]]*\]'The first prints the directory Cargo treats as the root. The second lists the package IDs of all members. If a crate you expected is missing, check members, exclude, and whether a nearer [workspace] table is capturing it.
Exercise
You have a root Cargo.toml with [workspace] and members = ["crates/*"], and no [package]. A teammate adds [dependencies] with serde = "1" to this file. What happens, and what are two correct ways to give the textkit member access to serde?
Show answer
Cargo rejects the manifest, because a virtual manifest cannot have a [dependencies] table. Two correct ways: (1) add serde = "1" to [dependencies] in crates/textkit/Cargo.toml directly, or (2) declare serde = "1" under [workspace.dependencies] in the root and write serde.workspace = true in the [dependencies] of crates/textkit/Cargo.toml.
Exercise
A workspace has a root package named app and a member textkit with failing unit tests. You run cargo test at the root and it passes. Why, and which command shows the failure?
Show answer
With a root package, cargo test at the root selects only the root package by default, so textkit tests are not run. cargo test --workspace runs every member and shows the failure. cargo test -p textkit runs only that crate.