tracel-ai/burn · error

Failed to get out dir

Error message

Failed to get out dir

What it means

The build script of burn-tch panics when the OUT_DIR environment variable is missing or empty. OUT_DIR is normally set by Cargo for all build scripts, so this failure indicates an abnormal build invocation (e.g. running build.rs manually or with a stripped environment). The directory is needed to place the libtorch wrapper artifacts.

Source

Thrown at crates/burn-tch/build.rs:212

                    .pic(true)
                    .warnings(false)
                    .flag("-std=c++17")
                    .files(&[cuda_dependency])
                    .compile("tch");
            }
        };
    }
}

fn check_out_dir() -> Option<PathBuf> {
    let out_dir = env_var_rerun("OUT_DIR").ok()?;
    let libtorch_dir = PathBuf::from(out_dir).join("libtorch");
    libtorch_dir.exists().then_some(libtorch_dir)
}

fn main() {
    let system_info = SystemInfo::new();
    let out_dir = env_var_rerun("OUT_DIR").expect("Failed to get out dir");

    let mut gpu_found = false;
    let found_dir = system_info.is_some();
    if let Some(system_info) = &system_info {
        let si_lib = &system_info.libtorch_lib_dir;
        let use_cuda =
            si_lib.join("libtorch_cuda.so").exists() || si_lib.join("torch_cuda.dll").exists();
        let use_hip =
            si_lib.join("libtorch_hip.so").exists() || si_lib.join("torch_hip.dll").exists();

        system_info.make(use_cuda, use_hip);
        gpu_found = use_cuda || use_hip;
    } else {
        SystemInfo::make_cpu();
    }
    let check_file = PathBuf::from(out_dir).join("tch_gpu_check.rs");
    if gpu_found {
        fs::write(check_file, "#[allow(clippy::no_effect)]\n()").unwrap();

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Build through Cargo (`cargo build -p burn-tch`) so OUT_DIR is set automatically
  2. Check that no wrapper or CI script is deleting environment variables before invoking rustc/cargo
  3. If using a custom build runner, set OUT_DIR to a writable directory path before invoking the build script
  4. Verify the CARGO_TARGET_DIR/TARGET layout is writable; recreate with `cargo clean` if corrupted

Example fix

// before (manual invocation)
rustc build.rs && ./build
// after
env OUT_DIR=./out cargo build -p burn-tch
Defensive patterns

Strategy: validation

Validate before calling

if std::env::var("OUT_DIR").is_err() {
    panic!("OUT_DIR not set; build burn-tch via cargo, not directly");
}

Prevention

When it happens

Trigger: Running `cargo build` for burn-tch with OUT_DIR unset in the environment; invoking the build script directly (e.g. `rustc build.rs`) outside of Cargo; using a build harness that clears env vars.

Common situations: Custom CI pipelines that sanitize the environment; sandboxed or restricted build environments; calling build.rs directly to debug; exotic build systems wrapping Cargo without propagating CARGO_* variables.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/7306e5a6a3d458ae. Report an issue: GitHub.