tracel-ai/burn · error

Could not get cache directory

Error message

Could not get cache directory

What it means

This panic comes from an `expect` on `dirs::cache_dir()` inside `get_cache_dir()` in burn-train's afine weights module. `dirs::cache_dir()` returns `None` when the OS has no user cache directory (e.g. `$XDG_CACHE_HOME`/`$HOME` unset on Linux, or no known cache path on the platform). The function is called by `load_pretrained_weights` before downloading/caching `afine.pth`, so any pretrained-weights load on such a system panics.

Source

Thrown at crates/burn-train/src/metric/vision/afine/weights.rs:50

use burn_store::{ModuleSnapshot, PytorchStore};
use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

use super::calibrators::{AfineAdapter, FrCalibratorWithLimit, NrCalibrator};
use super::metric::Afine;

/// Source URL for `afine.pth` on Hugging Face.
const AFINE_URL: &str =
    "https://huggingface.co/chaofengc/IQA-PyTorch-Weights/resolve/main/afine.pth";

/// Cached filename inside `~/.cache/burn-dataset/afine/`.
const CACHE_FILENAME: &str = "afine.pth";

fn get_cache_dir() -> PathBuf {
    let cache_dir = dirs::cache_dir()
        .expect("Could not get cache directory")
        .join("burn-dataset")
        .join("afine");
    if !cache_dir.exists() {
        create_dir_all(&cache_dir).expect("Failed to create cache directory");
    }
    cache_dir
}

fn download_if_needed(url: &str, cache_path: &PathBuf, message: &str) {
    if !cache_path.exists() {
        let bytes = download_file_as_bytes(url, message);
        let mut file = File::create(cache_path).expect("Failed to create cache file");
        file.write_all(&bytes).expect("Failed to write weights");
    }
}

/// Download `afine.pth` (if not cached) and load all six shards into
/// the matching submodules of an `Afine` previously produced by

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Set the XDG_CACHE_HOME (or HOME) environment variable to a writable directory before running the program, e.g. `XDG_CACHE_HOME=/tmp/.cache ./app`
  2. If running in Docker, add `ENV XDG_CACHE_HOME=/cache` and create/mount that directory
  3. Pre-download `afine.pth` and populate the cache dir manually if the environment cannot be fixed
  4. Patch/fork to fall back to `std::env::temp_dir()` when `dirs::cache_dir()` is None

Example fix

// before (Dockerfile)
CMD ["./trainer"]
// after (Dockerfile)
ENV XDG_CACHE_HOME=/tmp/.cache
RUN mkdir -p /tmp/.cache
CMD ["./trainer"]
Defensive patterns

Strategy: fallback

Validate before calling

// Rust: verify a usable cache dir before loading pretrained weights
fn ensure_cache_dir() -> std::path::PathBuf {
    let dir = dirs::cache_dir()
        .or_else(|| std::env::var("XDG_CACHE_HOME").ok().map(std::path::PathBuf::from))
        .unwrap_or_else(std::env::temp_dir)
        .join("burn-dataset").join("afine");
    std::fs::create_dir_all(&dir).expect("cannot create cache dir");
    dir
}
if dirs::cache_dir().is_none() { eprintln!("warning: no OS cache dir; set XDG_CACHE_HOME"); }

Type guard

fn cache_dir_available() -> bool { dirs::cache_dir().is_some() }

Try / catch

// expect() panics; catch at the process boundary or avoid it
let weights = std::panic::catch_unwind(load_pretrained_weights)
    .map_err(|_| anyhow!("pretrained weight load failed; check XDG_CACHE_HOME/HOME"))?;

Prevention

When it happens

Trigger: Calling `load_pretrained_weights` for the afine metric on a system where `dirs::cache_dir()` returns None — typically running on Linux with `$HOME` (and `$XDG_CACHE_HOME`) unset, e.g. inside a minimal Docker container, a systemd service with a scrubbed environment, or an init system without a user session.

Common situations: Docker/Kubernetes containers running as root with no HOME set; CI runners with minimal env; cron jobs or daemons whose environment lacks HOME/XDG_CACHE_HOME; unusual platforms where dirs has no cache-dir rule.

Related errors


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