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,39 +81,44 @@ impl Prettier {
if worktree_root != starting_path.worktree_root_path.as_ref() {
vec![worktree_root]
} else {
let (worktree_root_metadata, start_path_metadata) = if starting_path
.starting_path
.as_ref()
== Path::new("")
{
let worktree_root_data =
fs.metadata(&worktree_root).await.with_context(|| {
format!(
"FS metadata fetch for worktree root path {worktree_root:?}",
)
let worktree_root_metadata = fs
.metadata(&worktree_root)
.await
.with_context(|| {
format!("FS metadata fetch for worktree root path {worktree_root:?}",)
})?
.with_context(|| {
format!("empty FS metadata for worktree root at {worktree_root:?}")
})?;
(worktree_root_data.unwrap_or_else(|| {
panic!("cannot query prettier for non existing worktree root at {worktree_root:?}")
}), None)
if starting_path.starting_path.as_ref() == Path::new("") {
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()
} else {
let full_starting_path = worktree_root.join(&starting_path.starting_path);
let (worktree_root_data, start_path_data) = futures::try_join!(
fs.metadata(&worktree_root),
fs.metadata(&full_starting_path),
)
let start_path_metadata = fs
.metadata(&full_starting_path)
.await
.with_context(|| {
format!("FS metadata fetch for starting path {full_starting_path:?}",)
})?;
(
worktree_root_data.unwrap_or_else(|| {
panic!("cannot query prettier for non existing worktree root at {worktree_root:?}")
}),
start_path_data,
format!(
"FS metadata fetch for starting path {full_starting_path:?}"
)
};
})?
.with_context(|| {
format!(
"empty FS metadata for starting path {full_starting_path:?}"
)
})?;
match start_path_metadata {
Some(start_path_metadata) => {
anyhow::ensure!(worktree_root_metadata.is_dir,
"For non-empty start path, worktree root {starting_path:?} should be a directory");
anyhow::ensure!(
@ -138,21 +143,6 @@ impl Prettier {
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()
}
}
}
}
None => Vec::new(),