{"record":{"id":"79bb8403052de1d8","repo":"tracel-ai/burn","slug":"unable-to-get-target-os","errorCode":null,"errorMessage":"Unable to get TARGET_OS","messagePattern":"Unable to get TARGET_OS","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tch/build.rs","lineNumber":47,"sourceCode":"\n#[allow(dead_code)]\n#[derive(Debug, Clone)]\nstruct SystemInfo {\n    os: Os,\n    cxx11_abi: String,\n    libtorch_include_dirs: Vec<PathBuf>,\n    libtorch_lib_dir: PathBuf,\n}\n\nfn env_var_rerun(name: &str) -> Result<String, env::VarError> {\n    println!(\"cargo:rerun-if-env-changed={name}\");\n    env::var(name)\n}\n\nimpl SystemInfo {\n    fn new() -> Option<Self> {\n        let os = match env::var(\"CARGO_CFG_TARGET_OS\")\n            .expect(\"Unable to get TARGET_OS\")\n            .as_str()\n        {\n            \"linux\" => Os::Linux,\n            \"windows\" => Os::Windows,\n            \"macos\" => Os::Macos,\n            os => panic!(\"unsupported TARGET_OS '{os}'\"),\n        };\n        // Locate the currently active Python binary, similar to:\n        // https://github.com/PyO3/maturin/blob/243b8ec91d07113f97a6fe74d9b2dcb88086e0eb/src/target.rs#L547\n        let python_interpreter = match os {\n            Os::Windows => PathBuf::from(\"python.exe\"),\n            Os::Linux | Os::Macos => {\n                if env::var_os(\"VIRTUAL_ENV\").is_some() {\n                    PathBuf::from(\"python\")\n                } else {\n                    PathBuf::from(\"python3\")\n                }\n            }","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tch/build.rs#L29-L65","documentation":"In burn-tch's `build.rs`, `SystemInfo::new` reads the `CARGO_CFG_TARGET_OS` environment variable, which Cargo always sets for build scripts, and `.expect(\"Unable to get TARGET_OS\")` panics if it is missing. This variable is only defined when the script runs under Cargo's build-script environment; running the build script directly, or via tooling that strips the environment, triggers the panic. It is a build-time (compile-of-dependency) failure, not a runtime error in your application.","triggerScenarios":"Compiling the `burn-tch` crate when `CARGO_CFG_TARGET_OS` is absent from the build script's environment — e.g. invoking `build.rs` manually with rustc, running inside a sandboxed/wrapped build harness that sanitizes env vars, misconfigured `cargo` custom wrappers, or CI tools that execute build scripts outside a normal `cargo build`.","commonSituations":"Custom Bazel/Buck or Docker build pipelines that call rustc directly; Cargo plugins that don't forward Cargo-provided env vars; broken toolchain installs; users patching or re-running build scripts by hand to debug libtorch linking.","solutions":["Build normally through `cargo build`/`cargo check` so Cargo sets `CARGO_CFG_TARGET_OS`","If using a custom build system, define the variable yourself: `CARGO_CFG_TARGET_OS=linux cargo build ...` (values: linux/windows/macos)","Check that no wrapper script, `env -i`, or CI sandbox is stripping the environment before cargo runs","Update cargo and burn-tch to current versions in case of a toolchain regression"],"exampleFix":"// before (custom wrapper)\nrustc --crate-name burn_tch build.rs  # panics: no CARGO_CFG_TARGET_OS\n// after\nCARGO_CFG_TARGET_OS=linux cargo build -p burn-tch  # or just: cargo build","handlingStrategy":"fallback","validationCode":"// shell check before building\n[ -n \"$CARGO_CFG_TARGET_OS\" ] && echo ok || { echo 'not running under cargo'; exit 1; }","typeGuard":null,"tryCatchPattern":"// This is a build-script panic; guard in the invoking script:\nset -e\n: \"${CARGO_CFG_TARGET_OS:?CARGO_CFG_TARGET_OS not set — run via cargo build}\"","preventionTips":["Always build the crate through cargo, not direct rustc invocations of build.rs","In custom build systems, export CARGO_CFG_TARGET_OS to match the target triple","Audit CI/sandbox wrappers for environment sanitization that drops CARGO_* vars","Keep cargo up to date"],"tags":["build-script","env-var","cargo","compile-time"],"backgroundTag":"missing-env-var","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}