tracel-ai/burn · error

Could not get cache directory

Error message

Could not get cache directory

What it means

This is the `expect` on `dirs::cache_dir()` in the FID weights module's `get_cache_dir()`, used by `load_pretrained_weights` to resolve `~/.cache/burn-dataset/fid` for the Inception weights (`pt-inception-2015-12-05-6726825d.pth`). It panics when the platform has no determinable user cache directory. Any FID computation that needs the pretrained Inception network then fails at startup.

Source

Thrown at crates/burn-train/src/metric/vision/fid/weights.rs:13

use burn_std::network::downloader::download_file_as_bytes;
use burn_store::{ModuleSnapshot, PytorchStore};
use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::PathBuf;

use super::metric::Fid;

const INCEPTION_WEIGHTS_URL: &str = "https://github.com/mseitzer/pytorch-fid/releases/download/fid_weights/pt-inception-2015-12-05-6726825d.pth";

fn get_cache_dir() -> PathBuf {
    let cache_dir = dirs::cache_dir()
        .expect("Could not get cache directory")
        .join("burn-dataset")
        .join("fid");

    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");
    }
}

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Set `XDG_CACHE_HOME` (or HOME) to a writable directory in the process environment
  2. In Docker, add `ENV XDG_CACHE_HOME=/cache` and create or mount that path
  3. Pre-download `pt-inception-2015-12-05-6726825d.pth` into the expected cache dir once the env is fixed
  4. Change code to fall back to a temp dir when `dirs::cache_dir()` is None

Example fix

// before (shell)
./fid-eval   # panic: Could not get cache directory
// after (shell)
export XDG_CACHE_HOME=/tmp/.cache
mkdir -p /tmp/.cache
./fid-eval
Defensive patterns

Strategy: fallback

Validate before calling

fn ensure_fid_cache_base() -> std::path::PathBuf {
    let base = dirs::cache_dir()
        .or_else(|| std::env::var("XDG_CACHE_HOME").ok().map(std::path::PathBuf::from))
        .unwrap_or_else(std::env::temp_dir);
    std::fs::create_dir_all(&base).expect("cache base unusable");
    base
}
if dirs::cache_dir().is_none() { eprintln!("set XDG_CACHE_HOME before FID evaluation"); }

Type guard

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

Try / catch

let weights = std::panic::catch_unwind(load_pretrained_weights)
    .map_err(|_| anyhow!("FID weight load panicked; ensure XDG_CACHE_HOME/HOME is set"))?;

Prevention

When it happens

Trigger: Calling FID `load_pretrained_weights` on a system where `dirs::cache_dir()` returns None — Linux with HOME and XDG_CACHE_HOME unset (bare Docker image, systemd service, CI runner, cron job).

Common situations: Root containers with no HOME; services started by init systems without user env; minimal CI images; headless servers with scrubbed environments.

Related errors


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