-
Notifications
You must be signed in to change notification settings - Fork 33
Add function to strip trailing path separators and preserve root paths #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new utility function strip_trailing_separator() to handle trailing path separators from paths while preserving root paths, addressing issue #278. The function provides a lightweight alternative to norm_case() when only trailing separator removal is needed without full path normalization.
Changes:
- Added
strip_trailing_separator()function with platform-specific implementations for Windows and Unix - Enhanced documentation for
norm_case(),resolve_symlink(), andexpand_path()with detailed usage notes and cross-references - Added comprehensive test coverage for the new function on both Unix and Windows platforms
Comments suppressed due to low confidence (2)
crates/pet-fs/src/path.rs:37
- The hardcoded length check
> 3is a magic number that isn't clearly explained. Consider adding a comment explaining this preserves Windows root paths likeC:\(3 characters) or using a named constant. Additionally, this doesn't account for edge cases likeC:/(forward slashes on Windows) which would have length 3 but should also be preserved.
while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) {
result.pop();
}
crates/pet-fs/src/path.rs:354
- The Windows tests are missing coverage for important edge cases: UNC paths (
\\?\C:\path\), network paths (\\server\share\), paths with forward slashes that equal length 3 (C:/), and paths with mixed trailing separators (C:\path\/). These edge cases should be tested given the complexity of Windows path handling.
assert_eq!(strip_trailing_separator("C:\\"), PathBuf::from("C:\\"));
| // On Windows, preserve root paths like "C:\" | ||
| let mut result = path_str.to_string(); | ||
| while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) { |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Windows implementation has a logic error. It preserves paths longer than 3 characters (e.g., C:\) but will fail for UNC paths like \\server\share\ (length > 3) and network paths like \\server\ which should be preserved as root paths. The check should be more sophisticated to handle UNC/network paths (\\?\..., \\server\...) similar to how normalize_case_windows() uses Path::parent() for robust root detection.
This issue also appears in the following locations of the same file:
- line 35
- line 354
| // On Windows, preserve root paths like "C:\" | |
| let mut result = path_str.to_string(); | |
| while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) { | |
| // On Windows, preserve root paths (e.g. "C:\", "\\server\", "\\?\C:\") | |
| let mut result = path_str.to_string(); | |
| while (result.ends_with('\\') || result.ends_with('/')) | |
| && Path::new(&result).parent().is_some() | |
| { |
Fixes #278