{"id":"95b26fe77cafec6f","repo":"rust-lang/rust","slug":"target-was-not-set","errorCode":null,"errorMessage":"TARGET was not set","messagePattern":"TARGET was not set","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_llvm/build.rs","lineNumber":224,"sourceCode":"    //\n    // In that case, there's no guarantee that we can actually run the target,\n    // so the build system works around this by giving us the LLVM_CONFIG for\n    // the host platform. This only really works if the host LLVM and target\n    // LLVM are compiled the same way, but for us that's typically the case.\n    //\n    // We *want* detect this cross compiling situation by asking llvm-config\n    // what its host-target is. If that's not the TARGET, then we're cross\n    // compiling. Unfortunately `llvm-config` seems either be buggy, or we're\n    // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will\n    // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This\n    // tricks us into thinking we're doing a cross build when we aren't, so\n    // havoc ensues.\n    //\n    // In any case, if we're cross compiling, this generally just means that we\n    // can't trust all the output of llvm-config because it might be targeted\n    // for the host rather than the target. As a result a bunch of blocks below\n    // are gated on `if !is_crossed`\n    let target = env::var(\"TARGET\").expect(\"TARGET was not set\");\n    let host = env::var(\"HOST\").expect(\"HOST was not set\");\n    let is_crossed = target != host;\n\n    let components = output(Command::new(&llvm_config).arg(\"--components\"));\n    let mut components = components.split_whitespace().collect::<Vec<_>>();\n    components.retain(|c| OPTIONAL_COMPONENTS.contains(c) || REQUIRED_COMPONENTS.contains(c));\n\n    for component in REQUIRED_COMPONENTS {\n        if !components.contains(component) {\n            panic!(\"require llvm component {component} but wasn't found\");\n        }\n    }\n\n    for component in components.iter() {\n        println!(\"cargo:rustc-cfg=llvm_component=\\\"{component}\\\"\");\n    }\n\n    // Link in our own LLVM shims, compiled with the same flags as LLVM","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_llvm/build.rs#L206-L242","documentation":"Thrown at build.rs:224 via `env::var(\"TARGET\").expect(\"TARGET was not set\")`. Cargo normally exports TARGET (the triple being built for) to every build script as part of its documented environment; rustc_llvm uses TARGET pervasively (cross-compile detection at build.rs:226, platform-specific linking in build.rs:348-547). Its absence means the build script is not running under a normal Cargo invocation, so the contract is broken.","triggerScenarios":"Running the build script through a mechanism that does not set Cargo's standard build-script environment variables — e.g. invoking the compiled build script binary directly, or wrapping cargo in a way that strips env vars. Under normal `cargo build`/`./x.py`, Cargo always sets TARGET.","commonSituations":"Debugging by executing `./target/debug/build/rustc_llvm-<hash>/build-script-build` by hand; sandboxed/containers that filter the environment; broken custom build orchestration layered on top of cargo.","solutions":["Run the build through cargo or bootstrap (`./x.py build`) so Cargo injects TARGET and the other standard build-script variables.","If invoking the build script manually for debugging, export the variables Cargo would: `TARGET=<triple> HOST=<triple> ... ./build-script-build`.","Check that no wrapper script or container is sanitizing the environment passed to the build script."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# TARGET is set by cargo/x.py; running build.rs standalone leaves it unset\n[ -n \"${TARGET:-}\" ] || { echo \"TARGET not set: invoke via './x.py build' or 'cargo build', not rustc directly\"; exit 1; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build rustc_llvm through ./x.py or cargo, which populate TARGET","Never execute the build.rs as a standalone binary","In CI, assert `env | grep -q TARGET` before the build step"],"tags":["rustc-internals","build-script","cargo","environment"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}