From 1df8f86c22f5ae6631a42a974568e929e79bf691 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 5 Nov 2024 10:57:55 -0600 Subject: [PATCH] Eliminate dependencies on `directores` and `dirs-sys` (#8048) Migrate all directory related logic to `etcetera`, eliminated two dependecies. --- Cargo.lock | 15 ++----------- Cargo.toml | 2 -- crates/uv-dirs/Cargo.toml | 2 -- crates/uv-dirs/src/lib.rs | 40 +++++++++++++++++++++++++---------- crates/uv-settings/Cargo.toml | 2 +- crates/uv-settings/src/lib.rs | 23 ++++++-------------- crates/uv-tool/src/lib.rs | 1 - 7 files changed, 39 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ef911246d5e..33d0b992f487 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -914,15 +914,6 @@ dependencies = [ "crypto-common", ] -[[package]] -name = "directories" -version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" -dependencies = [ - "dirs-sys", -] - [[package]] name = "dirs" version = "5.0.1" @@ -4599,8 +4590,6 @@ dependencies = [ name = "uv-dirs" version = "0.0.1" dependencies = [ - "directories", - "dirs-sys", "etcetera", "uv-static", ] @@ -5236,7 +5225,7 @@ version = "0.0.1" dependencies = [ "assert_fs", "clap", - "dirs-sys", + "etcetera", "fs-err", "indoc", "schemars", @@ -5632,7 +5621,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.48.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5bd8e9491fb1..ed042f3d49b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,8 +91,6 @@ csv = { version = "1.3.0" } ctrlc = { version = "3.4.5" } dashmap = { version = "6.1.0" } data-encoding = { version = "2.6.0" } -directories = { version = "5.0.1" } -dirs-sys = { version = "0.4.1" } dotenvy = { version = "0.15.7" } dunce = { version = "1.0.5" } either = { version = "1.13.0" } diff --git a/crates/uv-dirs/Cargo.toml b/crates/uv-dirs/Cargo.toml index d32a4a4e3ccf..408c101fd3f2 100644 --- a/crates/uv-dirs/Cargo.toml +++ b/crates/uv-dirs/Cargo.toml @@ -19,6 +19,4 @@ workspace = true [dependencies] uv-static = { workspace = true } -dirs-sys = { workspace = true } -directories = { workspace = true } etcetera = { workspace = true } diff --git a/crates/uv-dirs/src/lib.rs b/crates/uv-dirs/src/lib.rs index 759a4df75dd2..464f95eaed2c 100644 --- a/crates/uv-dirs/src/lib.rs +++ b/crates/uv-dirs/src/lib.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{ffi::OsString, path::PathBuf}; use etcetera::BaseStrategy; @@ -20,19 +20,15 @@ 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(dirs_sys::is_absolute_path) - .or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(dirs_sys::is_absolute_path)) + .and_then(parse_path) + .or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(parse_path)) .or_else(|| { std::env::var_os(EnvVars::XDG_DATA_HOME) - .and_then(dirs_sys::is_absolute_path) + .and_then(parse_path) .map(|path| path.join("../bin")) }) .or_else(|| { - // See https://github.com/dirs-dev/dirs-rs/blob/50b50f31f3363b7656e5e63b3fa1060217cbc844/src/win.rs#L5C58-L5C78 - #[cfg(windows)] - let home_dir = dirs_sys::known_folder_profile(); - #[cfg(not(windows))] - let home_dir = dirs_sys::home_dir(); + let home_dir = etcetera::home_dir().ok(); home_dir.map(|path| path.join(".local").join("bin")) }) } @@ -51,7 +47,16 @@ pub fn user_cache_dir() -> Option { /// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference /// for using the XDG directories on all Unix platforms. pub fn legacy_user_cache_dir() -> Option { - directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.cache_dir().to_path_buf()) + etcetera::base_strategy::choose_native_strategy() + .ok() + .map(|dirs| dirs.cache_dir().join("uv")) + .map(|dir| { + if cfg!(windows) { + dir.join("cache") + } else { + dir + } + }) } /// Returns an appropriate user-level directory for storing application state. @@ -68,5 +73,18 @@ pub fn user_state_dir() -> Option { /// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference /// for using the XDG directories on all Unix platforms. pub fn legacy_user_state_dir() -> Option { - directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.data_dir().to_path_buf()) + etcetera::base_strategy::choose_native_strategy() + .ok() + .map(|dirs| dirs.data_dir().join("uv")) + .map(|dir| if cfg!(windows) { dir.join("data") } else { dir }) +} + +/// Return a [`PathBuf`] if the given [`OsString`] is an absolute path. +fn parse_path(path: OsString) -> Option { + let path = PathBuf::from(path); + if path.is_absolute() { + Some(path) + } else { + None + } } diff --git a/crates/uv-settings/Cargo.toml b/crates/uv-settings/Cargo.toml index 1aca3d09c1f5..afee1de22f05 100644 --- a/crates/uv-settings/Cargo.toml +++ b/crates/uv-settings/Cargo.toml @@ -32,7 +32,7 @@ uv-static = { workspace = true } uv-warnings = { workspace = true } clap = { workspace = true } -dirs-sys = { workspace = true } +etcetera = { workspace = true } fs-err = { workspace = true } schemars = { workspace = true, optional = true } serde = { workspace = true } diff --git a/crates/uv-settings/src/lib.rs b/crates/uv-settings/src/lib.rs index 5534dbaebe42..e937d7b8bb69 100644 --- a/crates/uv-settings/src/lib.rs +++ b/crates/uv-settings/src/lib.rs @@ -2,6 +2,8 @@ use std::env; use std::ops::Deref; use std::path::{Path, PathBuf}; +use etcetera::BaseStrategy; + use uv_fs::Simplified; use uv_static::EnvVars; use uv_warnings::warn_user; @@ -174,23 +176,12 @@ impl From for FilesystemOptions { /// Returns the path to the user configuration directory. /// -/// This is similar to the `config_dir()` returned by the `dirs` crate, but it uses the -/// `XDG_CONFIG_HOME` environment variable on both Linux _and_ macOS, rather than the -/// `Application Support` directory on macOS. +/// On Windows, use, e.g., C:\Users\Alice\AppData\Roaming +/// On Linux and macOS, use `XDG_CONFIG_HOME` or $HOME/.config, e.g., /home/alice/.config. fn user_config_dir() -> Option { - // On Windows, use, e.g., `C:\Users\Alice\AppData\Roaming`. - #[cfg(windows)] - { - dirs_sys::known_folder_roaming_app_data() - } - - // On Linux and macOS, use, e.g., `/home/alice/.config`. - #[cfg(not(windows))] - { - env::var_os(EnvVars::XDG_CONFIG_HOME) - .and_then(dirs_sys::is_absolute_path) - .or_else(|| dirs_sys::home_dir().map(|path| path.join(".config"))) - } + etcetera::choose_base_strategy() + .map(|dirs| dirs.config_dir()) + .ok() } #[cfg(not(windows))] diff --git a/crates/uv-tool/src/lib.rs b/crates/uv-tool/src/lib.rs index 085793e5610c..24be27dc2d91 100644 --- a/crates/uv-tool/src/lib.rs +++ b/crates/uv-tool/src/lib.rs @@ -1,5 +1,4 @@ use core::fmt; - use fs_err as fs; use uv_dirs::user_executable_directory;