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 error 32 but `RUSTUP_TOOLCHAIN` is NOT set in the environment, so the suggestion omits the `--toolchain` argument. The `library/Cargo.lock` for rust-src is missing and you are not running under a rustup-managed toolchain override.

Source

Thrown at src/compiler/standard_lib.rs:246

        .sysroot
        .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 target_data.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 42eee92bc9)

Solutions

  1. Install rust-src through your toolchain manager (rustup: `rustup component add rust-src`; Nix: add `rustPlatform.rustLibSrc`).
  2. Ensure `RUSTUP_TOOLCHAIN` is exported if you are actually using rustup so error 32's better hint appears.
  3. Point `RUSTC_LINK_SRC`/the build-std source path to a vendored rust source tree that includes `Cargo.lock`.
  4. Switch to a rustup-managed toolchain for `-Zbuild-std` workflows.

Example fix

// before (Nix shell)
rustc = pkgs.rustc   # no source
cargo +nightly build -Zbuild-std

// after
shell = pkgs.mkShell { buildInputs = [ pkgs.rustc pkgs.rustPlatform.rustLibSrc ]; }
Defensive patterns

Strategy: validation

Validate before calling

fn library_lock_exists(src_root: &Path) -> bool {
    src_root.join("library").join("Cargo.lock").exists()
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Building with `-Zbuild-std` on a standalone/custom toolchain installation (e.g. a distro-packaged rustc, a Nix derivation, or a hand-built toolchain) where rust-src was not installed or its `Cargo.lock` is absent.

Common situations: Linux distro rustc packages; dev environments without rustup; CI images that install rustc via apt/dnf but not the source; Nix shells with `pkgs.rustc` but no `rustPlatform.rustLibSrc`.

Related errors


AI-assisted analysis of rust-lang/cargo@42eee92bc9 (2026-08-11). Data as JSON: /api/errors/21b6792f2c017f42. Report an issue: GitHub.