skip to content

In a Cargo workspace, a member crate's Cargo.toml sets `[profile.release]` with `lto = true`, yet release builds show no LTO; why, and how do you fix it?

level: seniorimportance: should knowfreq 35%

answer

  1. one manifest is authoritative
  2. Cargo prints a warning, not an error
  3. move the table up
  4. verify the rustc flags

basics

~20 s

Cargo reads profiles only from the workspace root manifest and ignores a member's [profile] tables, printing a warning. Move the table to the root Cargo.toml, or override it through config or CARGO_PROFILE_RELEASE_LTO, then confirm -C lto with cargo build --release -v.

solid answer

~40 s

Cargo applies `[profile]` tables only from the **workspace root** `Cargo.toml`; profiles in member manifests and in dependencies are ignored. It says so on every build: `warning: profiles for the non root package will be ignored, specify profiles at the workspace root:`. The fix is to move `[profile.release]` into the root manifest, which works in a virtual manifest too. Profile values can also come from configuration: a `[profile.release]` table in `.cargo/config.toml`, the environment variable `CARGO_PROFILE_RELEASE_LTO=true`, or `--config 'profile.release.lto=true'`, each overriding the manifest. Either way I verify with `cargo build --release -v`: the final crate's `rustc` line must show `-C lto`. The same rule means a library cannot impose its profile on the binaries that use it.

code

toml · 9 lines
toml
# game/Cargo.toml (a workspace member)
[package]
name = "game"
version = "0.1.0"
edition = "2024"

[profile.release]
lto = true
panic = "abort"

go deeper

for a junior

Recall that in a workspace the [profile] tables belong in the root Cargo.toml, and that Cargo warns when it finds one in a member.

for a middle

Explain why member and dependency profiles are ignored, how per-member differences go into root overrides, and which keys overrides cannot set.

for a senior

Diagnose silent misconfiguration: read the warning in CI logs, confirm flags with cargo build --release -v, and check for config or CARGO_PROFILE_ overrides on build machines.

for a principal

Decide where profile policy lives, the root manifest versus CI configuration, so that shipped artifacts are reproducible and a stray environment variable cannot change them.

## The rule: one manifest owns the profiles A **Cargo workspace** is a set of packages built together under one root `Cargo.toml`, sharing one `Cargo.lock` and one `target` directory. A **profile** is the bundle of compiler settings, such as `opt-level` or `lto`, that Cargo turns into `rustc` flags. The Cargo book is explicit: Cargo only looks at the profile settings in the manifest at the **root of the workspace**, and profile settings defined in dependencies are ignored. So a `[profile.release]` table inside `game/Cargo.toml` has no effect when `game` is a member of a workspace rooted one directory up. It is not an error. Cargo builds normally and prints a warning: ```plaintext warning: profiles for the non root package will be ignored, specify profiles at the workspace root: ``` followed by the paths of the package manifest and the workspace manifest. In CI logs full of compiler output this line is easy to miss, which is how teams ship "LTO builds" that were never LTO builds. ## Why the rule exists - **Settings act on the whole build.** `lto` and `panic` affect the final linked artifact; two members asking for different values in one build would have no consistent meaning. - **A dependency must not dictate your build.** If a library could set `opt-level` or `panic = "abort"` for its consumers, adding a dependency could silently change how your binary behaves. - **One build, one profile.** A command uses a single profile for every crate in the build; per-package differences go through overrides in the root. ## Fixing it 1. **Move the table** to the root `Cargo.toml`. A virtual manifest, one with `[workspace]` and no `[package]`, can hold `[profile]` tables too. 2. **Per-member differences** go in the root as overrides, for example `[profile.release.package.game]` — but overrides cannot carry `lto`, `panic` or `rpath`, which can only be set for the profile as a whole. 3. **Override from configuration** when a value should differ by environment rather than live in the manifest. ## Configuration overrides and their precedence Profiles can also be set outside `Cargo.toml`, and these override the manifest: | source | example | precedence | |---|---|---| | `--config` on the command line | `cargo build --release --config 'profile.release.panic="abort"'` | highest | | environment variable | `CARGO_PROFILE_RELEASE_LTO=true` | above config files | | `.cargo/config.toml` | `[profile.release]` with `lto = true` | above the manifest | | root `Cargo.toml` | `[profile.release]` | the base | Config files and the variables are general Cargo configuration; the point for profiles is that a CI job can turn on LTO for a release artifact without editing the manifest, and that a stray variable on a build machine can change a profile without anyone touching `Cargo.toml`. Package overrides cannot be set through environment variables. ## Verifying what reached rustc Never trust the manifest alone. `cargo build --release -v` prints each `rustc` invocation. In the broken workspace, the `game` crate's line showed `-C opt-level=3` and the implied `-C strip=debuginfo`, but no `-C lto`. With the table moved, or with `CARGO_PROFILE_RELEASE_LTO=true` set, the same line showed `-C lto`. The `Finished` line also names the profile in use, which catches a build that ran under the wrong one. ## A checklist when a profile seems ignored 1. Search the build log for `profiles for the non root package will be ignored`. 2. Find the workspace root: Cargo walks up from the package to the manifest with a `[workspace]` table, and that file's `[profile]` tables are the ones applied. 3. Look for overrides outside the manifest: `.cargo/config.toml` files, `CARGO_PROFILE_*` variables in the CI job, `--config` arguments in the pipeline's build commands. 4. Check the command: `cargo test` uses the `test` profile and `cargo bench` the `bench` profile, so a `[profile.release]` change is invisible to a plain `cargo test`. 5. Read the `rustc` flags with `-v` to confirm the final state. ## Consequences for library authors - A library's `[profile]` tables matter only when someone builds that library as the workspace root, such as its own tests and benchmarks in its repository. - If a library genuinely needs a setting from its users — a minimum `opt-level` for acceptable speed, or a `panic` strategy — it can only document it; the binary's workspace root decides. - Copying a profile into every member "to be safe" adds nothing but warnings.

  • If both `.cargo/config.toml` and the root `Cargo.toml` set `profile.release.lto`, which one wins?
    The config file. Profiles set in config files or environment variables override the manifest's settings, and among those, `--config` on the command line beats `CARGO_PROFILE_*` variables, which beat config files. That is useful for CI, and a reason to check build machines for stray `CARGO_PROFILE_` variables.
  • Can a library crate force `panic = "abort"` on the binaries that depend on it?
    No. Profile settings in a dependency's manifest are ignored; only the workspace root of the build being run decides. A library that relies on a particular strategy can only document the requirement for its users.

  • Each workspace member can carry its own [profile.release] table.
  • Cargo refuses to build when a member manifest contains a profile.
  • A library can force its consumers to build with panic = "abort".
  • Profile keys can only be set in Cargo.toml, never from the environment.
  • A virtual manifest cannot hold [profile] tables.
  • If the manifest says lto = true, the release build used LTO.