1use anyhow::{Context as _, Result};
2use async_trait::async_trait;
3use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
4use gpui::SharedString;
5use serde_json::{Value, json};
6use smol::{
7 io::AsyncReadExt,
8 process::{Command, Stdio},
9};
10use task::{BuildTaskDefinition, DebugScenario, ShellBuilder, SpawnInTerminal, TaskTemplate};
11
12pub(crate) struct CargoLocator;
13
14async fn find_best_executable(executables: &[String], test_name: &str) -> Option<String> {
15 if executables.len() == 1 {
16 return executables.first().cloned();
17 }
18 for executable in executables {
19 let Some(mut child) = Command::new(&executable)
20 .arg("--list")
21 .stdout(Stdio::piped())
22 .spawn()
23 .ok()
24 else {
25 continue;
26 };
27 let mut test_lines = String::default();
28 if let Some(mut stdout) = child.stdout.take() {
29 stdout.read_to_string(&mut test_lines).await.ok();
30 for line in test_lines.lines() {
31 if line.contains(&test_name) {
32 return Some(executable.clone());
33 }
34 }
35 }
36 }
37 None
38}
39#[async_trait]
40impl DapLocator for CargoLocator {
41 fn name(&self) -> SharedString {
42 SharedString::new_static("rust-cargo-locator")
43 }
44 async fn create_scenario(
45 &self,
46 build_config: &TaskTemplate,
47 resolved_label: &str,
48 adapter: &DebugAdapterName,
49 ) -> Option<DebugScenario> {
50 if build_config.command != "cargo" {
51 return None;
52 }
53 let mut task_template = build_config.clone();
54 let cargo_action = task_template.args.first_mut()?;
55 if cargo_action == "check" || cargo_action == "clean" {
56 return None;
57 }
58
59 match cargo_action.as_ref() {
60 "run" | "r" => {
61 *cargo_action = "build".to_owned();
62 }
63 "test" | "t" | "bench" => {
64 let delimiter = task_template
65 .args
66 .iter()
67 .position(|arg| arg == "--")
68 .unwrap_or(task_template.args.len());
69 if !task_template.args[..delimiter]
70 .iter()
71 .any(|arg| arg == "--no-run")
72 {
73 task_template.args.insert(delimiter, "--no-run".to_owned());
74 }
75 }
76 _ => {}
77 }
78
79 let config = if adapter.as_ref() == "CodeLLDB" {
80 json!({
81 "sourceLanguages": ["rust"]
82 })
83 } else {
84 Value::Null
85 };
86 Some(DebugScenario {
87 adapter: adapter.0.clone(),
88 label: resolved_label.to_string().into(),
89 build: Some(BuildTaskDefinition::Template {
90 task_template,
91 locator_name: Some(self.name()),
92 }),
93 config,
94 tcp_connection: None,
95 })
96 }
97
98 async fn run(&self, build_config: SpawnInTerminal) -> Result<DebugRequest> {
99 let cwd = build_config
100 .cwd
101 .clone()
102 .context("Couldn't get cwd from debug config which is needed for locators")?;
103 let builder = ShellBuilder::new(true, &build_config.shell).non_interactive();
104 let (program, args) = builder.build(
105 "cargo".into(),
106 &build_config
107 .args
108 .iter()
109 .cloned()
110 .take_while(|arg| arg != "--")
111 .chain(Some("--message-format=json".to_owned()))
112 .collect(),
113 );
114 let mut child = Command::new(program)
115 .args(args)
116 .envs(build_config.env.iter().map(|(k, v)| (k.clone(), v.clone())))
117 .current_dir(cwd)
118 .stdout(Stdio::piped())
119 .spawn()?;
120
121 let mut output = String::new();
122 if let Some(mut stdout) = child.stdout.take() {
123 stdout.read_to_string(&mut output).await?;
124 }
125
126 let status = child.status().await?;
127 anyhow::ensure!(status.success(), "Cargo command failed");
128
129 let executables = output
130 .lines()
131 .filter(|line| !line.trim().is_empty())
132 .filter_map(|line| serde_json::from_str(line).ok())
133 .filter_map(|json: Value| {
134 json.get("executable")
135 .and_then(Value::as_str)
136 .map(String::from)
137 })
138 .collect::<Vec<_>>();
139 anyhow::ensure!(
140 !executables.is_empty(),
141 "Couldn't get executable in cargo locator"
142 );
143 let is_test = build_config
144 .args
145 .first()
146 .map_or(false, |arg| arg == "test" || arg == "t");
147
148 let mut test_name = None;
149 if is_test {
150 test_name = build_config
151 .args
152 .iter()
153 .rev()
154 .take_while(|name| "--" != name.as_str())
155 .find(|name| !name.starts_with("-"))
156 .cloned();
157 }
158 let executable = {
159 if let Some(ref name) = test_name.as_ref().and_then(|name| {
160 name.strip_prefix('$')
161 .map(|name| build_config.env.get(name))
162 .unwrap_or(Some(name))
163 }) {
164 find_best_executable(&executables, &name).await
165 } else {
166 None
167 }
168 };
169
170 let Some(executable) = executable.or_else(|| executables.first().cloned()) else {
171 anyhow::bail!("Couldn't get executable in cargo locator");
172 };
173
174 let mut args: Vec<_> = test_name.into_iter().collect();
175 if is_test {
176 args.push("--nocapture".to_owned());
177 }
178
179 Ok(DebugRequest::Launch(task::LaunchRequest {
180 program: executable,
181 cwd: build_config.cwd,
182 args,
183 env: build_config.env.into_iter().collect(),
184 }))
185 }
186}