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 DISTS weights module's `get_cache_dir()`, used by `load_pretrained_weights` to locate `~/.cache/burn-dataset/dists`. It panics when the OS-level user cache directory cannot be determined (no `$XDG_CACHE_HOME`/`$HOME` on Linux, or no platform cache path). Any DISTS pretrained-weight load then aborts before downloading the VGG16 backbone.
Source
Thrown at crates/burn-train/src/metric/vision/dists/weights.rs:22
use burn_store::{ModuleSnapshot, PytorchStore};
use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::PathBuf;
use super::metric::Dists;
/// URL for pretrained DISTS alpha/beta weights from the official repository.
/// Reference: https://github.com/dingkeyan93/DISTS
const DISTS_WEIGHTS_URL: &str =
"https://github.com/dingkeyan93/DISTS/raw/master/DISTS_pytorch/weights.pt";
/// URL for ImageNet pretrained VGG16 backbone weights from PyTorch.
const VGG16_IMAGENET_URL: &str = "https://download.pytorch.org/models/vgg16-397923af.pth";
/// Get the cache directory for DISTS weights.
fn get_cache_dir() -> PathBuf {
let cache_dir = dirs::cache_dir()
.expect("Could not get cache directory")
.join("burn-dataset")
.join("dists");
if !cache_dir.exists() {
create_dir_all(&cache_dir).expect("Failed to create cache directory");
}
cache_dir
}
/// Download file if not cached.
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
- Export `XDG_CACHE_HOME=/some/writable/dir` (or set HOME) before launching the program
- In Docker, add `ENV XDG_CACHE_HOME=/cache` plus a created/mounted directory
- Pre-provision the dists cache so the environment issue is caught early, or run with a fixed env via a wrapper script
- Patch to fall back to `std::env::temp_dir()` when `dirs::cache_dir()` returns None
Example fix
// before (systemd unit) [Service] ExecStart=/usr/local/bin/trainer // after (systemd unit) [Service] Environment=XDG_CACHE_HOME=/var/cache/trainer ExecStart=/usr/local/bin/trainer
Defensive patterns
Strategy: fallback
Validate before calling
fn ensure_dists_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 loading DISTS weights"); } 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!("DISTS weight load panicked; ensure XDG_CACHE_HOME/HOME is set"))?; Prevention
- Set XDG_CACHE_HOME explicitly in every deployment manifest (Docker, K8s, systemd, CI)
- Mount a writable volume at the cache location in containers
- Validate the runtime environment (env vars present) as a startup health check
- Keep the runtime user consistent across runs
When it happens
Trigger: Calling `load_pretrained_weights` for the DISTS metric on a host where `dirs::cache_dir()` is None — Linux process with HOME and XDG_CACHE_HOME unset (minimal Docker image, systemd unit, CI job, cron).
Common situations: Containers running as root with scrubbed env; daemons/services without a user session; stripped-down CI images; platforms where the `dirs` crate has no cache-dir convention.
Related errors
- Could not get cache directory
- Could not get cache directory
- Failed to create cache directory
- Failed to create cache directory
- Failed to create cache directory
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/b008e05e30ca435b.
Report an issue: GitHub.