From 13831f603ed9dd436788e08de72d8655b1546ca9 Mon Sep 17 00:00:00 2001 From: Shirayama Kazatsuyu Date: Fri, 5 Nov 2021 20:15:31 +0900 Subject: [PATCH] Avoid Windows invalid UNC path. On Windows, Path::join may makes invalid UNC path. It was fixed on nightly (https://github.com/rust-lang/rust/pull/89270), but not yet on stable. --- src/util/path.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/util/path.rs b/src/util/path.rs index fff8a249..0022315a 100644 --- a/src/util/path.rs +++ b/src/util/path.rs @@ -2,7 +2,7 @@ use path_abs::PathAbs; use std::{ fmt::{self, Display}, io, - path::{Path, PathBuf}, + path::{Component, Path, PathBuf}, }; use thiserror::Error; @@ -84,7 +84,33 @@ impl Display for PathNotPrefixed { } pub fn prefix_path(root: impl AsRef, path: impl AsRef) -> PathBuf { - root.as_ref().join(path) + let root = root.as_ref(); + let path = path.as_ref(); + let is_verbatim = if let Some(Component::Prefix(prefix)) = root.components().next() { + prefix.kind().is_verbatim() + } else { + false + }; + if !is_verbatim { + return root.join(path); + } + let mut buf = root.components().collect::>(); + for component in path.components() { + match component { + Component::RootDir => { + buf.truncate(1); + buf.push(component); + } + Component::CurDir => {} + Component::ParentDir => { + if let Some(_) = buf.last() { + buf.pop(); + } + } + _ => buf.push(component), + }; + } + buf.into_iter().collect() } pub fn unprefix_path(