diff --git a/crates/dap/src/adapters.rs b/crates/dap/src/adapters.rs index 26d41604f062a..a4b64381e9c1c 100644 --- a/crates/dap/src/adapters.rs +++ b/crates/dap/src/adapters.rs @@ -7,7 +7,7 @@ use node_runtime::NodeRuntime; use serde_json::Value; use smol::{self, fs::File, process}; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, ffi::OsString, fmt::Debug, ops::Deref, @@ -101,6 +101,20 @@ pub async fn download_adapter_from_github( let mut file = File::create(&zip_path).await?; futures::io::copy(response.body_mut(), &mut file).await?; + let old_files: HashSet<_> = + util::fs::collect_matching(&adapter_path.as_path(), |file_path| { + file_path != zip_path.as_path() + }) + .await + .into_iter() + .filter_map(|file_path| { + file_path + .file_name() + .and_then(|f| f.to_str()) + .map(|f| f.to_string()) + }) + .collect(); + let _unzip_status = process::Command::new("unzip") .current_dir(&adapter_path) .arg(&zip_path) @@ -109,7 +123,7 @@ pub async fn download_adapter_from_github( .status; let file_name = util::fs::find_file_name_in_dir(&adapter_path.as_path(), |file_name| { - file_name.contains(&adapter_name.to_string()) + !file_name.ends_with(".zip") && !old_files.contains(file_name) }) .await .ok_or_else(|| anyhow!("Unzipped directory not found")); diff --git a/crates/dap_adapters/src/javascript.rs b/crates/dap_adapters/src/javascript.rs index 8740a23fdc2f4..2c59a9ae2cbd5 100644 --- a/crates/dap_adapters/src/javascript.rs +++ b/crates/dap_adapters/src/javascript.rs @@ -56,7 +56,7 @@ impl DebugAdapter for JsDebugAdapter { file_name.starts_with(&file_name_prefix) }) .await - .ok_or_else(|| anyhow!("Couldn't find javascript dap directory"))?; + .ok_or_else(|| anyhow!("Couldn't find Javascript dap directory"))?; let version = adapter_path .file_name() diff --git a/crates/dap_adapters/src/php.rs b/crates/dap_adapters/src/php.rs index 097a2ebd9c870..a5aa647822a4d 100644 --- a/crates/dap_adapters/src/php.rs +++ b/crates/dap_adapters/src/php.rs @@ -56,13 +56,13 @@ impl DebugAdapter for PhpDebugAdapter { file_name.starts_with(&file_name_prefix) }) .await - .ok_or_else(|| anyhow!("Couldn't find javascript dap directory"))?; + .ok_or_else(|| anyhow!("Couldn't find Php dap directory"))?; let version = adapter_path .file_name() .and_then(|file_name| file_name.to_str()) .and_then(|file_name| file_name.strip_prefix(&file_name_prefix)) - .ok_or_else(|| anyhow!("Javascript debug adapter has invalid file name"))? + .ok_or_else(|| anyhow!("Php debug adapter has invalid file name"))? .to_string(); Ok(DebugAdapterBinary { diff --git a/crates/dap_adapters/src/python.rs b/crates/dap_adapters/src/python.rs index 70f2bffe960c8..136b95c097277 100644 --- a/crates/dap_adapters/src/python.rs +++ b/crates/dap_adapters/src/python.rs @@ -59,11 +59,11 @@ impl DebugAdapter for PythonDebugAdapter { .await .ok_or_else(|| anyhow!("Debugpy directory not found"))?; - let version = adapter_path + let version = debugpy_dir .file_name() .and_then(|file_name| file_name.to_str()) .and_then(|file_name| file_name.strip_prefix(&file_name_prefix)) - .ok_or_else(|| anyhow!("Javascript debug adapter has invalid file name"))? + .ok_or_else(|| anyhow!("Python debug adapter has invalid file name"))? .to_string(); Ok(DebugAdapterBinary { diff --git a/crates/util/src/fs.rs b/crates/util/src/fs.rs index 93340729df2b4..92ca4e60aa1ce 100644 --- a/crates/util/src/fs.rs +++ b/crates/util/src/fs.rs @@ -27,6 +27,25 @@ where } } +pub async fn collect_matching(dir: &Path, predicate: F) -> Vec +where + F: Fn(&Path) -> bool, +{ + let mut matching = vec![]; + + if let Some(mut entries) = fs::read_dir(dir).await.log_err() { + while let Some(entry) = entries.next().await { + if let Some(entry) = entry.log_err() { + if predicate(entry.path().as_path()) { + matching.push(entry.path()); + } + } + } + } + + matching +} + pub async fn find_file_name_in_dir(dir: &Path, predicate: F) -> Option where F: Fn(&str) -> bool, @@ -36,8 +55,11 @@ where if let Some(entry) = entry.log_err() { let entry_path = entry.path(); - if let Some(file_name) = entry_path.file_name() { - if predicate(file_name.to_str().unwrap_or("")) { + if let Some(file_name) = entry_path + .file_name() + .and_then(|file_name| file_name.to_str()) + { + if predicate(file_name) { return Some(entry_path); } }