Skip to main content

xet_runtime/core/
cache_dir.rs

1use std::env;
2use std::env::current_dir;
3use std::path::PathBuf;
4
5use dirs::home_dir;
6
7use crate::utils::TemplatedPathBuf;
8
9// Calculates the cache root path once.
10pub fn xet_cache_root() -> PathBuf {
11    // if HF_HOME is set use that instead of ~/.cache/huggingface
12    // if HF_XET_CACHE is set use that instead of ~/.cache/huggingface/xet
13    // HF_XET_CACHE takes precedence over HF_HOME
14
15    // If HF_XET_CACHE is set, use that directly.
16    if let Ok(cache) = env::var("HF_XET_CACHE") {
17        TemplatedPathBuf::evaluate(cache)
18
19    // If HF_HOME is set, use the $HF_HOME/xet
20    } else if let Ok(hf_home) = env::var("HF_HOME") {
21        TemplatedPathBuf::evaluate(hf_home).join("xet")
22
23    // If XDG_CACHE_HOME is set, use the $XDG_CACHE_HOME/huggingface/xet, otherwise
24    // use $HOME/.cache/huggingface/xet
25    } else if let Ok(xdg_cache_home) = env::var("XDG_CACHE_HOME") {
26        TemplatedPathBuf::evaluate(xdg_cache_home).join("huggingface").join("xet")
27
28    // Use the same default as huggingface_hub, ~/.cache/huggingface/xet (slightly nonstandard, but won't
29    // mess with it).
30    } else {
31        home_dir()
32            .unwrap_or(current_dir().unwrap_or_else(|_| ".".into()))
33            .join(".cache")
34            .join("huggingface")
35            .join("xet")
36    }
37}