Skip to content

Commit

Permalink
Fix bug where some daps wouldn't update/install correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Oct 23, 2024
1 parent f12192c commit 2edf771
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
18 changes: 16 additions & 2 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion crates/dap_adapters/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions crates/dap_adapters/src/php.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/dap_adapters/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 24 additions & 2 deletions crates/util/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ where
}
}

pub async fn collect_matching<F>(dir: &Path, predicate: F) -> Vec<PathBuf>
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<F>(dir: &Path, predicate: F) -> Option<PathBuf>
where
F: Fn(&str) -> bool,
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 2edf771

Please sign in to comment.