Skip to content

Commit a8d09cd

Browse files
zaniebcharliermarsh
authored andcommitted
Eliminate dependencies on directores and dirs-sys (#8048)
Migrate all directory related logic to `etcetera`, eliminated two dependecies.
1 parent 7040701 commit a8d09cd

File tree

7 files changed

+39
-46
lines changed

7 files changed

+39
-46
lines changed

Cargo.lock

Lines changed: 2 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ csv = { version = "1.3.0" }
9191
ctrlc = { version = "3.4.5" }
9292
dashmap = { version = "6.1.0" }
9393
data-encoding = { version = "2.6.0" }
94-
directories = { version = "5.0.1" }
95-
dirs-sys = { version = "0.4.1" }
9694
dotenvy = { version = "0.15.7" }
9795
dunce = { version = "1.0.5" }
9896
either = { version = "1.13.0" }

crates/uv-dirs/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@ workspace = true
1919
[dependencies]
2020
uv-static = { workspace = true }
2121

22-
dirs-sys = { workspace = true }
23-
directories = { workspace = true }
2422
etcetera = { workspace = true }

crates/uv-dirs/src/lib.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::{ffi::OsString, path::PathBuf};
22

33
use etcetera::BaseStrategy;
44

@@ -20,19 +20,15 @@ use uv_static::EnvVars;
2020
pub fn user_executable_directory(override_variable: Option<&'static str>) -> Option<PathBuf> {
2121
override_variable
2222
.and_then(std::env::var_os)
23-
.and_then(dirs_sys::is_absolute_path)
24-
.or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(dirs_sys::is_absolute_path))
23+
.and_then(parse_path)
24+
.or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(parse_path))
2525
.or_else(|| {
2626
std::env::var_os(EnvVars::XDG_DATA_HOME)
27-
.and_then(dirs_sys::is_absolute_path)
27+
.and_then(parse_path)
2828
.map(|path| path.join("../bin"))
2929
})
3030
.or_else(|| {
31-
// See https://github.com/dirs-dev/dirs-rs/blob/50b50f31f3363b7656e5e63b3fa1060217cbc844/src/win.rs#L5C58-L5C78
32-
#[cfg(windows)]
33-
let home_dir = dirs_sys::known_folder_profile();
34-
#[cfg(not(windows))]
35-
let home_dir = dirs_sys::home_dir();
31+
let home_dir = etcetera::home_dir().ok();
3632
home_dir.map(|path| path.join(".local").join("bin"))
3733
})
3834
}
@@ -51,7 +47,16 @@ pub fn user_cache_dir() -> Option<PathBuf> {
5147
/// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference
5248
/// for using the XDG directories on all Unix platforms.
5349
pub fn legacy_user_cache_dir() -> Option<PathBuf> {
54-
directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.cache_dir().to_path_buf())
50+
etcetera::base_strategy::choose_native_strategy()
51+
.ok()
52+
.map(|dirs| dirs.cache_dir().join("uv"))
53+
.map(|dir| {
54+
if cfg!(windows) {
55+
dir.join("cache")
56+
} else {
57+
dir
58+
}
59+
})
5560
}
5661

5762
/// Returns an appropriate user-level directory for storing application state.
@@ -68,5 +73,18 @@ pub fn user_state_dir() -> Option<PathBuf> {
6873
/// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference
6974
/// for using the XDG directories on all Unix platforms.
7075
pub fn legacy_user_state_dir() -> Option<PathBuf> {
71-
directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.data_dir().to_path_buf())
76+
etcetera::base_strategy::choose_native_strategy()
77+
.ok()
78+
.map(|dirs| dirs.data_dir().join("uv"))
79+
.map(|dir| if cfg!(windows) { dir.join("data") } else { dir })
80+
}
81+
82+
/// Return a [`PathBuf`] if the given [`OsString`] is an absolute path.
83+
fn parse_path(path: OsString) -> Option<PathBuf> {
84+
let path = PathBuf::from(path);
85+
if path.is_absolute() {
86+
Some(path)
87+
} else {
88+
None
89+
}
7290
}

crates/uv-settings/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ uv-static = { workspace = true }
3232
uv-warnings = { workspace = true }
3333

3434
clap = { workspace = true }
35-
dirs-sys = { workspace = true }
35+
etcetera = { workspace = true }
3636
fs-err = { workspace = true }
3737
schemars = { workspace = true, optional = true }
3838
serde = { workspace = true }

crates/uv-settings/src/lib.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::env;
22
use std::ops::Deref;
33
use std::path::{Path, PathBuf};
44

5+
use etcetera::BaseStrategy;
6+
57
use uv_fs::Simplified;
68
use uv_static::EnvVars;
79
use uv_warnings::warn_user;
@@ -174,23 +176,12 @@ impl From<Options> for FilesystemOptions {
174176

175177
/// Returns the path to the user configuration directory.
176178
///
177-
/// This is similar to the `config_dir()` returned by the `dirs` crate, but it uses the
178-
/// `XDG_CONFIG_HOME` environment variable on both Linux _and_ macOS, rather than the
179-
/// `Application Support` directory on macOS.
179+
/// On Windows, use, e.g., C:\Users\Alice\AppData\Roaming
180+
/// On Linux and macOS, use `XDG_CONFIG_HOME` or $HOME/.config, e.g., /home/alice/.config.
180181
fn user_config_dir() -> Option<PathBuf> {
181-
// On Windows, use, e.g., `C:\Users\Alice\AppData\Roaming`.
182-
#[cfg(windows)]
183-
{
184-
dirs_sys::known_folder_roaming_app_data()
185-
}
186-
187-
// On Linux and macOS, use, e.g., `/home/alice/.config`.
188-
#[cfg(not(windows))]
189-
{
190-
env::var_os(EnvVars::XDG_CONFIG_HOME)
191-
.and_then(dirs_sys::is_absolute_path)
192-
.or_else(|| dirs_sys::home_dir().map(|path| path.join(".config")))
193-
}
182+
etcetera::choose_base_strategy()
183+
.map(|dirs| dirs.config_dir())
184+
.ok()
194185
}
195186

196187
#[cfg(not(windows))]

crates/uv-tool/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::fmt;
2-
32
use fs_err as fs;
43

54
use uv_dirs::user_executable_directory;

0 commit comments

Comments
 (0)