Skip to content

Commit 3bc4cc0

Browse files
committed
Eliminate dependencies on directores and dirs-sys (#8048)
Migrate all directory related logic to `etcetera`, eliminated two dependecies.
1 parent 6444122 commit 3bc4cc0

File tree

7 files changed

+38
-46
lines changed

7 files changed

+38
-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
@@ -92,8 +92,6 @@ csv = { version = "1.3.0" }
9292
ctrlc = { version = "3.4.5" }
9393
dashmap = { version = "6.1.0" }
9494
data-encoding = { version = "2.6.0" }
95-
directories = { version = "5.0.1" }
96-
dirs-sys = { version = "0.4.1" }
9795
dunce = { version = "1.0.5" }
9896
either = { version = "1.13.0" }
9997
encoding_rs_io = { version = "0.1.7" }

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: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::ops::Deref;
33
use std::path::{Path, PathBuf};
44

5+
use etcetera::BaseStrategy;
56
use tracing::debug;
67

78
use uv_fs::Simplified;
@@ -176,23 +177,12 @@ impl From<Options> for FilesystemOptions {
176177

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

198188
#[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)