rust-lang/cargo · error

{} --toolchain {}

Error message

{} --toolchain {}

What it means

detect_sysroot_src_path (standard_lib.rs:220-252) locates the standard library source at `<sysroot>/lib/rustlib/src/rust/library/Cargo.lock` (used by `-Zbuild-std`). If that Cargo.lock does not exist, the rust-src component is missing. When `RUSTUP_TOOLCHAIN` is set, Cargo bails suggesting `rustup component add rust-src --toolchain <toolchain>` so the command targets the correct toolchain.

Source

Thrown at src/compiler/standard_lib.rs:245

    let src_path = ws
        .gctx()
        .get_sysroot(&rustc)
        .expect("able to invoke rustc")
        .join("lib")
        .join("rustlib")
        .join("src")
        .join("rust")
        .join("library");
    let lock = src_path.join("Cargo.lock");
    if !lock.exists() {
        let msg = format!(
            "{:?} does not exist, unable to build with the standard \
             library, try:\n        rustup component add rust-src",
            lock
        );
        match ws.gctx().get_env("RUSTUP_TOOLCHAIN") {
            Ok(rustup_toolchain) => {
                anyhow::bail!("{} --toolchain {}", msg, rustup_toolchain);
            }
            Err(_) => {
                anyhow::bail!(msg);
            }
        }
    }
    Ok(src_path)
}

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Install the source: `rustup component add rust-src --toolchain <toolchain>` (use the toolchain shown in the message).
  2. If you don't need to build the std library, drop the `-Zbuild-std*` flags.
  3. Verify with `rustup component list --toolchain <toolchain>` that `rust-src` is installed.

Example fix

# before
cargo +nightly build -Zbuild-std
# after
rustup component add rust-src --toolchain nightly
cargo +nightly build -Zbuild-std
Defensive patterns

Strategy: validation

Validate before calling

# Provision rust-src for the active nightly before -Zbuild-std:
rustup component add rust-src --toolchain nightly || true
cargo +nightly build -Zbuild-std

Prevention

When it happens

Trigger: Invoking a build that needs the standard library source (e.g. `cargo build -Zbuild-std`) without the `rust-src` rustup component installed for the active nightly toolchain.

Common situations: First-time `-Zbuild-std` use; switching toolchains where rust-src was added to one but not another; CI image that installs rustc but not rust-src.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/968852bf01035c96.json. Report an issue: GitHub.