`Path.strip_prefix` leaves a leading separator for UNC share root on Windows
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
When base is a UNC share root on Windows, Path.strip_prefix returns a Path with a leading separator.
I tried this code:
let path = Path::new(r"\\?\UNC\server\share\dir1\dir2");
let base = Path::new(r"\\?\UNC\server\share");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
I expected to see this happen: the assert_eq! should pass.
Instead, this happened: the assert_eq! fails.
assertion `left == right` failed
left: "\\dir1\\dir2"
right: "dir1\\dir2"
Note: the UNC path is what canonicalize() returns for a network share.
With this in the command prompt:
net use Z: \\server\share
The following code reproduces the issue.
let path = Path::new(r"Z:\dir1\dir2").canonicalize()?;
let base = Path::new(r"Z:\").canonicalize()?;
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
Note 2: following cases works as expected.
- When
baseis not a root of a network share.
let path = Path::new(r"\\?\UNC\server\share\dir1\dir2");
let base = Path::new(r"\\?\UNC\server\share\dir1");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir2");
- When it's not in the
\\?\UNCform.
let path = Path::new(r"\\server\share\dir1\dir2");
let base = Path::new(r"\\server\share");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
Meta
rustc --version --verbose:
rustc 1.94.1 (e408947bf 2026-03-25)
binary: rustc
commit-hash: e408947bfd200af42db322daf0fadfe7e26d3bd1
commit-date: 2026-03-25
host: x86_64-pc-windows-msvc
release: 1.94.1
LLVM version: 21.1.8
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Path::strip_prefix and reproduce the reported UNC-share case on Windows, comparing it with the working non-root and non-\?\UNC cases. Done means the canonicalized UNC share-root example returns "dir1\dir2" without a leading separator while the existing working cases remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100