Do not panic on prettier search

This commit is contained in:
Kirill Bulatov 2023-10-30 12:13:34 +02:00
parent 96bbb5cdea
commit 249bec3cac

View file

@ -81,77 +81,67 @@ impl Prettier {
if worktree_root != starting_path.worktree_root_path.as_ref() { if worktree_root != starting_path.worktree_root_path.as_ref() {
vec![worktree_root] vec![worktree_root]
} else { } else {
let (worktree_root_metadata, start_path_metadata) = if starting_path let worktree_root_metadata = fs
.starting_path .metadata(&worktree_root)
.as_ref() .await
== Path::new("") .with_context(|| {
{ format!("FS metadata fetch for worktree root path {worktree_root:?}",)
let worktree_root_data = })?
fs.metadata(&worktree_root).await.with_context(|| { .with_context(|| {
format!( format!("empty FS metadata for worktree root at {worktree_root:?}")
"FS metadata fetch for worktree root path {worktree_root:?}", })?;
) if starting_path.starting_path.as_ref() == Path::new("") {
})?; anyhow::ensure!(
(worktree_root_data.unwrap_or_else(|| { !worktree_root_metadata.is_dir,
panic!("cannot query prettier for non existing worktree root at {worktree_root:?}") "For empty start path, worktree root should not be a directory {starting_path:?}"
}), None) );
anyhow::ensure!(
!worktree_root_metadata.is_symlink,
"For empty start path, worktree root should not be a symlink {starting_path:?}"
);
worktree_root
.parent()
.map(|path| vec![path.to_path_buf()])
.unwrap_or_default()
} else { } else {
let full_starting_path = worktree_root.join(&starting_path.starting_path); let full_starting_path = worktree_root.join(&starting_path.starting_path);
let (worktree_root_data, start_path_data) = futures::try_join!( let start_path_metadata = fs
fs.metadata(&worktree_root), .metadata(&full_starting_path)
fs.metadata(&full_starting_path), .await
) .with_context(|| {
.with_context(|| { format!(
format!("FS metadata fetch for starting path {full_starting_path:?}",) "FS metadata fetch for starting path {full_starting_path:?}"
})?; )
( })?
worktree_root_data.unwrap_or_else(|| { .with_context(|| {
panic!("cannot query prettier for non existing worktree root at {worktree_root:?}") format!(
}), "empty FS metadata for starting path {full_starting_path:?}"
start_path_data, )
) })?;
};
match start_path_metadata { anyhow::ensure!(worktree_root_metadata.is_dir,
Some(start_path_metadata) => { "For non-empty start path, worktree root {starting_path:?} should be a directory");
anyhow::ensure!(worktree_root_metadata.is_dir, anyhow::ensure!(
"For non-empty start path, worktree root {starting_path:?} should be a directory"); !start_path_metadata.is_dir,
anyhow::ensure!( "For non-empty start path, it should not be a directory {starting_path:?}"
!start_path_metadata.is_dir, );
"For non-empty start path, it should not be a directory {starting_path:?}" anyhow::ensure!(
); !start_path_metadata.is_symlink,
anyhow::ensure!( "For non-empty start path, it should not be a symlink {starting_path:?}"
!start_path_metadata.is_symlink, );
"For non-empty start path, it should not be a symlink {starting_path:?}"
);
let file_to_format = starting_path.starting_path.as_ref(); let file_to_format = starting_path.starting_path.as_ref();
let mut paths_to_check = VecDeque::from(vec![worktree_root.clone()]); let mut paths_to_check = VecDeque::from(vec![worktree_root.clone()]);
let mut current_path = worktree_root; let mut current_path = worktree_root;
for path_component in file_to_format.components().into_iter() { for path_component in file_to_format.components().into_iter() {
current_path = current_path.join(path_component); current_path = current_path.join(path_component);
paths_to_check.push_front(current_path.clone()); paths_to_check.push_front(current_path.clone());
if path_component.as_os_str().to_string_lossy() == "node_modules" { if path_component.as_os_str().to_string_lossy() == "node_modules" {
break; break;
}
} }
paths_to_check.pop_front(); // last one is the file itself or node_modules, skip it
Vec::from(paths_to_check)
}
None => {
anyhow::ensure!(
!worktree_root_metadata.is_dir,
"For empty start path, worktree root should not be a directory {starting_path:?}"
);
anyhow::ensure!(
!worktree_root_metadata.is_symlink,
"For empty start path, worktree root should not be a symlink {starting_path:?}"
);
worktree_root
.parent()
.map(|path| vec![path.to_path_buf()])
.unwrap_or_default()
} }
paths_to_check.pop_front(); // last one is the file itself or node_modules, skip it
Vec::from(paths_to_check)
} }
} }
} }