1use super::DapLocator;
2use anyhow::{Result, anyhow};
3use async_trait::async_trait;
4use serde_json::{Value, json};
5use smol::{
6 io::AsyncReadExt,
7 process::{Command, Stdio},
8};
9use task::DebugTaskDefinition;
10use util::maybe;
11
12pub(super) 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 async fn run_locator(&self, debug_config: &mut DebugTaskDefinition) -> Result<()> {
42 let Some(launch_config) = (match &mut debug_config.request {
43 task::DebugRequestType::Launch(launch_config) => Some(launch_config),
44 _ => None,
45 }) else {
46 return Err(anyhow!("Couldn't get launch config in locator"));
47 };
48
49 let Some(cwd) = launch_config.cwd.clone() else {
50 return Err(anyhow!(
51 "Couldn't get cwd from debug config which is needed for locators"
52 ));
53 };
54
55 let mut child = Command::new("cargo")
56 .args(&launch_config.args)
57 .arg("--message-format=json")
58 .current_dir(cwd)
59 .stdout(Stdio::piped())
60 .spawn()?;
61
62 let mut output = String::new();
63 if let Some(mut stdout) = child.stdout.take() {
64 stdout.read_to_string(&mut output).await?;
65 }
66
67 let status = child.status().await?;
68 if !status.success() {
69 return Err(anyhow::anyhow!("Cargo command failed"));
70 }
71
72 let executables = output
73 .lines()
74 .filter(|line| !line.trim().is_empty())
75 .filter_map(|line| serde_json::from_str(line).ok())
76 .filter_map(|json: Value| {
77 json.get("executable")
78 .and_then(Value::as_str)
79 .map(String::from)
80 })
81 .collect::<Vec<_>>();
82 if executables.is_empty() {
83 return Err(anyhow!("Couldn't get executable in cargo locator"));
84 };
85
86 let is_test = launch_config
87 .args
88 .first()
89 .map_or(false, |arg| arg == "test");
90
91 let mut test_name = None;
92 if is_test {
93 if let Some(package_index) = launch_config
94 .args
95 .iter()
96 .position(|arg| arg == "-p" || arg == "--package")
97 {
98 test_name = launch_config
99 .args
100 .get(package_index + 2)
101 .filter(|name| !name.starts_with("--"))
102 .cloned();
103 }
104 }
105 let executable = {
106 if let Some(ref name) = test_name {
107 find_best_executable(&executables, &name).await
108 } else {
109 None
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 if debug_config.adapter == "LLDB" && debug_config.initialize_args.is_none() {
119 // Find Rust pretty-printers in current toolchain's sysroot
120 let cwd = launch_config.cwd.clone();
121 debug_config.initialize_args = maybe!(async move {
122 let cwd = cwd?;
123
124 let output = Command::new("rustc")
125 .arg("--print")
126 .arg("sysroot")
127 .current_dir(cwd)
128 .output()
129 .await
130 .ok()?;
131
132 if !output.status.success() {
133 return None;
134 }
135
136 let sysroot_path = String::from_utf8(output.stdout).ok()?;
137 let sysroot_path = sysroot_path.trim_end();
138 let first_command = format!(
139 r#"command script import "{sysroot_path}/lib/rustlib/etc/lldb_lookup.py"#
140 );
141 let second_command =
142 format!(r#"command source -s 0 '{sysroot_path}/lib/rustlib/etc/lldb_commands"#);
143
144 Some(json!({"initCommands": [first_command, second_command]}))
145 })
146 .await;
147 }
148
149 launch_config.args.clear();
150 if let Some(test_name) = test_name {
151 launch_config.args.push(test_name);
152 }
153 Ok(())
154 }
155}