cargo.rs

  1use super::DapLocator;
  2use anyhow::{Result, anyhow};
  3use async_trait::async_trait;
  4use serde_json::Value;
  5use smol::{
  6    io::AsyncReadExt,
  7    process::{Command, Stdio},
  8};
  9use task::DebugTaskDefinition;
 10
 11pub(super) 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) = Command::new(&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        if let Some(mut stdout) = child.stdout.take() {
 28            stdout.read_to_string(&mut test_lines).await.ok();
 29            for line in test_lines.lines() {
 30                if line.contains(&test_name) {
 31                    return Some(executable.clone());
 32                }
 33            }
 34        }
 35    }
 36    None
 37}
 38#[async_trait]
 39impl DapLocator for CargoLocator {
 40    async fn run_locator(&self, debug_config: &mut DebugTaskDefinition) -> Result<()> {
 41        let Some(launch_config) = (match &mut debug_config.request {
 42            task::DebugRequestType::Launch(launch_config) => Some(launch_config),
 43            _ => None,
 44        }) else {
 45            return Err(anyhow!("Couldn't get launch config in locator"));
 46        };
 47
 48        let Some(cwd) = launch_config.cwd.clone() else {
 49            return Err(anyhow!(
 50                "Couldn't get cwd from debug config which is needed for locators"
 51            ));
 52        };
 53
 54        let mut child = Command::new("cargo")
 55            .args(&launch_config.args)
 56            .arg("--message-format=json")
 57            .current_dir(cwd)
 58            .stdout(Stdio::piped())
 59            .spawn()?;
 60
 61        let mut output = String::new();
 62        if let Some(mut stdout) = child.stdout.take() {
 63            stdout.read_to_string(&mut output).await?;
 64        }
 65
 66        let status = child.status().await?;
 67        if !status.success() {
 68            return Err(anyhow::anyhow!("Cargo command failed"));
 69        }
 70
 71        let executables = output
 72            .lines()
 73            .filter(|line| !line.trim().is_empty())
 74            .filter_map(|line| serde_json::from_str(line).ok())
 75            .filter_map(|json: Value| {
 76                json.get("executable")
 77                    .and_then(Value::as_str)
 78                    .map(String::from)
 79            })
 80            .collect::<Vec<_>>();
 81        if executables.is_empty() {
 82            return Err(anyhow!("Couldn't get executable in cargo locator"));
 83        };
 84
 85        let is_test = launch_config
 86            .args
 87            .first()
 88            .map_or(false, |arg| arg == "test");
 89
 90        let mut test_name = None;
 91        if is_test {
 92            if let Some(package_index) = launch_config
 93                .args
 94                .iter()
 95                .position(|arg| arg == "-p" || arg == "--package")
 96            {
 97                test_name = launch_config
 98                    .args
 99                    .get(package_index + 2)
100                    .filter(|name| !name.starts_with("--"))
101                    .cloned();
102            }
103        }
104        let executable = {
105            if let Some(ref name) = test_name {
106                find_best_executable(&executables, &name).await
107            } else {
108                None
109            }
110        };
111
112        let Some(executable) = executable.or_else(|| executables.first().cloned()) else {
113            return Err(anyhow!("Couldn't get executable in cargo locator"));
114        };
115
116        launch_config.program = executable;
117
118        launch_config.args.clear();
119        if let Some(test_name) = test_name {
120            launch_config.args.push(test_name);
121        }
122        Ok(())
123    }
124}