From e6a7c6d165958837362093e22641187a37c396f6 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 22 Oct 2024 11:44:27 -0500 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 | 22 ++++++------------- crates/uv-tool/src/lib.rs | 1 - 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2bab6eebb4b..0c199b7e0532 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -912,15 +912,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" @@ -4548,8 +4539,6 @@ dependencies = [ name = "uv-dirs" version = "0.0.1" dependencies = [ - "directories", - "dirs-sys", "etcetera", "uv-static", ] @@ -5190,7 +5179,7 @@ version = "0.0.1" dependencies = [ "assert_fs", "clap", - "dirs-sys", + "etcetera", "fs-err", "indoc", "schemars", @@ -5562,7 +5551,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 36618fe2b8e2..46615f8769b3 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" } dunce = { version = "1.0.5" } either = { version = "1.13.0" } encoding_rs_io = { version = "0.1.7" } 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 80ce9fd16f62..c493bbc66d23 100644 --- a/crates/uv-settings/src/lib.rs +++ b/crates/uv-settings/src/lib.rs @@ -2,6 +2,7 @@ use std::env; use std::ops::Deref; use std::path::{Path, PathBuf}; +use etcetera::BaseStrategy; use tracing::debug; use uv_fs::Simplified; @@ -176,23 +177,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 631d6c026be8..45eebb4add12 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;