Skip to content

Commit

Permalink
Wip require worktree to start debug session
Browse files Browse the repository at this point in the history
  • Loading branch information
RemcoSmitsDev committed Jan 21, 2025
1 parent cc545ff commit fa6aec3
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 165 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use async_trait::async_trait;
use futures::io::BufReader;
use gpui::SharedString;
use gpui::{AsyncAppContext, SharedString};
pub use http_client::{github::latest_github_release, HttpClient};
use language::{LanguageName, Toolchain};
use language::LanguageToolchainStore;
use node_runtime::NodeRuntime;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use settings::WorktreeId;
use smol::{self, fs::File, lock::Mutex};
use std::{
collections::{HashMap, HashSet},
Expand All @@ -36,9 +37,10 @@ pub enum DapStatus {

#[async_trait(?Send)]
pub trait DapDelegate {
fn http_client(&self) -> Option<Arc<dyn HttpClient>>;
fn worktree_id(&self) -> WorktreeId;
fn http_client(&self) -> Arc<dyn HttpClient>;
fn node_runtime(&self) -> Option<NodeRuntime>;
fn toolchain(&self, adapter_name: &DebugAdapterName) -> Option<&Toolchain>;
fn toolchain_store(&self) -> Arc<dyn LanguageToolchainStore>;
fn fs(&self) -> Arc<dyn Fs>;
fn updated_adapters(&self) -> Arc<Mutex<HashSet<DebugAdapterName>>>;
fn update_status(&self, dap_name: DebugAdapterName, status: DapStatus);
Expand Down Expand Up @@ -137,10 +139,8 @@ pub async fn download_adapter_from_github(
&github_version.url,
);

let http_client = delegate
let mut response = delegate
.http_client()
.ok_or_else(|| anyhow!("Failed to download adapter: couldn't connect to GitHub"))?;
let mut response = http_client
.get(&github_version.url, Default::default(), true)
.await
.context("Error downloading release")?;
Expand Down Expand Up @@ -193,15 +193,11 @@ pub async fn fetch_latest_adapter_version_from_github(
github_repo: GithubRepo,
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let http_client = delegate
.http_client()
.ok_or_else(|| anyhow!("Failed to download adapter: couldn't connect to GitHub"))?;

let release = latest_github_release(
&format!("{}/{}", github_repo.repo_owner, github_repo.repo_name),
false,
false,
http_client,
delegate.http_client(),
)
.await?;

Expand All @@ -215,15 +211,12 @@ pub async fn fetch_latest_adapter_version_from_github(
pub trait DebugAdapter: 'static + Send + Sync {
fn name(&self) -> DebugAdapterName;

fn language_name(&self) -> Option<LanguageName> {
None
}

async fn get_binary(
&self,
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
if delegate
.updated_adapters()
Expand All @@ -234,7 +227,7 @@ pub trait DebugAdapter: 'static + Send + Sync {
log::info!("Using cached debug adapter binary {}", self.name());

if let Some(binary) = self
.get_installed_binary(delegate, &config, user_installed_path.clone())
.get_installed_binary(delegate, &config, user_installed_path.clone(), cx)
.await
.log_err()
{
Expand Down Expand Up @@ -264,7 +257,7 @@ pub trait DebugAdapter: 'static + Send + Sync {
.insert(self.name());
}

self.get_installed_binary(delegate, &config, user_installed_path)
self.get_installed_binary(delegate, &config, user_installed_path, cx)
.await
}

Expand All @@ -289,6 +282,7 @@ pub trait DebugAdapter: 'static + Send + Sync {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary>;

/// Should return base configuration to make the debug adapter work
Expand Down Expand Up @@ -337,6 +331,7 @@ impl DebugAdapter for FakeAdapter {
_delegate: &dyn DapDelegate,
_config: &DebugAdapterConfig,
_user_installed_path: Option<PathBuf>,
_cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
Ok(DebugAdapterBinary {
command: "command".into(),
Expand Down Expand Up @@ -366,6 +361,7 @@ impl DebugAdapter for FakeAdapter {
_delegate: &dyn DapDelegate,
_config: &DebugAdapterConfig,
_user_installed_path: Option<PathBuf>,
_cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
unimplemented!("get installed binary");
}
Expand Down
7 changes: 5 additions & 2 deletions crates/dap_adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ license = "GPL-3.0-or-later"

[features]
test-support = [
"util/test-support",
"task/test-support",
"dap/test-support",
"gpui/test-support",
"task/test-support",
"util/test-support",
]

[lints]
Expand All @@ -23,6 +24,7 @@ doctest = false
anyhow.workspace = true
async-trait.workspace = true
dap.workspace = true
gpui.workspace = true
language.workspace = true
paths.workspace = true
regex.workspace = true
Expand All @@ -34,5 +36,6 @@ util.workspace = true

[dev-dependencies]
dap = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
task = { workspace = true, features = ["test-support"] }
util = { workspace = true, features = ["test-support"] }
3 changes: 3 additions & 0 deletions crates/dap_adapters/src/custom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{ffi::OsString, path::PathBuf, sync::Arc};

use dap::transport::{StdioTransport, TcpTransport, Transport};
use gpui::AsyncAppContext;
use serde_json::Value;
use task::DebugAdapterConfig;

Expand Down Expand Up @@ -44,6 +45,7 @@ impl DebugAdapter for CustomDebugAdapter {
_: &dyn DapDelegate,
config: &DebugAdapterConfig,
_: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
Ok(DebugAdapterBinary {
command: self.custom_args.command.clone(),
Expand All @@ -70,6 +72,7 @@ impl DebugAdapter for CustomDebugAdapter {
_: &dyn DapDelegate,
_: &DebugAdapterConfig,
_: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
bail!("Custom debug adapters cannot be installed")
}
Expand Down
3 changes: 3 additions & 0 deletions crates/dap_adapters/src/gdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ffi::OsStr;
use anyhow::Result;
use async_trait::async_trait;
use dap::transport::{StdioTransport, Transport};
use gpui::AsyncAppContext;
use task::DebugAdapterConfig;

use crate::*;
Expand Down Expand Up @@ -32,6 +33,7 @@ impl DebugAdapter for GdbDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<std::path::PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let user_setting_path = user_installed_path
.filter(|p| p.exists())
Expand Down Expand Up @@ -74,6 +76,7 @@ impl DebugAdapter for GdbDebugAdapter {
_: &dyn DapDelegate,
_: &DebugAdapterConfig,
_: Option<std::path::PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
unimplemented!("GDB cannot be installed by Zed (yet)")
}
Expand Down
5 changes: 4 additions & 1 deletion crates/dap_adapters/src/go.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dap::transport::{TcpTransport, Transport};
use gpui::AsyncAppContext;
use std::{ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};

use crate::*;
Expand Down Expand Up @@ -37,8 +38,9 @@ impl DebugAdapter for GoDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
self.get_installed_binary(delegate, config, user_installed_path)
self.get_installed_binary(delegate, config, user_installed_path, cx)
.await
}

Expand Down Expand Up @@ -69,6 +71,7 @@ impl DebugAdapter for GoDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
_: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let delve_path = delegate
.which(OsStr::new("dlv"))
Expand Down
7 changes: 3 additions & 4 deletions crates/dap_adapters/src/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use adapters::latest_github_release;
use dap::transport::{TcpTransport, Transport};
use gpui::AsyncAppContext;
use regex::Regex;
use std::{collections::HashMap, net::Ipv4Addr, path::PathBuf, sync::Arc};
use sysinfo::{Pid, Process};
Expand Down Expand Up @@ -40,14 +41,11 @@ impl DebugAdapter for JsDebugAdapter {
&self,
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let http_client = delegate
.http_client()
.ok_or_else(|| anyhow!("Failed to download adapter: couldn't connect to GitHub"))?;
let release = latest_github_release(
&format!("{}/{}", "microsoft", Self::ADAPTER_NAME),
true,
false,
http_client,
delegate.http_client(),
)
.await?;

Expand All @@ -70,6 +68,7 @@ impl DebugAdapter for JsDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let adapter_path = if let Some(user_installed_path) = user_installed_path {
user_installed_path
Expand Down
3 changes: 3 additions & 0 deletions crates/dap_adapters/src/lldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{ffi::OsStr, path::PathBuf, sync::Arc};
use anyhow::Result;
use async_trait::async_trait;
use dap::transport::{StdioTransport, Transport};
use gpui::AsyncAppContext;
use task::DebugAdapterConfig;

use crate::*;
Expand Down Expand Up @@ -32,6 +33,7 @@ impl DebugAdapter for LldbDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let lldb_dap_path = if cfg!(target_os = "macos") {
util::command::new_smol_command("xcrun")
Expand Down Expand Up @@ -76,6 +78,7 @@ impl DebugAdapter for LldbDebugAdapter {
_: &dyn DapDelegate,
_: &DebugAdapterConfig,
_: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
unimplemented!("LLDB debug adapter cannot be installed by Zed (yet)")
}
Expand Down
7 changes: 3 additions & 4 deletions crates/dap_adapters/src/php.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use adapters::latest_github_release;
use dap::transport::{TcpTransport, Transport};
use gpui::AsyncAppContext;
use std::{net::Ipv4Addr, path::PathBuf, sync::Arc};

use crate::*;
Expand Down Expand Up @@ -37,14 +38,11 @@ impl DebugAdapter for PhpDebugAdapter {
&self,
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let http_client = delegate
.http_client()
.ok_or_else(|| anyhow!("Failed to download adapter: couldn't connect to GitHub"))?;
let release = latest_github_release(
&format!("{}/{}", "xdebug", Self::ADAPTER_NAME),
true,
false,
http_client,
delegate.http_client(),
)
.await?;

Expand All @@ -67,6 +65,7 @@ impl DebugAdapter for PhpDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
_: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let adapter_path = if let Some(user_installed_path) = user_installed_path {
user_installed_path
Expand Down
41 changes: 13 additions & 28 deletions crates/dap_adapters/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dap::transport::{TcpTransport, Transport};
use language::LanguageName;
use std::{ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};
use gpui::AsyncAppContext;
use std::{net::Ipv4Addr, path::PathBuf, sync::Arc};

use crate::*;

Expand Down Expand Up @@ -30,10 +30,6 @@ impl DebugAdapter for PythonDebugAdapter {
DebugAdapterName(Self::ADAPTER_NAME.into())
}

fn language_name(&self) -> Option<LanguageName> {
Some(LanguageName::new(Self::LANGUAGE_NAME))
}

fn transport(&self) -> Arc<dyn Transport> {
Arc::new(TcpTransport::new(self.host, self.port, self.timeout))
}
Expand Down Expand Up @@ -84,6 +80,7 @@ impl DebugAdapter for PythonDebugAdapter {
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
cx: &mut AsyncAppContext,
) -> Result<DebugAdapterBinary> {
let debugpy_dir = if let Some(user_installed_path) = user_installed_path {
user_installed_path
Expand All @@ -98,30 +95,18 @@ impl DebugAdapter for PythonDebugAdapter {
.ok_or_else(|| anyhow!("Debugpy directory not found"))?
};

let python_path = if let Some(toolchain) = delegate.toolchain(&self.name()) {
Some(toolchain.path.to_string())
} else {
let python_cmds = [
OsStr::new("python3"),
OsStr::new("python"),
OsStr::new("py"),
];
python_cmds
.iter()
.filter_map(|cmd| {
delegate
.which(cmd)
.and_then(|path| path.to_str().map(|str| str.to_string()))
})
.find(|_| true)
};

let python_path = python_path.ok_or(anyhow!(
"Failed to start debugger because python couldn't be found in PATH or toolchain"
))?;
let toolchain = delegate
.toolchain_store()
.active_toolchain(
delegate.worktree_id(),
language::LanguageName::new(Self::LANGUAGE_NAME),
cx,
)
.await
.ok_or(anyhow!("failed to find active toolchain for Python"))?;

Ok(DebugAdapterBinary {
command: python_path,
command: toolchain.path.to_string(),
arguments: Some(vec![
debugpy_dir.join(Self::ADAPTER_PATH).into(),
format!("--port={}", self.port).into(),
Expand Down
6 changes: 5 additions & 1 deletion crates/debugger_ui/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use gpui::{Model, TestAppContext, View, WindowHandle};
use project::Project;
use project::{Project, Worktree};
use settings::SettingsStore;
use terminal_view::terminal_panel::TerminalPanel;
use workspace::Workspace;
Expand Down Expand Up @@ -71,3 +71,7 @@ pub fn active_debug_panel_item(
})
.unwrap()
}

pub fn worktree_from_project(project: &Model<Project>, cx: &mut TestAppContext) -> Model<Worktree> {
project.read_with(cx, |project, cx| project.worktrees(cx).next().unwrap())
}
Loading

0 comments on commit fa6aec3

Please sign in to comment.