diff --git a/Cargo.lock b/Cargo.lock index 8b9392469..1c7bc4c6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1046,12 +1046,6 @@ dependencies = [ "rust_decimal", ] -[[package]] -name = "dunce" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541" - [[package]] name = "dyn-clone" version = "1.0.9" @@ -2810,7 +2804,7 @@ name = "nu-path" version = "0.70.1" dependencies = [ "dirs-next", - "dunce", + "omnipath", "pwd", ] @@ -3145,6 +3139,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "omnipath" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7461858c5ac9bde3fcdeedc17f58ed0469ec74f2d737b6369fc31c0b0ef576c" + [[package]] name = "once_cell" version = "1.15.0" diff --git a/crates/nu-path/Cargo.toml b/crates/nu-path/Cargo.toml index 12803c63f..f38db741b 100644 --- a/crates/nu-path/Cargo.toml +++ b/crates/nu-path/Cargo.toml @@ -9,7 +9,7 @@ version = "0.70.1" [dependencies] dirs-next = "2.0.0" -dunce = "1.0.1" +omnipath = "0.1.1" [target.'cfg(all(unix, not(target_os = "macos")))'.dependencies] -pwd = "1.3.1" \ No newline at end of file +pwd = "1.3.1" diff --git a/crates/nu-path/src/dots.rs b/crates/nu-path/src/dots.rs index b6025c479..ae6c6dbfb 100644 --- a/crates/nu-path/src/dots.rs +++ b/crates/nu-path/src/dots.rs @@ -1,5 +1,7 @@ use std::path::{is_separator, Component, Path, PathBuf}; +use super::helpers; + const EXPAND_STR: &str = if cfg!(windows) { r"..\" } else { "../" }; fn handle_dots_push(string: &mut String, count: u8) { @@ -108,7 +110,7 @@ pub fn expand_dots(path: impl AsRef) -> PathBuf { _ => result.push(component), }); - dunce::simplified(&result).to_path_buf() + helpers::simiplified(&result) } #[cfg(test)] diff --git a/crates/nu-path/src/expansions.rs b/crates/nu-path/src/expansions.rs index ffa202a3d..fc5eeea5f 100644 --- a/crates/nu-path/src/expansions.rs +++ b/crates/nu-path/src/expansions.rs @@ -2,6 +2,7 @@ use std::io; use std::path::{Path, PathBuf}; use super::dots::{expand_dots, expand_ndots}; +use super::helpers; use super::tilde::expand_tilde; // Join a path relative to another path. Paths starting with tilde are considered as absolute. @@ -30,7 +31,7 @@ fn canonicalize(path: impl AsRef) -> io::Result { let path = expand_tilde(path); let path = expand_ndots(path); - dunce::canonicalize(path) + helpers::canonicalize(&path) } /// Resolve all symbolic links and all components (tilde, ., .., ...+) and return the path in its diff --git a/crates/nu-path/src/helpers.rs b/crates/nu-path/src/helpers.rs index eaab1958c..febe8a2a7 100644 --- a/crates/nu-path/src/helpers.rs +++ b/crates/nu-path/src/helpers.rs @@ -1,3 +1,5 @@ +#[cfg(windows)] +use omnipath::WinPathExt; use std::path::PathBuf; pub fn home_dir() -> Option { @@ -7,3 +9,22 @@ pub fn home_dir() -> Option { pub fn config_dir() -> Option { dirs_next::config_dir() } + +#[cfg(windows)] +pub fn canonicalize(path: &std::path::Path) -> std::io::Result { + path.canonicalize()?.to_winuser_path() +} +#[cfg(not(windows))] +pub fn canonicalize(path: &std::path::Path) -> std::io::Result { + path.canonicalize() +} + +#[cfg(windows)] +pub fn simiplified(path: &std::path::Path) -> PathBuf { + path.to_winuser_path() + .unwrap_or_else(|_| path.to_path_buf()) +} +#[cfg(not(windows))] +pub fn simiplified(path: &std::path::Path) -> PathBuf { + path.to_path_buf() +} diff --git a/tests/path/canonicalize.rs b/tests/path/canonicalize.rs index 4109c759c..73c72a473 100644 --- a/tests/path/canonicalize.rs +++ b/tests/path/canonicalize.rs @@ -419,3 +419,14 @@ fn canonicalize_with_should_fail() { assert!(canonicalize_with(path, relative_to).is_err()); } + +#[cfg(windows)] +#[test] +fn canonicalize_unc() { + // Ensure that canonicalizing UNC paths does not turn them verbatim. + // Assumes the C drive exists and that the `localhost` UNC path works. + let actual = + nu_path::canonicalize_with(r"\\localhost\c$", ".").expect("failed to canonicalize"); + let expected = Path::new(r"\\localhost\c$"); + assert_eq!(actual, expected); +}