From 287b5b1eab2b059357b6cf6615c8172334e1ed42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Thu, 26 Mar 2026 20:34:23 +0100 Subject: [PATCH] common : add getpwuid fallback for HF cache when HOME is not set (#21035) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- common/hf-cache.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/hf-cache.cpp b/common/hf-cache.cpp index 80dcab0177..665c9ff066 100644 --- a/common/hf-cache.cpp +++ b/common/hf-cache.cpp @@ -26,6 +26,8 @@ namespace nl = nlohmann; #include #else #define HOME_DIR "HOME" +#include +#include #endif namespace hf_cache { @@ -51,6 +53,13 @@ static fs::path get_cache_directory() { return entry.path.empty() ? base : base / entry.path; } } +#ifndef _WIN32 + const struct passwd * pw = getpwuid(getuid()); + + if (pw->pw_dir && *pw->pw_dir) { + return fs::path(pw->pw_dir) / ".cache" / "huggingface" / "hub"; + } +#endif throw std::runtime_error("Failed to determine HF cache directory"); }();