rust-lang/cargo · error

{:?} does not exist, unable to build with the standard libra

Error message

{:?} does not exist, unable to build with the standard library, try:
        rustup component add rust-src

What it means

Identical to [44] but emitted when `RUSTUP_TOOLCHAIN` is not set (standard_lib.rs:247-249). The standard library source Cargo.lock is missing and Cargo cannot suggest a specific toolchain, so it bails with the plain message recommending `rustup component add rust-src`.

Source

Thrown at src/compiler/standard_lib.rs:248

        .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 rust-src for the active toolchain: `rustup component add rust-src` (or your distro's equivalent).
  2. If using a non-rustup toolchain, point `__CARGO_TESTS_ONLY_SRC_ROOT` (tests) or obtain the source via the toolchain's documentation.
  3. Remove `-Zbuild-std*` flags if building std is not required.

Example fix

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

Strategy: validation

Validate before calling

# For non-rustup toolchains, verify the source is present:
SYSROOT=$(rustc --print sysroot)
test -f "$SYSROOT/lib/rustlib/src/rust/library/Cargo.lock" || \
  echo "missing rust-src: install via your toolchain provider"

Prevention

When it happens

Trigger: Building with `-Zbuild-std` on a hand-built (non-rustup) toolchain, or a rustup setup where RUSTUP_TOOLCHAIN is unset, without rust-src installed.

Common situations: Custom/dev toolchain without rust-src; rustup where the toolchain was selected via `default` rather than `+toolchain` and the env var is absent; stripped containers.

Related errors


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