From 2ddf85492ff5d5806e63a46286ee0e3603042c02 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 25 May 2023 15:29:58 -0400 Subject: fix(compile): improve panic message when stripping root path fails (#19258) I'm not sure what's going on here, so this will help us debug. For #19251 --- cli/standalone/virtual_fs.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cli/standalone/virtual_fs.rs b/cli/standalone/virtual_fs.rs index 31f630db8..e94758bd9 100644 --- a/cli/standalone/virtual_fs.rs +++ b/cli/standalone/virtual_fs.rs @@ -84,7 +84,7 @@ impl VfsBuilder { } pub fn add_dir(&mut self, path: &Path) -> &mut VirtualDirectory { - let path = path.strip_prefix(&self.root_path).unwrap(); + let path = self.path_relative_root(path); let mut current_dir = &mut self.root_dir; for component in path.components() { @@ -151,7 +151,7 @@ impl VfsBuilder { } pub fn add_symlink(&mut self, path: &Path, target: &Path) { - let dest = target.strip_prefix(&self.root_path).unwrap().to_path_buf(); + let dest = self.path_relative_root(target); let dir = self.add_dir(path.parent().unwrap()); let name = path.file_name().unwrap().to_string_lossy(); match dir.entries.binary_search_by(|e| e.name().cmp(&name)) { @@ -174,6 +174,20 @@ impl VfsBuilder { pub fn into_dir_and_files(self) -> (VirtualDirectory, Vec>) { (self.root_dir, self.files) } + + fn path_relative_root(&self, path: &Path) -> PathBuf { + match path.strip_prefix(&self.root_path) { + Ok(p) => p.to_path_buf(), + Err(err) => { + panic!( + "Failed to strip prefix '{}' from '{}': {:#}", + self.root_path.display(), + path.display(), + err + ) + } + } + } } #[derive(Debug)] -- cgit v1.2.3