Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect XDG_DATA_HOME on macOS if set #4769

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ workspace = true
directories = { workspace = true }
tempfile = { workspace = true }
fs-err = { workspace = true }
dirs-sys = { workspace = true }
28 changes: 26 additions & 2 deletions crates/uv-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl StateStore {
pub fn from_settings(state_dir: Option<PathBuf>) -> Result<Self, io::Error> {
if let Some(state_dir) = state_dir {
StateStore::from_path(state_dir)
} else if let Some(project_dirs) = ProjectDirs::from("", "", "uv") {
StateStore::from_path(project_dirs.data_dir())
} else if let Some(data_dir) = data_dir() {
StateStore::from_path(data_dir.join("uv"))
} else {
StateStore::from_path(".uv")
}
Expand All @@ -109,3 +109,27 @@ impl StateBucket {
}
}
}

/// Returns the path to the user data directory.
///
/// This is similar to the `data_dir()` returned by the `dirs` crate, but it respects the
/// `XDG_DATA_HOME` environment variable on both Linux _and_ macOS. If `XDG_DATA_HOME` is not
/// set, it defaults to `$HOME/.local/share` on Linux and `Application Support` on macOS.
///
/// Note we do not use `XDG_STATE_HOME` because this data is portable:
///
/// > The $XDG_STATE_HOME contains state data that should persist between (application) restarts,
/// > but that is not important or portable enough to the user that it should be stored in $XDG_DATA_HOME.
fn data_dir() -> Option<PathBuf> {
let default = ProjectDirs::from("", "", "uv").map(|dirs| dirs.data_dir().to_path_buf());

// On macOS, respect `XDG_DATA_HOME` if present, then fallback to the default
if cfg!(target_os = "macos") {
std::env::var_os("XDG_DATA_HOME")
.and_then(dirs_sys::is_absolute_path)
.or(default)
// On Windows and Linux, use the `ProjectDirs` default behavior.
} else {
default
}
}
Loading