rust-lang / rust-lang/rust

`Path.strip_prefix` leaves a leading separator for UNC share root on Windows

Open
#155,183 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
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 base is 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 \\?\UNC form.
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.