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