debugger: Run build in terminal (#29645)

Currently contains the pre-work of making sessions creatable without a
definition, but still need to change the spawn in terminal
to use the running session

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
Conrad Irwin 2025-05-05 21:08:14 +01:00 committed by GitHub
parent c12e6376b8
commit ff215b4f11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 695 additions and 622 deletions

View file

@ -15,7 +15,9 @@ use breakpoint_list::BreakpointList;
use collections::{HashMap, IndexMap};
use console::Console;
use dap::{
Capabilities, RunInTerminalRequestArguments, Thread, client::SessionId,
Capabilities, RunInTerminalRequestArguments, Thread,
adapters::{DebugAdapterName, DebugTaskDefinition},
client::SessionId,
debugger_settings::DebuggerSettings,
};
use futures::{SinkExt, channel::mpsc};
@ -23,6 +25,7 @@ use gpui::{
Action as _, AnyView, AppContext, Entity, EntityId, EventEmitter, FocusHandle, Focusable,
NoAction, Pixels, Point, Subscription, Task, WeakEntity,
};
use language::Buffer;
use loaded_source_list::LoadedSourceList;
use module_list::ModuleList;
use project::{
@ -34,6 +37,10 @@ use rpc::proto::ViewId;
use serde_json::Value;
use settings::Settings;
use stack_frame_list::StackFrameList;
use task::{
DebugScenario, LaunchRequest, TaskContext, substitute_variables_in_map,
substitute_variables_in_str,
};
use terminal_view::TerminalView;
use ui::{
ActiveTheme, AnyElement, App, ButtonCommon as _, Clickable as _, Context, ContextMenu,
@ -667,6 +674,143 @@ impl RunningState {
self.panes.pane_at_pixel_position(position).is_some()
}
pub(crate) fn resolve_scenario(
&self,
scenario: DebugScenario,
task_context: TaskContext,
buffer: Option<Entity<Buffer>>,
window: &Window,
cx: &mut Context<Self>,
) -> Task<Result<DebugTaskDefinition>> {
let Some(workspace) = self.workspace.upgrade() else {
return Task::ready(Err(anyhow!("no workspace")));
};
let project = workspace.read(cx).project().clone();
let dap_store = project.read(cx).dap_store().downgrade();
let task_store = project.read(cx).task_store().downgrade();
let weak_project = project.downgrade();
let weak_workspace = workspace.downgrade();
cx.spawn_in(window, async move |this, cx| {
let DebugScenario {
adapter,
label,
build,
request,
initialize_args,
tcp_connection,
stop_on_entry,
} = scenario;
let request = if let Some(request) = request {
request
} else if let Some(build) = build {
let Some(task) = task_store.update(cx, |this, cx| {
this.task_inventory().and_then(|inventory| {
inventory
.read(cx)
.task_template_by_label(buffer, &build, cx)
})
})?
else {
anyhow::bail!("Couldn't find task template for {:?}", build)
};
let Some(task) = task.resolve_task("debug-build-task", &task_context) else {
anyhow::bail!("Could not resolve task variables within a debug scenario");
};
let terminal = project
.update_in(cx, |project, window, cx| {
project.create_terminal(
TerminalKind::Task(task.resolved.clone()),
window.window_handle(),
cx,
)
})?
.await?;
let terminal_view = cx.new_window_entity(|window, cx| {
TerminalView::new(
terminal.clone(),
weak_workspace,
None,
weak_project,
false,
window,
cx,
)
})?;
this.update_in(cx, |this, window, cx| {
this.ensure_pane_item(DebuggerPaneItem::Terminal, window, cx);
this.debug_terminal.update(cx, |debug_terminal, cx| {
debug_terminal.terminal = Some(terminal_view);
cx.notify();
});
})?;
let exit_status = terminal
.read_with(cx, |terminal, cx| terminal.wait_for_completed_task(cx))?
.await
.ok_or_else(|| anyhow!("Failed to wait for completed task"))?;
if !exit_status.success() {
anyhow::bail!("Build failed");
}
dap_store
.update(cx, |this, cx| this.run_debug_locator(task.resolved, cx))?
.await?
} else {
return Err(anyhow!("No request or build provided"));
};
let request = match request {
dap::DebugRequest::Launch(launch_request) => {
let cwd = match launch_request.cwd.as_deref().and_then(|path| path.to_str()) {
Some(cwd) => {
let substituted_cwd = substitute_variables_in_str(&cwd, &task_context)
.ok_or_else(|| anyhow!("Failed to substitute variables in cwd"))?;
Some(PathBuf::from(substituted_cwd))
}
None => None,
};
let env = substitute_variables_in_map(
&launch_request.env.into_iter().collect(),
&task_context,
)
.ok_or_else(|| anyhow!("Failed to substitute variables in env"))?
.into_iter()
.collect();
let new_launch_request = LaunchRequest {
program: substitute_variables_in_str(
&launch_request.program,
&task_context,
)
.ok_or_else(|| anyhow!("Failed to substitute variables in program"))?,
args: launch_request
.args
.into_iter()
.map(|arg| substitute_variables_in_str(&arg, &task_context))
.collect::<Option<Vec<_>>>()
.ok_or_else(|| anyhow!("Failed to substitute variables in args"))?,
cwd,
env,
};
dap::DebugRequest::Launch(new_launch_request)
}
request @ dap::DebugRequest::Attach(_) => request,
};
Ok(DebugTaskDefinition {
label,
adapter: DebugAdapterName(adapter),
request,
initialize_args,
stop_on_entry,
tcp_connection,
})
})
}
fn handle_run_in_terminal(
&self,
request: &RunInTerminalRequestArguments,
@ -914,7 +1058,7 @@ impl RunningState {
let Some((adapter_name, pane_group)) = this
.update(cx, |this, cx| {
let adapter_name = this.session.read(cx).adapter_name();
let adapter_name = this.session.read(cx).adapter();
(
adapter_name,
persistence::build_serialized_pane_layout(&this.panes.root, cx),