{"id":"eaa0c189f576ee7b","repo":"rust-lang/rust","slug":"real-library-path-var","errorCode":null,"errorMessage":"REAL_LIBRARY_PATH_VAR","messagePattern":"REAL_LIBRARY_PATH_VAR","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_llvm/build.rs","lineNumber":55,"sourceCode":"    // Force the link mode we want, preferring static by default, but\n    // possibly overridden by `configure --enable-llvm-link-shared`.\n    if tracked_env_var_os(\"LLVM_LINK_SHARED\").is_some() {\n        (\"dylib\", \"--link-shared\")\n    } else {\n        (\"static\", \"--link-static\")\n    }\n}\n\n// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may\n// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM\n// shared library, which means that when our freshly built llvm-config goes to load it's\n// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first\n// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from\n// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't\n// perfect -- we might actually want to see something from Cargo's added library paths -- but\n// for now it works.\nfn restore_library_path() {\n    let key = tracked_env_var_os(\"REAL_LIBRARY_PATH_VAR\").expect(\"REAL_LIBRARY_PATH_VAR\");\n    if let Some(env) = tracked_env_var_os(\"REAL_LIBRARY_PATH\") {\n        unsafe {\n            env::set_var(&key, env);\n        }\n    } else {\n        unsafe {\n            env::remove_var(&key);\n        }\n    }\n}\n\n/// Reads an environment variable and adds it to dependencies.\n/// Supposed to be used for all variables except those set for build scripts by cargo\n/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>\nfn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {\n    println!(\"cargo:rerun-if-env-changed={key}\");\n    env::var_os(key)\n}","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_llvm/build.rs#L37-L73","documentation":"Thrown by restore_library_path (build.rs:54-65) via `.expect(\"REAL_LIBRARY_PATH_VAR\")` when reading the env var of the same name. rustc_llvm's build.rs needs to undo Cargo's injection of the compiler's dylib path into the library search path (comment block at build.rs:46-53); bootstrap sets REAL_LIBRARY_PATH_VAR and REAL_LIBRARY_PATH to the values it observed before Cargo mutated the environment. If the var is absent, the contract with bootstrap has been broken and the build script panics.","triggerScenarios":"Building rustc_llvm outside of bootstrap (./x.py), e.g. by running `cargo build` directly in compiler/rustc_llvm, which does not export REAL_LIBRARY_PATH_VAR. Also reproducible if bootstrap's env-passing is bypassed or a custom build wrapper strips environment variables before invoking the build script.","commonSituations":"Trying to build a single rustc crate by hand with cargo rather than through `./x.py build`; CI environments that sanitize or drop inherited environment; mismatched bootstrap/tool versions where the bootstrap binary does not set this var.","solutions":["Always build rustc_llvm through bootstrap: `./x.py build` (or the equivalent `./x.py build compiler/rustc_llvm`), which sets REAL_LIBRARY_PATH_VAR.","If invoking the build script manually for debugging, set REAL_LIBRARY_PATH_VAR (and usually REAL_LIBRARY_PATH) to the correct dylib search-path variable name for the platform, e.g. LD_LIBRARY_PATH on Linux, DYLD_LIBRARY_PATH on macOS.","Update your bootstrap checkout to match the rustc source version you are building, since the variable contract is version-coupled."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"# Pre-build: reject targets without a known REAL_LIBRARY_PATH_VAR mapping\ncase \"$TARGET\" in\n  *-linux-*|*-apple-darwin*|*-windows-*) : ;;\n  *) echo \"unsupported target for rustc_llvm library path mapping\"; exit 1 ;;\nesac","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build for a target triple enumerated in compiler/rustc_llvm/build.rs:55","If you need a new target, extend the mapping and document the fallback env var","Pin CI to a known-good target triple when building the LLVM-backed rustc"],"tags":["rustc-internals","build-script","llvm","environment","bootstrap"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}