Skip to content

Commit 13831f6

Browse files
committed
Avoid Windows invalid UNC path.
On Windows, Path::join may makes invalid UNC path. It was fixed on nightly (rust-lang/rust#89270), but not yet on stable.
1 parent 0f40b7b commit 13831f6

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/util/path.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use path_abs::PathAbs;
22
use std::{
33
fmt::{self, Display},
44
io,
5-
path::{Path, PathBuf},
5+
path::{Component, Path, PathBuf},
66
};
77
use thiserror::Error;
88

@@ -84,7 +84,33 @@ impl Display for PathNotPrefixed {
8484
}
8585

8686
pub fn prefix_path(root: impl AsRef<Path>, path: impl AsRef<Path>) -> PathBuf {
87-
root.as_ref().join(path)
87+
let root = root.as_ref();
88+
let path = path.as_ref();
89+
let is_verbatim = if let Some(Component::Prefix(prefix)) = root.components().next() {
90+
prefix.kind().is_verbatim()
91+
} else {
92+
false
93+
};
94+
if !is_verbatim {
95+
return root.join(path);
96+
}
97+
let mut buf = root.components().collect::<Vec<_>>();
98+
for component in path.components() {
99+
match component {
100+
Component::RootDir => {
101+
buf.truncate(1);
102+
buf.push(component);
103+
}
104+
Component::CurDir => {}
105+
Component::ParentDir => {
106+
if let Some(_) = buf.last() {
107+
buf.pop();
108+
}
109+
}
110+
_ => buf.push(component),
111+
};
112+
}
113+
buf.into_iter().collect()
88114
}
89115

90116
pub fn unprefix_path(

0 commit comments

Comments
 (0)