From 9f56b1d64ef57bf109b1408570d9c85b5131b644 Mon Sep 17 00:00:00 2001 From: Zhenping Zhao Date: Sat, 23 Nov 2024 10:56:03 -0800 Subject: [PATCH] Remove cfg-if crate --- crates/nu-path/Cargo.toml | 1 - crates/nu-path/src/lib.rs | 4 +- crates/nu-path/src/pwd_per_drive.rs | 572 +++++++++--------- crates/nu-path/src/tilde.rs | 3 +- crates/nu-protocol/src/engine/engine_state.rs | 3 +- crates/nu-protocol/src/engine/stack.rs | 3 +- 6 files changed, 284 insertions(+), 302 deletions(-) diff --git a/crates/nu-path/Cargo.toml b/crates/nu-path/Cargo.toml index 53d4d437b2..178e62b090 100644 --- a/crates/nu-path/Cargo.toml +++ b/crates/nu-path/Cargo.toml @@ -13,7 +13,6 @@ bench = false [dependencies] dirs = { workspace = true } -cfg-if = "1.0.0" [target.'cfg(windows)'.dependencies] omnipath = { workspace = true } diff --git a/crates/nu-path/src/lib.rs b/crates/nu-path/src/lib.rs index fdf28b3bc0..130181100a 100644 --- a/crates/nu-path/src/lib.rs +++ b/crates/nu-path/src/lib.rs @@ -6,6 +6,7 @@ pub mod expansions; pub mod form; mod helpers; mod path; +#[cfg(windows)] pub mod pwd_per_drive; mod tilde; mod trailing_slash; @@ -14,6 +15,7 @@ pub use components::components; pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs}; pub use helpers::{cache_dir, data_dir, home_dir, nu_config_dir}; pub use path::*; -pub use pwd_per_drive::pwd_per_drive_singleton::{expand_pwd_per_drive, set_pwd_per_drive}; +#[cfg(windows)] +pub use pwd_per_drive::_impl::singleton::{expand_pwd, set_pwd}; pub use tilde::expand_tilde; pub use trailing_slash::{has_trailing_slash, strip_trailing_slash}; diff --git a/crates/nu-path/src/pwd_per_drive.rs b/crates/nu-path/src/pwd_per_drive.rs index fc87466e9e..10be4a5ac7 100644 --- a/crates/nu-path/src/pwd_per_drive.rs +++ b/crates/nu-path/src/pwd_per_drive.rs @@ -1,366 +1,344 @@ -use std::path::{Path, PathBuf}; +#[cfg(windows)] +pub mod _impl { + use std::path::{Path, PathBuf}; -cfg_if::cfg_if! { if #[cfg(windows)] { - - -struct Drive2PWDmap { - map: [Option; 26], // Fixed-size array for A-Z -} - -impl Drive2PWDmap { - pub fn new() -> Self { - Drive2PWDmap { - map: Default::default(), // Initialize all to `None` - } + struct Drive2PWD { + map: [Option; 26], // Fixed-size array for A-Z } - /// Set the PWD for the drive letter in the absolute path. - /// Return String for error description. - pub fn set_pwd(&mut self, path: &Path) -> Result<(), String> { - if let (Some(drive_letter), Some(path_str)) = ( - Self::extract_drive_letter(path), - path.to_str(), - ) { - self.map[drive_letter as usize - 'A' as usize] = Some(path_str.to_string()); - return Ok(()); + impl Drive2PWD { + pub fn new() -> Self { + Drive2PWD { + map: Default::default(), // Initialize all to `None` + } } - Err(format!("Invalid path: {}", path.display())) - } - /// Get the PWD for drive, if not yet, ask GetFullPathNameW(), - /// or else return default r"X:\". - fn get_pwd(&mut self, drive: char) -> Option { - if drive.is_ascii_alphabetic() { - let drive = drive.to_ascii_uppercase(); - let index = drive as usize - 'A' as usize; - Some(self.map[index] - .clone() - .unwrap_or_else(|| + /// Set the PWD for the drive letter in the absolute path. + /// Return String for error description. + pub fn set_pwd(&mut self, path: &Path) -> Result<(), String> { + if let (Some(drive_letter), Some(path_str)) = + (Self::extract_drive_letter(path), path.to_str()) + { + self.map[drive_letter as usize - 'A' as usize] = Some(path_str.to_string()); + return Ok(()); + } + Err(format!("Invalid path: {}", path.display())) + } + + /// Get the PWD for drive, if not yet, ask GetFullPathNameW(), + /// or else return default r"X:\". + fn get_pwd(&mut self, drive: char) -> Option { + if drive.is_ascii_alphabetic() { + let drive = drive.to_ascii_uppercase(); + let index = drive as usize - 'A' as usize; + Some(self.map[index].clone().unwrap_or_else(|| { if let Some(system_pwd) = get_full_path_name_w(&format!("{}:", drive)) { self.map[index] = Some(system_pwd.clone()); system_pwd } else { format!(r"{}:\", drive) } - ) - ) - } else { - None - } - } - - /// Expand a relative path using the PWD-per-drive, return PathBuf - /// of absolute path. - /// Return None if path is not valid or can't get drive letter. - pub fn expand_path(&mut self, path: &Path) -> Option { - let path_str = path.to_str()?; - if let Some(drive_letter) = Self::extract_drive_letter(path) { - if let Some(pwd) = self.get_pwd(drive_letter) { - // Combine current PWD with the relative path - let mut base = PathBuf::from(Self::ensure_trailing_separator(&pwd)); - base.push(path_str.split_at(2).1); // Skip the "C:" part of the relative path - return Some(base) + })) + } else { + None } } - None // Invalid path or has no drive letter - } - /// Extract the drive letter from a path (e.g., `C:test` -> `C`) - fn extract_drive_letter(path: &Path) -> Option { - Some(path.to_str() - .and_then(|s| s.chars().next()) - .filter(|c| c.is_ascii_alphabetic())? - ) - } + /// Expand a relative path using the PWD-per-drive, return PathBuf + /// of absolute path. + /// Return None if path is not valid or can't get drive letter. + pub fn expand_path(&mut self, path: &Path) -> Option { + let path_str = path.to_str()?; + if let Some(drive_letter) = Self::extract_drive_letter(path) { + if let Some(pwd) = self.get_pwd(drive_letter) { + // Combine current PWD with the relative path + let mut base = PathBuf::from(Self::ensure_trailing_separator(&pwd)); + base.push(path_str.split_at(2).1); // Skip the "C:" part of the relative path + return Some(base); + } + } + None // Invalid path or has no drive letter + } - /// Ensure a path has a trailing `\` - fn ensure_trailing_separator(path: &str) -> String { - if !path.ends_with('\\') && !path.ends_with('/') { - format!(r"{}\", path) - } else { - path.to_string() + /// Extract the drive letter from a path (e.g., `C:test` -> `C`) + fn extract_drive_letter(path: &Path) -> Option { + Some( + path.to_str() + .and_then(|s| s.chars().next()) + .filter(|c| c.is_ascii_alphabetic())?, + ) + } + + /// Ensure a path has a trailing `\` + fn ensure_trailing_separator(path: &str) -> String { + if !path.ends_with('\\') && !path.ends_with('/') { + format!(r"{}\", path) + } else { + path.to_string() + } } } -} -// GetFullPathW -fn get_full_path_name_w(path_str: &str) -> Option { - use std::ffi::OsString; - use std::os::windows::ffi::OsStringExt; - use std::os::windows::ffi::OsStrExt; - use winapi::um::fileapi::GetFullPathNameW; + // Wrapper of Win32 API GetFullPathW() + fn get_full_path_name_w(path_str: &str) -> Option { + use std::ffi::OsString; + use std::os::windows::ffi::OsStrExt; + use std::os::windows::ffi::OsStringExt; + use winapi::um::fileapi::GetFullPathNameW; - const MAX_PATH : usize = 260; - let mut buffer: [u16; MAX_PATH] = [0; MAX_PATH]; + const MAX_PATH: usize = 260; + let mut buffer: [u16; MAX_PATH] = [0; MAX_PATH]; - unsafe { - // Convert input to wide string. - let wide_path: Vec = OsString::from(path_str).encode_wide().chain(Some(0)).collect(); - let length = GetFullPathNameW( - wide_path.as_ptr(), - buffer.len() as u32, - buffer.as_mut_ptr(), - std::ptr::null_mut(), - ); + unsafe { + // Convert input to wide string. + let wide_path: Vec = OsString::from(path_str) + .encode_wide() + .chain(Some(0)) + .collect(); + let length = GetFullPathNameW( + wide_path.as_ptr(), + buffer.len() as u32, + buffer.as_mut_ptr(), + std::ptr::null_mut(), + ); - if length > 0 && (length as usize) < MAX_PATH { - let path = OsString::from_wide(&buffer[..length as usize]); - if let Some(path_str) = path.to_str() { - let path_string = path_str.to_string(); - { - return Some(path_string); + if length > 0 && (length as usize) < MAX_PATH { + let path = OsString::from_wide(&buffer[..length as usize]); + if let Some(path_str) = path.to_str() { + let path_string = path_str.to_string(); + { + return Some(path_string); + } } } } + None } - None -} -/// Global singleton instance of DrivePwdMap -use std::sync::{Once, Mutex}; + /// Global singleton instance of DrivePwdMap + use std::sync::{Mutex, Once}; -static INIT: Once = Once::new(); -static mut DRIVE_PWD_MAP: Option> = None; + static INIT: Once = Once::new(); + static mut DRIVE_PWD_MAP: Option> = None; -/// Access the singleton instance -fn get_drive_pwd_map() -> &'static Mutex { - unsafe { - INIT.call_once(|| { - DRIVE_PWD_MAP = Some(Mutex::new(Drive2PWDmap::new())); - }); - DRIVE_PWD_MAP.as_ref().unwrap() - } -} - -/// Test for Drive2PWD map -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_singleton_set_and_get_pwd() { - // To avoid conflict with other test threads (on testing result), - // use different drive set in multiple singleton tests - let drive_pwd_map = get_drive_pwd_map(); - { - let mut map = drive_pwd_map.lock().unwrap(); - - // Set PWD for drive X - assert!(map.set_pwd(Path::new(r"X:\Users\Example")).is_ok()); + /// Access the singleton instance + fn get_drive_pwd_map() -> &'static Mutex { + unsafe { + INIT.call_once(|| { + DRIVE_PWD_MAP = Some(Mutex::new(Drive2PWD::new())); + }); + DRIVE_PWD_MAP.as_ref().unwrap() } + } - { - let mut map = drive_pwd_map.lock().unwrap(); + /// Test for Drive2PWD map + #[cfg(test)] + mod tests { + use super::*; - // Get PWD for drive X - assert_eq!(map.get_pwd('X'), Some(r"X:\Users\Example".to_string())); + #[test] + fn test_singleton_set_and_get_pwd() { + // To avoid conflict with other test threads (on testing result), + // use different drive set in multiple singleton tests + let drive_pwd_map = get_drive_pwd_map(); + { + let mut map = drive_pwd_map.lock().unwrap(); - // Get PWD for drive E (not set, should return E:\) ??? - // 11-21-2024 happened to start nushell from drive E:, - // run toolkit test 'toolkit check pr' then this test failed - // since the singleton has its own state, so this type of test ('not set, - // should return ...') must be more careful to avoid accidentally fail. - if let Some(pwd_on_e) = get_full_path_name_w("E:") { - assert_eq!(map.get_pwd('E'), Some(pwd_on_e)); - } else { - assert_eq!(map.get_pwd('E'), Some(r"E:\".to_string())); + // Set PWD for drive X + assert!(map.set_pwd(Path::new(r"X:\Users\Example")).is_ok()); + } + + { + let mut map = drive_pwd_map.lock().unwrap(); + + // Get PWD for drive X + assert_eq!(map.get_pwd('X'), Some(r"X:\Users\Example".to_string())); + + // Get PWD for drive E (not set, should return E:\) ??? + // 11-21-2024 happened to start nushell from drive E:, + // run toolkit test 'toolkit check pr' then this test failed + // since the singleton has its own state, so this type of test ('not set, + // should return ...') must be more careful to avoid accidentally fail. + if let Some(pwd_on_e) = get_full_path_name_w("E:") { + assert_eq!(map.get_pwd('E'), Some(pwd_on_e)); + } else { + assert_eq!(map.get_pwd('E'), Some(r"E:\".to_string())); + } } } - } - #[test] - fn test_expand_path() { - let mut drive_map = Drive2PWDmap::new(); + #[test] + fn test_expand_path() { + let mut drive_map = Drive2PWD::new(); - // Set PWD for drive C - assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(())); + // Set PWD for drive C + assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(())); - // Expand a relative path - let expanded = drive_map.expand_path(Path::new(r"C:test")); - assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test"))); + // Expand a relative path + let expanded = drive_map.expand_path(Path::new(r"C:test")); + assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test"))); - // Expand an absolute path - let expanded = drive_map.expand_path(Path::new(r"C:\absolute\path")); - assert_eq!(expanded, Some(PathBuf::from(r"C:\absolute\path"))); + // Expand an absolute path + let expanded = drive_map.expand_path(Path::new(r"C:\absolute\path")); + assert_eq!(expanded, Some(PathBuf::from(r"C:\absolute\path"))); - // Expand with no drive letter - let expanded = drive_map.expand_path(Path::new(r"\no_drive")); - assert_eq!(expanded, None); + // Expand with no drive letter + let expanded = drive_map.expand_path(Path::new(r"\no_drive")); + assert_eq!(expanded, None); - // Expand with no PWD set for the drive - let expanded = drive_map.expand_path(Path::new("D:test")); - if let Some(pwd_on_d) = get_full_path_name_w("D:") { - assert_eq!(expanded, Some(PathBuf::from(format!(r"{}test", Drive2PWDmap::ensure_trailing_separator(&pwd_on_d))))); - } else { - assert_eq!(expanded, Some(PathBuf::from(r"D:\test"))); + // Expand with no PWD set for the drive + let expanded = drive_map.expand_path(Path::new("D:test")); + if let Some(pwd_on_d) = get_full_path_name_w("D:") { + assert_eq!( + expanded, + Some(PathBuf::from(format!( + r"{}test", + Drive2PWD::ensure_trailing_separator(&pwd_on_d) + ))) + ); + } else { + assert_eq!(expanded, Some(PathBuf::from(r"D:\test"))); + } + } + + #[test] + fn test_set_and_get_pwd() { + let mut drive_map = Drive2PWD::new(); + + // Set PWD for drive C + assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok()); + assert_eq!( + drive_map.get_pwd('C'), + Some(r"C:\Users\Example".to_string()) + ); + + // Set PWD for drive D + assert!(drive_map.set_pwd(Path::new(r"D:\Projects")).is_ok()); + assert_eq!(drive_map.get_pwd('D'), Some(r"D:\Projects".to_string())); + + // Get PWD for drive E (not set, should return E:\) + // 11-21-2024 happened to start nushell from drive E:, + // run toolkit test 'toolkit check pr' then this test failed + // if a drive has not been set PWD, it will ask system to get + // current directory, so this type of test ('not set, should + // return ...') must be more careful to avoid accidentally fail. + if let Some(pwd_on_e) = get_full_path_name_w("E:") { + assert_eq!(drive_map.get_pwd('E'), Some(pwd_on_e)); + } else { + assert_eq!(drive_map.get_pwd('E'), Some(r"E:\".to_string())); + } + } + + #[test] + fn test_set_pwd_invalid_path() { + let mut drive_map = Drive2PWD::new(); + + // Invalid path (no drive letter) + let result = drive_map.set_pwd(Path::new(r"\InvalidPath")); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), r"Invalid path: \InvalidPath"); + } + + #[test] + fn test_get_pwd_invalid_drive() { + let mut drive_map = Drive2PWD::new(); + + // Get PWD for a drive not set (e.g., Z) + assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string())); + + // Invalid drive letter (non-alphabetic) + assert_eq!(drive_map.get_pwd('1'), None); } } - #[test] - fn test_set_and_get_pwd() { - let mut drive_map = Drive2PWDmap::new(); - - // Set PWD for drive C - assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok()); - assert_eq!(drive_map.get_pwd('C'), Some(r"C:\Users\Example".to_string())); - - // Set PWD for drive D - assert!(drive_map.set_pwd(Path::new(r"D:\Projects")).is_ok()); - assert_eq!(drive_map.get_pwd('D'), Some(r"D:\Projects".to_string())); - - // Get PWD for drive E (not set, should return E:\) - // 11-21-2024 happened to start nushell from drive E:, - // run toolkit test 'toolkit check pr' then this test failed - // if a drive has not been set PWD, it will ask system to get - // current directory, so this type of test ('not set, should - // return ...') must be more careful to avoid accidentally fail. - if let Some(pwd_on_e) = get_full_path_name_w("E:") { - assert_eq!(drive_map.get_pwd('E'), Some(pwd_on_e)); - } else { - assert_eq!(drive_map.get_pwd('E'), Some(r"E:\".to_string())); - } - } - - #[test] - fn test_set_pwd_invalid_path() { - let mut drive_map = Drive2PWDmap::new(); - - // Invalid path (no drive letter) - let result = drive_map.set_pwd(Path::new(r"\InvalidPath")); - assert!(result.is_err()); - assert_eq!(result.unwrap_err(), r"Invalid path: \InvalidPath"); - } - - #[test] - fn test_get_pwd_invalid_drive() { - let mut drive_map = Drive2PWDmap::new(); - - // Get PWD for a drive not set (e.g., Z) - assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string())); - - // Invalid drive letter (non-alphabetic) - assert_eq!(drive_map.get_pwd('1'), None); - } -} - -}} // cfg_if! if #[cfg(windows)] - -/// Usage for pwd_per_drive on windows -/// -/// Upon change PWD, call set_pwd_per_drive() with absolute path -/// -/// Call expand_pwd_per_drive() with relative path to get absolution path -/// -/// ``` -/// use std::path::{Path, PathBuf}; -/// use nu_path::{expand_pwd_per_drive, set_pwd_per_drive}; -/// -/// //assert!(false); // Comment out to verify really tested -/// if cfg!(windows) { -/// // Set PWD for drive C -/// set_pwd_per_drive(Path::new(r"C:\Users\Home")).unwrap(); -/// -/// // Expand a relative path -/// let expanded = expand_pwd_per_drive(Path::new("C:test")); -/// assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test"))); -/// -/// // Will NOT expand an absolute path -/// let expanded = expand_pwd_per_drive(Path::new(r"C:\absolute\path")); -/// assert_eq!(expanded, None); -/// -/// // Expand with no drive letter -/// let expanded = expand_pwd_per_drive(Path::new(r"\no_drive")); -/// assert_eq!(expanded, None); -/// -/// // Expand with no PWD set for the drive -/// let expanded = expand_pwd_per_drive(Path::new("D:test")); -/// assert_eq!(expanded, Some(PathBuf::from(r"D:\test"))); -/// } -/// ``` -pub mod pwd_per_drive_singleton { - use super::*; - - /// set_pwd_per_drive - /// On Windows, record PWD for drive, path must be absolute path - /// return Ok(()) if succeeded, otherwise error message - /// Other platforms, return Ok(()) - pub fn set_pwd_per_drive(_path: &Path) -> Result<(), String> { - cfg_if::cfg_if! { if #[cfg(target_os="windows")] { + /// Usage for pwd_per_drive on windows + /// + /// Upon change PWD, call set_pwd_per_drive() with absolute path + /// + /// Call expand_pwd_per_drive() with relative path to get absolution path + /// + /// ``` + /// use std::path::{Path, PathBuf}; + /// use nu_path::{expand_pwd, set_pwd}; + /// + /// // Set PWD for drive C + /// set_pwd(Path::new(r"C:\Users\Home")).unwrap(); + /// + /// // Expand a relative path + /// let expanded = expand_pwd(Path::new("C:test")); + /// assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test"))); + /// + /// // Will NOT expand an absolute path + /// let expanded = expand_pwd(Path::new(r"C:\absolute\path")); + /// assert_eq!(expanded, None); + /// + /// // Expand with no drive letter + /// let expanded = expand_pwd(Path::new(r"\no_drive")); + /// assert_eq!(expanded, None); + /// + /// // Expand with no PWD set for the drive + /// let expanded = expand_pwd(Path::new("D:test")); + /// assert_eq!(expanded, Some(PathBuf::from(r"D:\test"))); + /// ``` + pub mod singleton { + use super::*; + /// set_pwd_per_drive + /// Record PWD for drive, path must be absolute path + /// return Ok(()) if succeeded, otherwise error message + pub fn set_pwd(_path: &Path) -> Result<(), String> { if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() { pwd_per_drive.set_pwd(_path) } else { Err("Failed to lock map".to_string()) } - - } else { - - Ok(()) - - }} - } - - /// expand_pwe_per_drive - /// On windows, input relative path, expand PWD-per-drive to construct absolute path - /// return PathBuf for absolute path, None if input path is invalid. - /// Otherwise, return None. - pub fn expand_pwd_per_drive(_path: &Path) -> Option { - cfg_if::cfg_if! { if #[cfg(target_os="windows")] { - - if need_expand_pwd_per_drive(_path) { - if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() { - return pwd_per_drive.expand_path(_path); - } } - }} - - None - } - - cfg_if::cfg_if! { if #[cfg(target_os="windows")] { - /// Helper only used on Windows, if input path is relative path - /// with drive letter, it can be expanded with PWD-per-drive. - fn need_expand_pwd_per_drive(_path: &Path) -> bool { - if let Some(path_str) = _path.to_str() { - let chars: Vec = path_str.chars().collect(); - if chars.len() >= 2 { - return chars[1] == ':' && - (chars.len() == 2 || - (chars[2] != '/' && - chars[2] != '\\' - ) - ); + /// expand_pwe_per_drive + /// Input relative path, expand PWD-per-drive to construct absolute path + /// return PathBuf for absolute path, None if input path is invalid. + pub fn expand_pwd(_path: &Path) -> Option { + if need_expand_pwd_per_drive(_path) { + if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() { + return pwd_per_drive.expand_path(_path); + } } + None } - false - } - }} - #[test] - fn test_usage_for_pwd_per_drive() { - // Set PWD for drive F - assert!(set_pwd_per_drive(Path::new(r"F:\Users\Home")).is_ok()); + /// Helper only used on Windows, if input path is relative path + /// with drive letter, it can be expanded with PWD-per-drive. + fn need_expand_pwd_per_drive(_path: &Path) -> bool { + if let Some(path_str) = _path.to_str() { + let chars: Vec = path_str.chars().collect(); + if chars.len() >= 2 { + return chars[1] == ':' + && (chars.len() == 2 || (chars[2] != '/' && chars[2] != '\\')); + } + } + false + } + + #[test] + fn test_usage_for_pwd_per_drive() { + // Set PWD for drive F + assert!(set_pwd(Path::new(r"F:\Users\Home")).is_ok()); - if cfg!(windows) { // Expand a relative path - let expanded = expand_pwd_per_drive(Path::new("f:test")); + let expanded = expand_pwd(Path::new("f:test")); assert_eq!(expanded, Some(PathBuf::from(r"F:\Users\Home\test"))); // Will NOT expand an absolute path - let expanded = expand_pwd_per_drive(Path::new(r"F:\absolute\path")); + let expanded = expand_pwd(Path::new(r"F:\absolute\path")); assert_eq!(expanded, None); // Expand with no drive letter - let expanded = expand_pwd_per_drive(Path::new(r"\no_drive")); + let expanded = expand_pwd(Path::new(r"\no_drive")); assert_eq!(expanded, None); // Expand with no PWD set for the drive - let expanded = expand_pwd_per_drive(Path::new("G:test")); + let expanded = expand_pwd(Path::new("G:test")); assert_eq!(expanded, Some(PathBuf::from(r"G:\test"))); - } else { - // None always - assert_eq!(None, expand_pwd_per_drive(Path::new("F:test"))); } } } diff --git a/crates/nu-path/src/tilde.rs b/crates/nu-path/src/tilde.rs index 262ae172e5..7c09cda317 100644 --- a/crates/nu-path/src/tilde.rs +++ b/crates/nu-path/src/tilde.rs @@ -22,7 +22,8 @@ fn expand_tilde_with_home(path: impl AsRef, home: Option) -> Path if !path.starts_with("~") { // Try to expand relative path by PWD-per-drive - if let Some(expanded_dir) = crate::expand_pwd_per_drive(path) { + #[cfg(windows)] + if let Some(expanded_dir) = crate::expand_pwd(path) { return expanded_dir; } let string = path.to_string_lossy(); diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 38de1d9d5b..1efe19c3ff 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -957,7 +957,8 @@ impl EngineState { } else if !path.is_dir() { Err(error("$env.PWD points to a non-directory", &path)) } else { - let _ = nu_path::set_pwd_per_drive(path.as_std_path()); + #[cfg(windows)] + let _ = nu_path::set_pwd(path.as_std_path()); Ok(path) } } else { diff --git a/crates/nu-protocol/src/engine/stack.rs b/crates/nu-protocol/src/engine/stack.rs index b97e593d81..0900f796be 100644 --- a/crates/nu-protocol/src/engine/stack.rs +++ b/crates/nu-protocol/src/engine/stack.rs @@ -727,7 +727,8 @@ impl Stack { let value = Value::string(path.to_string_lossy(), Span::unknown()); self.add_env_var("PWD".into(), value); // Sync with PWD-per-drive - let _ = nu_path::set_pwd_per_drive(&path); + #[cfg(windows)] + let _ = nu_path::set_pwd(&path); Ok(()) } }