rust-lang/rust · error
TOOLCHAIN_NAME
Error message
TOOLCHAIN_NAME
What it means
This panic fires in the `cargo-clif` wrapper script for the Cranelift codegen backend. The script uses `option_env!("TOOLCHAIN_NAME")` to read the toolchain name at compile time, then `.expect("TOOLCHAIN_NAME")` at runtime. `TOOLCHAIN_NAME` is a build-time environment variable that must be set when the wrapper script itself is compiled (by the cg_clif build system). If it's not set, the `option_env!` yields `None` and the script panics on launch.
Source
Thrown at compiler/rustc_codegen_cranelift/scripts/cargo-clif.rs:37
}
if let Some(name) = option_env!("BUILTIN_BACKEND") {
rustflags.push(format!("-Zcodegen-backend={name}"));
} else {
let dylib = sysroot.join("lib").join(
env::consts::DLL_PREFIX.to_string()
+ "rustc_codegen_cranelift"
+ env::consts::DLL_SUFFIX,
);
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
}
rustflags.push("--sysroot".to_owned());
rustflags.push(sysroot.to_str().unwrap().to_owned());
let cargo = if let Some(cargo) = option_env!("CARGO") {
cargo
} else {
// Ensure that the right toolchain is used
env::set_var("RUSTUP_TOOLCHAIN", option_env!("TOOLCHAIN_NAME").expect("TOOLCHAIN_NAME"));
"cargo"
};
let mut args = env::args().skip(1).collect::<Vec<_>>();
if args.get(0).map(|arg| &**arg) == Some("clif") {
// Avoid infinite recursion when invoking `cargo-clif` as cargo subcommand using
// `cargo clif`.
args.remove(0);
}
let args: Vec<_> = match args.get(0).map(|arg| &**arg) {
Some("jit") => {
rustflags.push("-Cprefer-dynamic".to_owned());
args.remove(0);
IntoIterator::into_iter(["rustc".to_string()])
.chain(args)
.chain([
"--".to_string(),View on GitHub (pinned to 7088e4b63a)
Solutions
- Use the official build system (`./y.rs prepare && ./y.rs build`) which sets `TOOLCHAIN_NAME` during compilation.
- Set `TOOLCHAIN_NAME` manually before building: `TOOLCHAIN_NAME=stage2 cargo build --manifest-path scripts/Cargo.toml`.
- If `CARGO` env var is set, the script bypasses the toolchain check — set `CARGO` to your cargo binary as a workaround.
- Follow the cg_clif README's installation instructions exactly; do not copy binaries built without the build system.
Example fix
# before (manual build → TOOLCHAIN_NAME not set → panic) cargo build --manifest-path compiler/rustc_codegen_cranelift/scripts/Cargo.toml ./target/debug/cargo-clif build # after (use the official build system) cd compiler/rustc_codegen_cranelift ./y.rs prepare && ./y.rs build # then use the installed cargo-clif
Defensive patterns
Strategy: validation
Validate before calling
// Check before building that TOOLCHAIN_NAME will be available.
// In the build script or CI:
// test -n "$TOOLCHAIN_NAME" || { echo "TOOLCHAIN_NAME not set"; exit 1; }
// Or check at Rust compile time (in the script's build.rs):
// fn main() {
// if option_env!("TOOLCHAIN_NAME").is_none() && option_env!("CARGO").is_none() {
// panic!("Set TOOLCHAIN_NAME or CARGO before building cargo-clif");
// }
// } Prevention
- Always use y.rs build system for cg_clif — it sets TOOLCHAIN_NAME automatically.
- Never manually cargo build the scripts without setting TOOLCHAIN_NAME.
- Set CARGO env var as a fallback to bypass the toolchain lookup path.
- Document the required env vars in your build pipeline.
When it happens
Trigger: Running `cargo-clif` (the Cranelift backend's cargo wrapper) when the binary was compiled without the `TOOLCHAIN_NAME` environment variable set. This happens when the wrapper is built outside the official cg_clif build system (e.g., copied manually, built with a bare `cargo build`, or installed via a method that bypasses `build_system/`).
Common situations: Installing rustc_codegen_cranelift by manually running `cargo build` in the scripts directory instead of using the project's `build_system/` / `y.rs` build tool. Also happens if the build system's environment setup is incomplete or the toolchain name variable was removed in a refactor.
Related errors
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/aa36c463641636b4.
Report an issue: GitHub.