Sub topic 12
Profiles and settings that only apply at the root
This chapter assumes you can read a single-package Cargo.toml, know the dev and release build modes, and have a workspace with a virtual manifest like the one from earlier chapters. It covers which settings Cargo reads only from the workspace root, and what happens when you put them in a member.
Why some settings live only at the root
A workspace shares one target directory and one build plan. Settings that change how code is compiled, such as optimization level, must therefore be the same for everyone who builds the workspace. Cargo reads these settings from the root manifest and ignores them in member manifests.
The main root-only settings are:
[profile.*]tables, which control the compiler options.[patch]and[replace], which redirect dependencies to other sources.resolver, which selects the feature resolver.
Profiles in the root manifest
A profile is a named set of compiler options. Cargo has built-in dev, release, test and bench profiles. You change them in the root Cargo.toml.
[workspace]
resolver = "2"
members = ["crates/*"]
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
lto = "thin"
codegen-units = 1
panic = "abort"Every member built in release mode now uses thin link-time optimization (LTO) and a single codegen unit. That gives smaller and faster binaries but slower builds.
Member profiles are ignored
If crates/app/Cargo.toml contains a [profile.release] table, Cargo prints a warning and skips it:
warning: profiles for the non root package will be ignored, specify profiles at the workspace rootMove the table to the root manifest. This is the most common mistake with root-only settings.
Overriding profiles per package
You can still tune individual packages, but you do it from the root with [profile.<name>.package.<spec>].
# Compile dependencies with optimizations even in dev builds.
[profile.dev.package."*"]
opt-level = 2
# Keep the workspace's own crates fast to compile.
[profile.dev.package.app]
opt-level = 0The spec "*" matches every dependency that is not a workspace member. A name such as app matches that package. This is useful when a heavy dependency, for example an image decoder, makes debug builds too slow to use.
Build scripts and procedural macros run on the host while you compile, so you can tune them separately:
[profile.dev.build-override]
opt-level = 3Custom profiles
You can define your own profile that inherits from another one. A custom profile must name the profile it inherits from.
[profile.profiling]
inherits = "release"
debug = trueSelect it with --profile:
cargo build --profile profilingThe output goes to target/profiling/, next to target/debug/ and target/release/.
Other settings tied to the root
The [patch] table also belongs in the root. It lets you replace a crates.io dependency with a local checkout or a Git branch for the whole workspace.
[patch.crates-io]
serde_json = { path = "../serde_json" }Because there is one Cargo.lock, every member sees the patched version. The resolver key sits in [workspace] for a virtual manifest, as shown in the earlier chapter on features. Per-project Cargo configuration in .cargo/config.toml is also found by searching up from the current directory, so placing it at the workspace root applies it to all members.
Exercise
A teammate adds [profile.release] with lto = true to crates/textkit/Cargo.toml and finds that release builds did not change. Why, and how do they fix it? Name one way to apply a different opt-level to only textkit in dev builds.
Show answer
Cargo reads profiles only from the workspace root, so it ignores the table in a member manifest and prints a warning. Move the [profile.release] table into the root Cargo.toml. To change only textkit in dev builds, add [profile.dev.package.textkit] with the desired opt-level in the root manifest.