debugger: Allow users to include PickProcessId in debug tasks and resolve Pid (#42913)

Closes #33286

This PR adds support for Zed's `$ZED_PICK_PID` command in debug
configurations, which allows users to select a process to attach to at
debug time. When this variable is present in a debug configuration, Zed
automatically opens a process picker modal.

Follow up for this will be integrating this variable in the task system
instead of just the debug configuration system.

Release Notes:

- Added `$ZED_PICK_PID` variable for debug configurations, allowing
users to select which process to attach the debugger to at runtime

---------

Co-authored-by: Remco Smits <djsmits12@gmail.com>
This commit is contained in:
Anthony Eid
2025-11-20 10:12:59 -05:00
committed by GitHub
co-authored by Remco Smits
parent e033829ef2
commit 56401fc99c
6 changed files with 413 additions and 90 deletions
+149 -11
View File
@@ -1,4 +1,8 @@
use crate::{attach_modal::Candidate, tests::start_debug_session_with, *};
use crate::{
attach_modal::{Candidate, ModalIntent},
tests::start_debug_session_with,
*,
};
use attach_modal::AttachModal;
use dap::{FakeAdapter, adapters::DebugTaskDefinition};
use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
@@ -98,12 +102,6 @@ async fn test_show_attach_modal_and_select_process(
workspace.toggle_modal(window, cx, |window, cx| {
AttachModal::with_processes(
workspace_handle,
task::ZedDebugConfig {
adapter: FakeAdapter::ADAPTER_NAME.into(),
request: dap::DebugRequest::Attach(AttachRequest::default()),
label: "attach example".into(),
stop_on_entry: None,
},
vec![
Candidate {
pid: 0,
@@ -124,6 +122,12 @@ async fn test_show_attach_modal_and_select_process(
.into_iter()
.collect(),
true,
ModalIntent::AttachToProcess(task::ZedDebugConfig {
adapter: FakeAdapter::ADAPTER_NAME.into(),
request: dap::DebugRequest::Attach(AttachRequest::default()),
label: "attach example".into(),
stop_on_entry: None,
}),
window,
cx,
)
@@ -138,8 +142,7 @@ async fn test_show_attach_modal_and_select_process(
// assert we got the expected processes
workspace
.update(cx, |_, window, cx| {
let names =
attach_modal.update(cx, |modal, cx| attach_modal::_process_names(modal, cx));
let names = attach_modal.update(cx, |modal, cx| attach_modal::process_names(modal, cx));
// Initially all processes are visible.
assert_eq!(3, names.len());
attach_modal.update(cx, |this, cx| {
@@ -153,8 +156,7 @@ async fn test_show_attach_modal_and_select_process(
// assert we got the expected processes
workspace
.update(cx, |_, _, cx| {
let names =
attach_modal.update(cx, |modal, cx| attach_modal::_process_names(modal, cx));
let names = attach_modal.update(cx, |modal, cx| attach_modal::process_names(modal, cx));
// Initially all processes are visible.
assert_eq!(2, names.len());
})
@@ -171,3 +173,139 @@ async fn test_show_attach_modal_and_select_process(
})
.unwrap();
}
#[gpui::test]
async fn test_attach_with_pick_pid_variable(executor: BackgroundExecutor, cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(executor.clone());
fs.insert_tree(
path!("/project"),
json!({
"main.rs": "First line\nSecond line\nThird line\nFourth line",
}),
)
.await;
let project = Project::test(fs, [path!("/project").as_ref()], cx).await;
let workspace = init_test_workspace(&project, cx).await;
let cx = &mut VisualTestContext::from_window(*workspace, cx);
let _initialize_subscription =
project::debugger::test::intercept_debug_sessions(cx, |client| {
client.on_request::<dap::requests::Attach, _>(move |_, args| {
let raw = &args.raw;
assert_eq!(raw["request"], "attach");
assert_eq!(
raw["process_id"], "42",
"verify process id has been replaced"
);
Ok(())
});
});
let pick_pid_placeholder = task::VariableName::PickProcessId.template_value();
workspace
.update(cx, |workspace, window, cx| {
workspace.start_debug_session(
DebugTaskDefinition {
adapter: FakeAdapter::ADAPTER_NAME.into(),
label: "attach with picker".into(),
config: json!({
"request": "attach",
"process_id": pick_pid_placeholder,
}),
tcp_connection: None,
}
.to_scenario(),
task::TaskContext::default(),
None,
None,
window,
cx,
)
})
.unwrap();
cx.run_until_parked();
let attach_modal = workspace
.update(cx, |workspace, _window, cx| {
workspace.active_modal::<AttachModal>(cx)
})
.unwrap();
assert!(
attach_modal.is_some(),
"Attach modal should open when config contains ZED_PICK_PID"
);
let attach_modal = attach_modal.unwrap();
workspace
.update(cx, |_, window, cx| {
attach_modal.update(cx, |modal, cx| {
attach_modal::set_candidates(
modal,
vec![
Candidate {
pid: 10,
name: "process-1".into(),
command: vec![],
},
Candidate {
pid: 42,
name: "target-process".into(),
command: vec![],
},
Candidate {
pid: 99,
name: "process-3".into(),
command: vec![],
},
]
.into_iter()
.collect(),
window,
cx,
)
})
})
.unwrap();
cx.run_until_parked();
workspace
.update(cx, |_, window, cx| {
attach_modal.update(cx, |modal, cx| {
modal.picker.update(cx, |picker, cx| {
picker.set_query("target", window, cx);
})
})
})
.unwrap();
cx.run_until_parked();
workspace
.update(cx, |_, _, cx| {
let names = attach_modal.update(cx, |modal, cx| attach_modal::process_names(modal, cx));
assert_eq!(names.len(), 1);
assert_eq!(names[0], " 42 target-process");
})
.unwrap();
cx.dispatch_action(Confirm);
cx.run_until_parked();
workspace
.update(cx, |workspace, _window, cx| {
assert!(
workspace.active_modal::<AttachModal>(cx).is_none(),
"Attach modal should be dismissed after selection"
);
})
.unwrap();
}