Skip to content

Commit ee56706

Browse files
debugger: Fix up Rust test tasks definitions (#30232)
Closes #ISSUE Release Notes: - N/A --------- Co-authored-by: Conrad Irwin <[email protected]>
1 parent 3cc8850 commit ee56706

File tree

8 files changed

+53
-27
lines changed

8 files changed

+53
-27
lines changed

crates/dap/src/adapters.rs

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ impl From<DebugAdapterName> for SharedString {
7878
name.0
7979
}
8080
}
81+
impl From<SharedString> for DebugAdapterName {
82+
fn from(name: SharedString) -> Self {
83+
DebugAdapterName(name)
84+
}
85+
}
8186

8287
impl<'a> From<&'a str> for DebugAdapterName {
8388
fn from(str: &'a str) -> DebugAdapterName {

crates/dap/src/registry.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ use std::{collections::BTreeMap, sync::Arc};
1616
pub trait DapLocator: Send + Sync {
1717
fn name(&self) -> SharedString;
1818
/// Determines whether this locator can generate debug target for given task.
19-
fn create_scenario(&self, build_config: &TaskTemplate, adapter: &str) -> Option<DebugScenario>;
19+
fn create_scenario(
20+
&self,
21+
build_config: &TaskTemplate,
22+
resolved_label: &str,
23+
adapter: DebugAdapterName,
24+
) -> Option<DebugScenario>;
2025

2126
async fn run(&self, build_config: SpawnInTerminal) -> Result<DebugRequest>;
2227
}

crates/debugger_ui/src/session/running.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -731,30 +731,37 @@ impl RunningState {
731731
(task, None)
732732
}
733733
};
734+
let Some(task) = task.resolve_task("debug-build-task", &task_context) else {
735+
anyhow::bail!("Could not resolve task variables within a debug scenario");
736+
};
737+
734738
let locator_name = if let Some(locator_name) = locator_name {
735739
debug_assert!(request.is_none());
736740
Some(locator_name)
737741
} else if request.is_none() {
738742
dap_store
739743
.update(cx, |this, cx| {
740-
this.debug_scenario_for_build_task(task.clone(), adapter.clone(), cx)
741-
.and_then(|scenario| match scenario.build {
744+
this.debug_scenario_for_build_task(
745+
task.original_task().clone(),
746+
adapter.clone().into(),
747+
task.display_label().to_owned().into(),
748+
cx,
749+
)
750+
.and_then(|scenario| {
751+
match scenario.build {
742752
Some(BuildTaskDefinition::Template {
743753
locator_name, ..
744754
}) => locator_name,
745755
_ => None,
746-
})
756+
}
757+
})
747758
})
748759
.ok()
749760
.flatten()
750761
} else {
751762
None
752763
};
753764

754-
let Some(task) = task.resolve_task("debug-build-task", &task_context) else {
755-
anyhow::bail!("Could not resolve task variables within a debug scenario");
756-
};
757-
758765
let builder = ShellBuilder::new(is_local, &task.resolved.shell);
759766
let command_label = builder.command_label(&task.resolved.command_label);
760767
let (command, args) =

crates/editor/src/editor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5276,7 +5276,8 @@ impl Editor {
52765276
if let Some(scenario) = this
52775277
.debug_scenario_for_build_task(
52785278
task.original_task().clone(),
5279-
debug_adapter.clone(),
5279+
debug_adapter.clone().into(),
5280+
task.display_label().to_owned().into(),
52805281
cx,
52815282
)
52825283
{

crates/languages/src/rust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,8 @@ impl ContextProvider for RustContextProvider {
685685
"-p".into(),
686686
RUST_PACKAGE_TASK_VARIABLE.template_value(),
687687
"--".into(),
688-
RUST_TEST_NAME_TASK_VARIABLE.template_value(),
689688
"--nocapture".into(),
689+
RUST_TEST_NAME_TASK_VARIABLE.template_value(),
690690
],
691691
tags: vec!["rust-test".to_owned()],
692692
cwd: Some("$ZED_DIRNAME".to_owned()),

crates/project/src/debugger/dap_store.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,14 @@ impl DapStore {
283283
pub fn debug_scenario_for_build_task(
284284
&self,
285285
build: TaskTemplate,
286-
adapter: SharedString,
286+
adapter: DebugAdapterName,
287+
label: SharedString,
287288
cx: &mut App,
288289
) -> Option<DebugScenario> {
289290
DapRegistry::global(cx)
290291
.locators()
291292
.values()
292-
.find_map(|locator| locator.create_scenario(&build, &adapter))
293+
.find_map(|locator| locator.create_scenario(&build, &label, adapter.clone()))
293294
}
294295

295296
pub fn run_debug_locator(

crates/project/src/debugger/locators/cargo.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Result, anyhow};
22
use async_trait::async_trait;
3-
use dap::{DapLocator, DebugRequest};
3+
use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
44
use gpui::SharedString;
55
use serde_json::Value;
66
use smol::{
@@ -41,7 +41,12 @@ impl DapLocator for CargoLocator {
4141
fn name(&self) -> SharedString {
4242
SharedString::new_static("rust-cargo-locator")
4343
}
44-
fn create_scenario(&self, build_config: &TaskTemplate, adapter: &str) -> Option<DebugScenario> {
44+
fn create_scenario(
45+
&self,
46+
build_config: &TaskTemplate,
47+
resolved_label: &str,
48+
adapter: DebugAdapterName,
49+
) -> Option<DebugScenario> {
4550
if build_config.command != "cargo" {
4651
return None;
4752
}
@@ -70,9 +75,9 @@ impl DapLocator for CargoLocator {
7075
}
7176
_ => {}
7277
}
73-
let label = format!("Debug `{}`", build_config.label);
78+
let label = format!("Debug `{resolved_label}`");
7479
Some(DebugScenario {
75-
adapter: adapter.to_owned().into(),
80+
adapter: adapter.0,
7681
label: SharedString::from(label),
7782
build: Some(BuildTaskDefinition::Template {
7883
task_template,
@@ -136,20 +141,20 @@ impl DapLocator for CargoLocator {
136141

137142
let mut test_name = None;
138143
if is_test {
139-
if let Some(package_index) = build_config
144+
test_name = build_config
140145
.args
141146
.iter()
142-
.position(|arg| arg == "-p" || arg == "--package")
143-
{
144-
test_name = build_config
145-
.args
146-
.get(package_index + 2)
147-
.filter(|name| !name.starts_with("--"))
148-
.cloned();
149-
}
147+
.rev()
148+
.take_while(|name| "--" != name.as_str())
149+
.find(|name| !name.starts_with("-"))
150+
.cloned();
150151
}
151152
let executable = {
152-
if let Some(ref name) = test_name {
153+
if let Some(ref name) = test_name.as_ref().and_then(|name| {
154+
name.strip_prefix('$')
155+
.map(|name| build_config.env.get(name))
156+
.unwrap_or(Some(name))
157+
}) {
153158
find_best_executable(&executables, &name).await
154159
} else {
155160
None

crates/project/src/lsp_store/lsp_ext_command.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,9 @@ impl LspCommand for GetLspRunnables {
663663
// We cannot escape all shell arguments unconditionally, as we use this for ssh commands, which may involve paths starting with `~`.
664664
// That bit is not auto-expanded when using single quotes.
665665
// Escape extra cargo args unconditionally as those are unlikely to contain `~`.
666-
.map(|extra_arg| format!("'{extra_arg}'")),
666+
.flat_map(|extra_arg| {
667+
shlex::try_quote(&extra_arg).ok().map(|s| s.to_string())
668+
}),
667669
);
668670
}
669671
}

0 commit comments

Comments
 (0)