Sub topic 12
Profiles and Settings That Only Apply at the Root
This chapter assumes you can read a single-package Cargo.toml. It also assumes you have a workspace with a virtual manifest and a few member crates, as built in earlier chapters. You should know the difference between cargo build and cargo build --release.
Most settings in a member's Cargo.toml affect only that member. A few settings belong to the whole workspace. Cargo reads them only from the workspace root manifest and ignores or warns about them anywhere else. If you put them in the wrong file, builds don't fail. They quietly behave differently from what you intended.
What "root only" means
When you run a command inside a member directory, Cargo searches upward for a manifest with a [workspace] table. That manifest is the workspace root. Cargo then builds one dependency graph for all members and applies the root's build-wide settings to that graph.
Some settings describe the build as a whole, such as how aggressively to optimize. A member can't change those for itself, because members are compiled into the same build.
The table below lists the main root-only settings.
| Setting | Where it must live | What happens in a member manifest |
|---|---|---|
| `[profile.*]` | Workspace root manifest | Ignored, with a warning |
| `[patch.*]` | Workspace root manifest | Ignored, with a warning |
| `[replace]` | Workspace root manifest | Ignored (deprecated, use `[patch]`) |
| `resolver` in `[workspace]` | Workspace root manifest | Ignored, with a warning |
| `[workspace.*]` tables | Workspace root manifest | Not allowed in a member that is not itself a root |
| `Cargo.lock` | Workspace root directory | Not created per member |
| `target/` directory | Workspace root, by default | Not created per member |
The exact warning wording can vary between Cargo versions. A typical one is profiles for the non root package will be ignored, specify profiles at the workspace root.
Profiles
A profile is a named set of compiler options. Cargo has built-in profiles: dev (used by cargo build), release (used by cargo build --release), test and bench. Both test and bench inherit from the other two.
Common options:
opt-level: optimization from0(none) to3(most)."s"and"z"optimize for size.debug: how much debug information to emit.lto: link-time optimization, which lets the compiler optimize across crate boundaries.codegen-units: how many pieces each crate is split into for parallel compilation. Fewer units can produce faster code but slower builds.panic:"unwind"or"abort".strip: remove symbols or debug info from the final binary.overflow-checks: whether integer overflow panics.
Here is a root manifest that sets some of them for the textkit and app workspace:
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.dependencies]
textkit = { path = "crates/textkit" }
[profile.dev]
opt-level = 0
debug = "line-tables-only"
[profile.release]
opt-level = 3
lto = "thin"
codegen-units = 1
panic = "abort"
strip = "symbols"Setting debug = "line-tables-only" in dev gives backtraces with line numbers but shrinks the debug information, which speeds up linking. The release profile trades build time for runtime speed and a smaller binary.
Per-package overrides
You can change a profile for one package, or for all dependencies, from the same root file:
# Optimize third-party code even in debug builds.
[profile.dev.package."*"]
opt-level = 2
# Keep the workspace's own crates unoptimized for fast rebuilds.
[profile.dev.package.textkit]
opt-level = 0
# Build scripts and proc macros are compiled for the host machine.
[profile.dev.build-override]
opt-level = 1The "*" pattern matches every package that is not a workspace member. A named override beats "*". This is a useful pattern when a dependency such as an image decoder is far too slow at opt-level = 0, and your own crates still need fast rebuilds.
[profile.dev.build-override] applies to build scripts, procedural macros and their dependencies. These are compiled for the machine running Cargo, and they run during your build.
Settings that Cargo overrides
Some profile options are ignored in certain contexts. The panic setting is ignored for the test and bench profiles, because the test harness needs unwinding to catch failing tests. It is also ignored for build scripts and proc macros. If you set panic = "abort" in release and then run cargo test --release, tests still unwind.
Custom profiles
You can define your own profile that inherits from another. A custom profile must name a parent with inherits:
[profile.release-debug]
inherits = "release"
debug = true
strip = "none"
[profile.ci]
inherits = "dev"
debug = false
incremental = falseSelect a custom profile with --profile:
cargo build --profile release-debug
cargo test --profile ciOutput goes to target/<profile-name>/, for example target/release-debug/. The built-in dev profile is the exception: it writes to target/debug/.
release-debug is useful when profiling, because you need optimized code with symbols. ci turns off incremental compilation, which only helps when the same checkout is rebuilt many times and wastes disk and time on a fresh CI runner.
Other root-only settings
`[patch]`
[patch] replaces a dependency from a registry or git repository with another source, for the whole workspace. It is useful when you need to test a local fix to a dependency before it is released.
[patch.crates-io]
serde_json = { path = "../serde_json" }Every member that depends on serde_json, directly or indirectly, gets the patched copy. A [patch] section in a member would affect the shared lock file, so Cargo accepts it only from the root.
A patch must still satisfy the version requirement of whoever depends on it. If the patched crate's version doesn't match, Cargo reports that the patch was not used. Check cargo tree or the warning in the build output.
`resolver`
The resolver key in [workspace] chooses the feature resolver for the whole workspace. A virtual manifest has no edition to infer a default from, so Cargo falls back to resolver "1" and warns if you don't set it. Always set resolver explicitly in a virtual manifest. For edition 2021 use "2". For edition 2024 packages the default is "3".
The edition field in a member crate does not change the resolver for the workspace. Only the root decides.
`[workspace.metadata]`
Tools sometimes read custom configuration from [workspace.metadata]. Cargo ignores its contents. It is a convenient shared place for settings of third-party tools, and it is read from the root only.
Cargo configuration files
The .cargo/config.toml file is different from Cargo.toml, but it follows a related rule. Cargo looks for it starting at the current working directory and walking up. It does not look in the member directory of a package you chose with -p. If you keep .cargo/config.toml in the workspace root, it applies whether you run Cargo from the root or from any member below it. A config file in crates/app/.cargo/ takes effect only when you run Cargo from inside crates/app. Builds then differ depending on where you type the command.
Put shared settings, such as [build] options, [alias] entries and [target.*] linker flags, in one .cargo/config.toml at the root.
Checking that settings take effect
Build with verbose output and look at the flags passed to rustc:
cargo build --release -vEach rustc line should show -C opt-level=3 and -C codegen-units=1 (and -C lto=thin where it applies). To see a per-package override at work, build the dev profile verbosely and compare a workspace crate with a dependency:
cargo build -vThe dependency's rustc line should show opt-level=2. The textkit line should show opt-level=0 or no optimization flag, depending on your Cargo version.
If a profile setting seems to do nothing, run cargo build and read the first lines of output. A "profiles for the non root package will be ignored" warning means you edited the wrong file.
Common mistakes
- Profile in a member manifest. Cargo ignores it and warns. Move it to the root.
- Missing
resolverin a virtual manifest. Cargo uses resolver"1"and features unify in surprising ways across members and build dependencies. - Using
panic = "abort"and expectingcargo testto abort. Tests still use unwinding. - Overriding
ltoper package. Cargo rejects it. Setltoin the profile itself. - Per-member
.cargo/config.tomlfiles. Results change with the directory you run from. - Forgetting that profiles apply to everything. Setting
opt-level = 3in[profile.dev]speeds up every member and dependency but slows every incremental rebuild. Prefer[profile.dev.package."*"]for dependencies only.
Exercise
A teammate adds [profile.release] with lto = "fat" and codegen-units = 1 to crates/app/Cargo.toml. The release binary is no faster, and cargo build --release prints a warning. What went wrong, and how do you fix it so that textkit and app both build with those settings while third-party dependencies in the dev profile get opt-level = 2?
Show answer
Profiles are read only from the workspace root manifest. Cargo ignored the table in crates/app/Cargo.toml and warned that profiles for a non-root package are ignored. Move [profile.release] with lto = "fat" and codegen-units = 1 into the root Cargo.toml, next to [workspace]. Then add [profile.dev.package."*"] with opt-level = 2 to the same root file. The "*" pattern matches non-member packages only, so textkit and app stay at the default dev settings.