debugger: More tidy up for SSH (#28993)

Split `locator` out of DebugTaskDefinition to make it clearer when
location needs to happen.

Release Notes:

- N/A

---------

Co-authored-by: Anthony Eid <hello@anthonyeid.me>
Co-authored-by: Anthony <anthony@zed.dev>
Co-authored-by: Cole Miller <m@cole-miller.net>
This commit is contained in:
Conrad Irwin 2025-04-21 10:00:03 -06:00 committed by GitHub
parent d13cd007a2
commit 9d35f0389d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
57 changed files with 1146 additions and 884 deletions

View file

@ -35,7 +35,6 @@ client.workspace = true
clock.workspace = true
collections.workspace = true
component.workspace = true
dap.workspace = true
db.workspace = true
derive_more.workspace = true
fs.workspace = true

View file

@ -1,75 +1,132 @@
use gpui::Context;
use std::process::ExitStatus;
use anyhow::{Result, anyhow};
use gpui::{Context, Task};
use project::TaskSourceKind;
use remote::ConnectionState;
use task::{ResolvedTask, TaskContext, TaskTemplate};
use task::{ResolvedTask, SpawnInTerminal, TaskContext, TaskTemplate};
use ui::Window;
use crate::Workspace;
pub fn schedule_task(
workspace: &mut Workspace,
task_source_kind: TaskSourceKind,
task_to_resolve: &TaskTemplate,
task_cx: &TaskContext,
omit_history: bool,
cx: &mut Context<Workspace>,
) {
match workspace.project.read(cx).ssh_connection_state(cx) {
None | Some(ConnectionState::Connected) => {}
Some(
ConnectionState::Connecting
| ConnectionState::Disconnected
| ConnectionState::HeartbeatMissed
| ConnectionState::Reconnecting,
) => {
log::warn!("Cannot schedule tasks when disconnected from a remote host");
impl Workspace {
pub fn schedule_task(
self: &mut Workspace,
task_source_kind: TaskSourceKind,
task_to_resolve: &TaskTemplate,
task_cx: &TaskContext,
omit_history: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
match self.project.read(cx).ssh_connection_state(cx) {
None | Some(ConnectionState::Connected) => {}
Some(
ConnectionState::Connecting
| ConnectionState::Disconnected
| ConnectionState::HeartbeatMissed
| ConnectionState::Reconnecting,
) => {
log::warn!("Cannot schedule tasks when disconnected from a remote host");
return;
}
}
if let Some(spawn_in_terminal) =
task_to_resolve.resolve_task(&task_source_kind.to_id_base(), task_cx)
{
self.schedule_resolved_task(
task_source_kind,
spawn_in_terminal,
omit_history,
window,
cx,
);
}
}
pub fn schedule_resolved_task(
self: &mut Workspace,
task_source_kind: TaskSourceKind,
mut resolved_task: ResolvedTask,
omit_history: bool,
window: &mut Window,
cx: &mut Context<Workspace>,
) {
if let Some(spawn_in_terminal) = resolved_task.resolved.take() {
if !omit_history {
resolved_task.resolved = Some(spawn_in_terminal.clone());
self.project().update(cx, |project, cx| {
if let Some(task_inventory) =
project.task_store().read(cx).task_inventory().cloned()
{
task_inventory.update(cx, |inventory, _| {
inventory.task_scheduled(task_source_kind, resolved_task);
})
}
});
}
if let Some(terminal_provider) = self.terminal_provider.as_ref() {
terminal_provider
.spawn(spawn_in_terminal, window, cx)
.detach_and_log_err(cx);
}
}
}
pub fn schedule_debug_task(
&mut self,
task: ResolvedTask,
window: &mut Window,
cx: &mut Context<Workspace>,
) {
let Some(debug_config) = task.resolved_debug_adapter_config() else {
log::error!("Debug task has no debug adapter config");
return;
}
}
};
if let Some(spawn_in_terminal) =
task_to_resolve.resolve_task(&task_source_kind.to_id_base(), task_cx)
{
schedule_resolved_task(
workspace,
task_source_kind,
spawn_in_terminal,
omit_history,
cx,
);
}
}
let project = self.project().clone();
cx.spawn_in(window, async move |workspace, cx| {
let config = if debug_config.locator.is_some() {
let task = workspace.update_in(cx, |workspace, window, cx| {
workspace.spawn_in_terminal(task.resolved.unwrap(), window, cx)
})?;
pub fn schedule_resolved_task(
workspace: &mut Workspace,
task_source_kind: TaskSourceKind,
mut resolved_task: ResolvedTask,
omit_history: bool,
cx: &mut Context<Workspace>,
) {
let debug_config = resolved_task.resolved_debug_adapter_config();
if let Some(spawn_in_terminal) = resolved_task.resolved.take() {
if let Some(debug_config) = debug_config {
workspace
.debug_task_queue
.insert(resolved_task.id.clone(), debug_config);
}
if !omit_history {
resolved_task.resolved = Some(spawn_in_terminal.clone());
workspace.project().update(cx, |project, cx| {
if let Some(task_inventory) =
project.task_store().read(cx).task_inventory().cloned()
{
task_inventory.update(cx, |inventory, _| {
inventory.task_scheduled(task_source_kind, resolved_task);
})
let exit_code = task.await?;
if !exit_code.success() {
return anyhow::Ok(());
}
});
}
let ret = project
.update(cx, |project, cx| {
project.dap_store().update(cx, |dap_store, cx| {
dap_store.run_debug_locator(debug_config, cx)
})
})?
.await?;
ret
} else {
debug_config.definition
};
cx.emit(crate::Event::SpawnTask {
action: Box::new(spawn_in_terminal),
});
project
.update(cx, |project, cx| project.start_debug_session(config, cx))?
.await?;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}
pub fn spawn_in_terminal(
self: &mut Workspace,
spawn_in_terminal: SpawnInTerminal,
window: &mut Window,
cx: &mut Context<Workspace>,
) -> Task<Result<ExitStatus>> {
if let Some(terminal_provider) = self.terminal_provider.as_ref() {
terminal_provider.spawn(spawn_in_terminal, window, cx)
} else {
Task::ready(Err(anyhow!("No terminal provider")))
}
}
}

View file

@ -15,7 +15,6 @@ mod toast_layer;
mod toolbar;
mod workspace_settings;
use dap::DapRegistry;
pub use toast_layer::{RunAction, ToastAction, ToastLayer, ToastView};
use anyhow::{Context as _, Result, anyhow};
@ -92,11 +91,12 @@ use std::{
env,
hash::{Hash, Hasher},
path::{Path, PathBuf},
process::ExitStatus,
rc::Rc,
sync::{Arc, LazyLock, Weak, atomic::AtomicUsize},
time::Duration,
};
use task::{DebugTaskDefinition, SpawnInTerminal, TaskId};
use task::SpawnInTerminal;
use theme::{ActiveTheme, SystemAppearance, ThemeSettings};
pub use toolbar::{Toolbar, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
pub use ui;
@ -130,6 +130,15 @@ static ZED_WINDOW_POSITION: LazyLock<Option<Point<Pixels>>> = LazyLock::new(|| {
.and_then(parse_pixel_position_env_var)
});
pub trait TerminalProvider {
fn spawn(
&self,
task: SpawnInTerminal,
window: &mut Window,
cx: &mut App,
) -> Task<Result<ExitStatus>>;
}
actions!(
workspace,
[
@ -626,7 +635,6 @@ pub fn register_serializable_item<I: SerializableItem>(cx: &mut App) {
pub struct AppState {
pub languages: Arc<LanguageRegistry>,
pub debug_adapters: Arc<DapRegistry>,
pub client: Arc<Client>,
pub user_store: Entity<UserStore>,
pub workspace_store: Entity<WorkspaceStore>,
@ -678,7 +686,6 @@ impl AppState {
let fs = fs::FakeFs::new(cx.background_executor().clone());
let languages = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
let debug_adapters = Arc::new(DapRegistry::fake());
let clock = Arc::new(clock::FakeSystemClock::new());
let http_client = http_client::FakeHttpClient::with_404_response();
let client = Client::new(clock, http_client.clone(), cx);
@ -694,7 +701,6 @@ impl AppState {
client,
fs,
languages,
debug_adapters,
user_store,
workspace_store,
node_runtime: NodeRuntime::unavailable(),
@ -772,9 +778,6 @@ pub enum Event {
},
ContactRequestedJoin(u64),
WorkspaceCreated(WeakEntity<Workspace>),
SpawnTask {
action: Box<SpawnInTerminal>,
},
OpenBundledFile {
text: Cow<'static, str>,
title: &'static str,
@ -856,11 +859,11 @@ pub struct Workspace {
bounds_save_task_queued: Option<Task<()>>,
on_prompt_for_new_path: Option<PromptForNewPath>,
on_prompt_for_open_path: Option<PromptForOpenPath>,
terminal_provider: Option<Box<dyn TerminalProvider>>,
serializable_items_tx: UnboundedSender<Box<dyn SerializableItemHandle>>,
serialized_ssh_project: Option<SerializedSshProject>,
_items_serializer: Task<Result<()>>,
session_id: Option<String>,
debug_task_queue: HashMap<task::TaskId, DebugTaskDefinition>,
}
impl EventEmitter<Event> for Workspace {}
@ -1182,11 +1185,11 @@ impl Workspace {
bounds_save_task_queued: None,
on_prompt_for_new_path: None,
on_prompt_for_open_path: None,
terminal_provider: None,
serializable_items_tx,
_items_serializer,
session_id: Some(session_id),
serialized_ssh_project: None,
debug_task_queue: Default::default(),
}
}
@ -1207,7 +1210,6 @@ impl Workspace {
app_state.node_runtime.clone(),
app_state.user_store.clone(),
app_state.languages.clone(),
app_state.debug_adapters.clone(),
app_state.fs.clone(),
env,
cx,
@ -1699,6 +1701,10 @@ impl Workspace {
self.on_prompt_for_open_path = Some(prompt)
}
pub fn set_terminal_provider(&mut self, provider: impl TerminalProvider + 'static) {
self.terminal_provider = Some(Box::new(provider));
}
pub fn serialized_ssh_project(&self) -> Option<SerializedSshProject> {
self.serialized_ssh_project.clone()
}
@ -5082,7 +5088,6 @@ impl Workspace {
window.activate_window();
let app_state = Arc::new(AppState {
languages: project.read(cx).languages().clone(),
debug_adapters: project.read(cx).debug_adapters().clone(),
workspace_store,
client,
user_store,
@ -5238,16 +5243,6 @@ impl Workspace {
.update(cx, |_, window, _| window.activate_window())
.ok();
}
pub fn debug_task_ready(&mut self, task_id: &TaskId, cx: &mut App) {
if let Some(debug_config) = self.debug_task_queue.remove(task_id) {
self.project.update(cx, |project, cx| {
project
.start_debug_session(debug_config, cx)
.detach_and_log_err(cx);
})
}
}
}
fn leader_border_for_pane(