From 2b96093dd239d58e5d93d396ede720607af270fc Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 25 Nov 2024 17:18:55 -0600 Subject: [PATCH] Include explicit separator when constructing path to Windows global config file --- crates/uv-settings/src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/uv-settings/src/lib.rs b/crates/uv-settings/src/lib.rs index 187cf65d1d5c4..c8575de71d3d3 100644 --- a/crates/uv-settings/src/lib.rs +++ b/crates/uv-settings/src/lib.rs @@ -201,10 +201,25 @@ fn locate_system_config_xdg(value: Option<&str>) -> Option { None } -#[cfg(windows)] fn locate_system_config_windows(system_drive: &std::ffi::OsStr) -> Option { // On Windows, use `%SYSTEMDRIVE%\ProgramData\uv\uv.toml` (e.g., `C:\ProgramData`). - let candidate = PathBuf::from(system_drive).join("ProgramData\\uv\\uv.toml"); + let candidate = PathBuf::from(system_drive); + + // `PathBuf::join` does not include a separator for system drives, so we need to include it + // manually. However, we allow arbitrary file paths in the `SYSTEMDRIVE` environment variable + // for testing so we check if the path is actually a drive before adding the separator. + // + // See https://github.com/rust-lang/rust/blob/7db7489f9bc274cb60c4956bfa56de0185eb1b9b/library/std/src/path.rs#L1305-L1313 + let components = candidate.components(); + let prefix = if comps.prefix_len() > 0 + && comps.prefix_len() == comps.path.len() + && comps.prefix.unwrap().is_drive() { + "\\" + } else { + "" + }; + + let candidate = candidate.join(format!("{prefix}ProgramData\\uv\\uv.toml")); candidate.as_path().is_file().then_some(candidate) }