Skip to content

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

@kojiishi

Description

@kojiishi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions