In a Cargo virtual manifest, which packages does a bare cargo build at the root compile, and why must that manifest set resolver itself?
answer
- [workspace] without [package]
- all members, or default-members
- -p, --workspace, --exclude
- no edition to infer from
basics
~20 sA virtual manifest has [workspace] and no [package]; a bare cargo build at its root builds every member, or default-members if set. With no edition to infer from, it must set resolver, or Cargo falls back to resolver 1.
solid answer
~40 sA root `Cargo.toml` with `[workspace]` and no `[package]` is a **virtual manifest**. At its root, a command without `-p` or `--workspace` acts on every member, as if `--workspace` were passed, unless `default-members` narrows it; a member listed there still pulls in the members it depends on. A root that *does* have a `[package]` builds only that root package by default. Inside a member directory Cargo picks that member. The resolver is a workspace-wide setting read only from the root, and a non-virtual root infers it from its own `edition`. A virtual manifest has no edition, so without `resolver = "3"` (or `"2"`) Cargo 1.99.1 defaults to `"1"` and warns, even when every member is on edition 2024.
code
toml · 5 lines# Cargo.toml at the repository root: a virtual manifest
[workspace]
members = ["crates/*"]
default-members = ["crates/cli"]
resolver = "3"go deeper
Recall that a virtual manifest is a root Cargo.toml with [workspace] and no [package], and that -p picks one member.
Explain the selection rules (flags, then the current directory, then default-members) and why a virtual manifest has no edition to infer the resolver from.
Spot the silent resolver 1 fallback in an inherited repository and make CI commands explicit with --workspace instead of relying on the job's directory.
Decide whether the repository has a main package worth making the root, and what a bare cargo build should mean for developers.
## Two kinds of workspace root A workspace root is the `Cargo.toml` that carries `[workspace]`. It comes in two shapes: - **Root package:** the same file also has a `[package]`, so the root directory is itself a package (often the main binary) and the members sit beside it. - **Virtual manifest:** the file has `[workspace]` and no `[package]`. It is common when no crate is the obvious "main" one, such as an `api`, `core` and `cli` trio under `crates/`. `members` lists member directories and accepts globs (`"crates/*"`); `exclude` removes paths a glob would match. Path dependencies that sit inside the workspace directory become members automatically. ## Which packages a command selects Cargo decides what to act on from the flags first and the current directory second: 1. `-p SPEC` / `--package SPEC` selects the named packages; the flag can repeat (`-p ledger-core -p ledger-api`). 2. `--workspace` selects every member; `--exclude SPEC` removes one and is only accepted together with `--workspace` (otherwise: `--exclude can only be used together with --workspace`). The old `--all` is a deprecated alias for `--workspace`. 3. With neither flag, inside a member directory, Cargo acts on that member. 4. With neither flag, at the root, it acts on `default-members` if set; otherwise on the root package (root-package workspace) or on every member (virtual manifest). | Bare command run at the root | Root package | Virtual manifest | |---|---|---| | No `default-members` | the root package only | every member | | `default-members = ["crates/cli"]` | the listed members | the listed members | | `--workspace` | every member | every member | Selection names the packages to build; their dependencies are built too. With `default-members = ["crates/cli"]`, a bare `cargo build` in the trio compiled `ledger-core`, `ledger-api` and `ledger-cli`, because the binary depends on both libraries. A related edge: `cargo run` at a virtual root with two binary members fails with `` `cargo run` could not determine which binary to run ``, and the fix is to name the binary with `--bin` or the package with `-p`. ## Why the resolver must be set in a virtual manifest The **resolver version** changes how Cargo resolves dependencies and unifies features. It is a single setting for the whole workspace, read only from the root; a `resolver` key in a member manifest is ignored with a warning. A package normally gets a default from its edition: `"2"` for edition 2021 and `"3"` for edition 2024 (resolver 3 exists since Cargo 1.84). A **root package** therefore infers it from its own `edition`. A virtual manifest has no `[package]` and so no `edition`. Cargo does not look at the members' editions to choose; it falls back to the oldest resolver and says so: ```plaintext warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2024 which implies `resolver = "3"` ``` The fix is one line in the `[workspace]` table: `resolver = "3"`. What resolvers 2 and 3 change in version selection and feature unification is a resolver and features subject; the workspace part is only *where* the setting lives. ## Root package or virtual manifest? Choosing the shape is a small design decision with visible effects: - A **root package** suits a repository with one obvious deliverable, such as a binary with a few helper libraries; developers at the root get that binary from a bare `cargo run`. - A **virtual manifest** suits a set of peers, where no crate should be privileged and the root `Cargo.toml` holds only workspace settings: members, the resolver, `[workspace.package]`, `[workspace.dependencies]` and the profiles. - Converting later is cheap: moving the root package into `crates/` and deleting its `[package]` table changes paths and manifests, not code. The lockfile and target directory stay at the root either way. ## Practical habits - Set `resolver` explicitly in every virtual manifest, matching the members' edition. - Use a glob in `members` with a flat `crates/` directory, as the Cargo book recommends, so adding a crate needs no root edit. - Use `default-members` when a bare `cargo build` should build the deliverable rather than every tool and test-support crate. - In CI, be explicit: `cargo test --workspace` states the intent and does not depend on which directory the job starts in.
- What does default-members change when the root has a [package]?Without it, a bare command at a root-package workspace acts on the root package alone, and other members are reached only with `-p` or `--workspace`. With `default-members` set, the bare command acts on the listed members instead. Flags still win: `-p` and `--workspace` ignore `default-members`.
- Why does adding resolver = "2" to a member's [package] not fix the virtual-manifest warning?The resolver applies to the whole workspace and is read only from the root. Cargo prints `resolver for the non root package will be ignored, specify resolver at the workspace root` and keeps defaulting the virtual workspace to resolver 1. The key has to go in the root's `[workspace]` table.
- A virtual manifest builds nothing unless -p or --workspace is given.
- Cargo picks the resolver from the members' editions automatically.
- Each member can choose its own resolver version.
- --exclude works on its own without --workspace.
- default-members stops Cargo building those members' dependencies.
Assumes Cargo 1.99; resolver "3" exists since Cargo 1.84 and is the edition 2024 default, so a virtual manifest must name it.