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
- Install the source: `rustup component add rust-src --toolchain <toolchain>` (use the toolchain shown in the message).
- If you don't need to build the std library, drop the `-Zbuild-std*` flags.
- 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
- Add `rust-src` to your rustup component list in CI setup scripts.
- Pin the nightly toolchain with rust-toolchain.toml so rust-src targets the right toolchain.
- Document -Zbuild-std prerequisites for contributors.
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
- {:?} does not exist, unable to build with the standard libra
- able to invoke rustc
- not implemented
- running the file `{cmd}` requires `-Zscript`
- no such file or subcommand `{cmd}`{is_dir}{suggested_command
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/968852bf01035c96.json.
Report an issue: GitHub.