cargo.rs

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