From 25d691eeafc9089a77d4fe1d82f22ca48006cda0 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 8 Jan 2026 15:22:41 -0600 Subject: [PATCH] Document why `parse_xdg_path` requires absolute paths (#17365) Related to #17364 Co-authored-by: Claude --- crates/uv-dirs/src/lib.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/uv-dirs/src/lib.rs b/crates/uv-dirs/src/lib.rs index df3af9057..f55e84292 100644 --- a/crates/uv-dirs/src/lib.rs +++ b/crates/uv-dirs/src/lib.rs @@ -24,11 +24,11 @@ use uv_static::EnvVars; pub fn user_executable_directory(override_variable: Option<&'static str>) -> Option { override_variable .and_then(std::env::var_os) - .and_then(parse_path) - .or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(parse_path)) + .and_then(parse_xdg_path) + .or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(parse_xdg_path)) .or_else(|| { std::env::var_os(EnvVars::XDG_DATA_HOME) - .and_then(parse_path) + .and_then(parse_xdg_path) .map(|path| path.join("../bin")) }) .or_else(|| { @@ -84,7 +84,15 @@ pub fn legacy_user_state_dir() -> Option { } /// Return a [`PathBuf`] if the given [`OsString`] is an absolute path. -fn parse_path(path: OsString) -> Option { +/// +/// Relative paths are considered invalid per the [XDG Base Directory Specification]: +/// +/// > All paths set in these environment variables must be absolute. If an implementation +/// > encounters a relative path in any of these variables it should consider the path invalid +/// > and ignore it. +/// +/// [XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir-spec/latest/ +fn parse_xdg_path(path: OsString) -> Option { let path = PathBuf::from(path); if path.is_absolute() { Some(path) } else { None } }