From 0dd4eac6ca7628f5424cf7354b9d5fe5849799b7 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 19 Jan 2026 22:11:32 -0800 Subject: [PATCH] Enhance strip_trailing_separator to handle UNC and extended-length paths on Windows --- crates/pet-fs/src/path.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/pet-fs/src/path.rs b/crates/pet-fs/src/path.rs index 84e5643e..d64621f3 100644 --- a/crates/pet-fs/src/path.rs +++ b/crates/pet-fs/src/path.rs @@ -30,9 +30,11 @@ pub fn strip_trailing_separator>(path: P) -> PathBuf { #[cfg(windows)] { - // On Windows, preserve root paths like "C:\" + // On Windows, preserve root paths (e.g. "C:\", "\\server\", "\\?\C:\") let mut result = path_str.to_string(); - while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) { + while (result.ends_with('\\') || result.ends_with('/')) + && Path::new(&result).parent().is_some() + { result.pop(); } PathBuf::from(result) @@ -354,6 +356,31 @@ mod tests { assert_eq!(strip_trailing_separator("C:\\"), PathBuf::from("C:\\")); } + #[test] + #[cfg(windows)] + fn test_strip_trailing_separator_windows_unc_paths() { + // UNC path with trailing separator - should strip it + assert_eq!( + strip_trailing_separator("\\\\server\\share\\folder\\"), + PathBuf::from("\\\\server\\share\\folder") + ); + // UNC root path should be preserved + assert_eq!( + strip_trailing_separator("\\\\server\\share\\"), + PathBuf::from("\\\\server\\share\\") + ); + // Extended-length path root should be preserved + assert_eq!( + strip_trailing_separator("\\\\?\\C:\\"), + PathBuf::from("\\\\?\\C:\\") + ); + // Extended-length path with subfolder - should strip trailing separator + assert_eq!( + strip_trailing_separator("\\\\?\\C:\\Users\\"), + PathBuf::from("\\\\?\\C:\\Users") + ); + } + // ==================== norm_case tests ==================== #[test]