1use anyhow::Result;
2use async_trait::async_trait;
3use collections::HashMap;
4use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
5use gpui::SharedString;
6use serde::{Deserialize, Serialize};
7use task::{DebugScenario, SpawnInTerminal, TaskTemplate};
8
9pub(crate) struct GoLocator;
10
11#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
12#[serde(rename_all = "camelCase")]
13struct DelveLaunchRequest {
14 request: String,
15 mode: String,
16 program: String,
17 #[serde(skip_serializing_if = "Option::is_none")]
18 cwd: Option<String>,
19 args: Vec<String>,
20 build_flags: Vec<String>,
21 env: HashMap<String, String>,
22}
23
24fn is_debug_flag(arg: &str) -> Option<bool> {
25 let mut part = if let Some(suffix) = arg.strip_prefix("test.") {
26 suffix
27 } else {
28 arg
29 };
30 let mut might_have_arg = true;
31 if let Some(idx) = part.find('=') {
32 might_have_arg = false;
33 part = &part[..idx];
34 }
35 match part {
36 "benchmem" | "failfast" | "fullpath" | "fuzzworker" | "json" | "short" | "v"
37 | "paniconexit0" => Some(false),
38 "bench"
39 | "benchtime"
40 | "blockprofile"
41 | "blockprofilerate"
42 | "count"
43 | "coverprofile"
44 | "cpu"
45 | "cpuprofile"
46 | "fuzz"
47 | "fuzzcachedir"
48 | "fuzzminimizetime"
49 | "fuzztime"
50 | "gocoverdir"
51 | "list"
52 | "memprofile"
53 | "memprofilerate"
54 | "mutexprofile"
55 | "mutexprofilefraction"
56 | "outputdir"
57 | "parallel"
58 | "run"
59 | "shuffle"
60 | "skip"
61 | "testlogfile"
62 | "timeout"
63 | "trace" => Some(might_have_arg),
64 _ if arg.starts_with("test.") => Some(false),
65 _ => None,
66 }
67}
68
69fn is_build_flag(mut arg: &str) -> Option<bool> {
70 let mut might_have_arg = true;
71 if let Some(idx) = arg.find('=') {
72 might_have_arg = false;
73 arg = &arg[..idx];
74 }
75 match arg {
76 "a" | "n" | "race" | "msan" | "asan" | "cover" | "work" | "x" | "v" | "buildvcs"
77 | "json" | "linkshared" | "modcacherw" | "trimpath" => Some(false),
78
79 "p" | "covermode" | "coverpkg" | "asmflags" | "buildmode" | "compiler" | "gccgoflags"
80 | "gcflags" | "installsuffix" | "ldflags" | "mod" | "modfile" | "overlay" | "pgo"
81 | "pkgdir" | "tags" | "toolexec" => Some(might_have_arg),
82 _ => None,
83 }
84}
85
86#[async_trait]
87impl DapLocator for GoLocator {
88 fn name(&self) -> SharedString {
89 SharedString::new_static("go-debug-locator")
90 }
91
92 async fn create_scenario(
93 &self,
94 build_config: &TaskTemplate,
95 resolved_label: &str,
96 adapter: &DebugAdapterName,
97 ) -> Option<DebugScenario> {
98 if build_config.command != "go" {
99 return None;
100 }
101 let go_action = build_config.args.first()?;
102
103 match go_action.as_str() {
104 "test" => {
105 let mut program = ".".to_string();
106 let mut args = Vec::default();
107 let mut build_flags = Vec::default();
108
109 let mut all_args_are_test = false;
110 let mut next_arg_is_test = false;
111 let mut next_arg_is_build = false;
112 let mut seen_pkg = false;
113 let mut seen_v = false;
114
115 for arg in build_config.args.iter().skip(1) {
116 if all_args_are_test || next_arg_is_test {
117 // HACK: tasks assume that they are run in a shell context,
118 // so the -run regex has escaped specials. Delve correctly
119 // handles escaping, so we undo that here.
120 if let Some((left, right)) = arg.split_once("/")
121 && left.starts_with("\\^")
122 && left.ends_with("\\$")
123 && right.starts_with("\\^")
124 && right.ends_with("\\$")
125 {
126 let mut left = left[1..left.len() - 2].to_string();
127 left.push('$');
128
129 let mut right = right[1..right.len() - 2].to_string();
130 right.push('$');
131
132 args.push(format!("{left}/{right}"));
133 } else if arg.starts_with("\\^") && arg.ends_with("\\$") {
134 let mut arg = arg[1..arg.len() - 2].to_string();
135 arg.push('$');
136 args.push(arg);
137 } else {
138 args.push(arg.clone());
139 }
140 next_arg_is_test = false;
141 } else if next_arg_is_build {
142 build_flags.push(arg.clone());
143 next_arg_is_build = false;
144 } else if arg.starts_with('-') {
145 let flag = arg.trim_start_matches('-');
146 if flag == "args" {
147 all_args_are_test = true;
148 } else if let Some(has_arg) = is_debug_flag(flag) {
149 if flag == "v" || flag == "test.v" {
150 seen_v = true;
151 }
152 if flag.starts_with("test.") {
153 args.push(arg.clone());
154 } else {
155 args.push(format!("-test.{flag}"))
156 }
157 next_arg_is_test = has_arg;
158 } else if let Some(has_arg) = is_build_flag(flag) {
159 build_flags.push(arg.clone());
160 next_arg_is_build = has_arg;
161 }
162 } else if !seen_pkg {
163 program = arg.clone();
164 seen_pkg = true;
165 } else {
166 args.push(arg.clone());
167 }
168 }
169 if !seen_v {
170 args.push("-test.v".to_string());
171 }
172
173 let config: serde_json::Value = serde_json::to_value(DelveLaunchRequest {
174 request: "launch".to_string(),
175 mode: "test".to_string(),
176 program,
177 args,
178 build_flags,
179 cwd: build_config.cwd.clone(),
180 env: build_config.env.clone(),
181 })
182 .unwrap();
183
184 Some(DebugScenario {
185 label: resolved_label.to_string().into(),
186 adapter: adapter.0.clone(),
187 build: None,
188 config,
189 tcp_connection: None,
190 })
191 }
192 "run" => {
193 let mut next_arg_is_build = false;
194 let mut seen_pkg = false;
195
196 let mut program = ".".to_string();
197 let mut args = Vec::default();
198 let mut build_flags = Vec::default();
199
200 for arg in build_config.args.iter().skip(1) {
201 if seen_pkg {
202 args.push(arg.clone())
203 } else if next_arg_is_build {
204 build_flags.push(arg.clone());
205 next_arg_is_build = false;
206 } else if arg.starts_with("-") {
207 if let Some(has_arg) = is_build_flag(arg.trim_start_matches("-")) {
208 next_arg_is_build = has_arg;
209 }
210 build_flags.push(arg.clone())
211 } else {
212 program = arg.to_string();
213 seen_pkg = true;
214 }
215 }
216
217 let config: serde_json::Value = serde_json::to_value(DelveLaunchRequest {
218 cwd: build_config.cwd.clone(),
219 env: build_config.env.clone(),
220 request: "launch".to_string(),
221 mode: "debug".to_string(),
222 program,
223 args,
224 build_flags,
225 })
226 .unwrap();
227
228 Some(DebugScenario {
229 label: resolved_label.to_string().into(),
230 adapter: adapter.0.clone(),
231 build: None,
232 config,
233 tcp_connection: None,
234 })
235 }
236 _ => None,
237 }
238 }
239
240 async fn run(&self, _build_config: SpawnInTerminal) -> Result<DebugRequest> {
241 unreachable!()
242 }
243}
244
245#[cfg(test)]
246mod tests {
247 use super::*;
248 use gpui::TestAppContext;
249 use task::{HideStrategy, RevealStrategy, RevealTarget, Shell, TaskTemplate};
250
251 #[gpui::test]
252 async fn test_create_scenario_for_go_build(_: &mut TestAppContext) {
253 let locator = GoLocator;
254 let task = TaskTemplate {
255 label: "go build".into(),
256 command: "go".into(),
257 args: vec!["build".into(), ".".into()],
258 env: Default::default(),
259 cwd: Some("${ZED_WORKTREE_ROOT}".into()),
260 use_new_terminal: false,
261 allow_concurrent_runs: false,
262 reveal: RevealStrategy::Always,
263 reveal_target: RevealTarget::Dock,
264 hide: HideStrategy::Never,
265 shell: Shell::System,
266 tags: vec![],
267 show_summary: true,
268 show_command: true,
269 };
270
271 let scenario = locator
272 .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
273 .await;
274
275 assert!(scenario.is_none());
276 }
277
278 #[gpui::test]
279 async fn test_skip_non_go_commands_with_non_delve_adapter(_: &mut TestAppContext) {
280 let locator = GoLocator;
281 let task = TaskTemplate {
282 label: "cargo build".into(),
283 command: "cargo".into(),
284 args: vec!["build".into()],
285 env: Default::default(),
286 cwd: Some("${ZED_WORKTREE_ROOT}".into()),
287 use_new_terminal: false,
288 allow_concurrent_runs: false,
289 reveal: RevealStrategy::Always,
290 reveal_target: RevealTarget::Dock,
291 hide: HideStrategy::Never,
292 shell: Shell::System,
293 tags: vec![],
294 show_summary: true,
295 show_command: true,
296 };
297
298 let scenario = locator
299 .create_scenario(
300 &task,
301 "test label",
302 &DebugAdapterName("SomeOtherAdapter".into()),
303 )
304 .await;
305 assert!(scenario.is_none());
306
307 let scenario = locator
308 .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
309 .await;
310 assert!(scenario.is_none());
311 }
312 #[gpui::test]
313 async fn test_go_locator_run(_: &mut TestAppContext) {
314 let locator = GoLocator;
315 let delve = DebugAdapterName("Delve".into());
316
317 let task = TaskTemplate {
318 label: "go run with flags".into(),
319 command: "go".into(),
320 args: vec![
321 "run".to_string(),
322 "-race".to_string(),
323 "-ldflags".to_string(),
324 "-X main.version=1.0".to_string(),
325 "./cmd/myapp".to_string(),
326 "--config".to_string(),
327 "production.yaml".to_string(),
328 "--verbose".to_string(),
329 ],
330 env: {
331 let mut env = HashMap::default();
332 env.insert("GO_ENV".to_string(), "production".to_string());
333 env
334 },
335 cwd: Some("/project/root".into()),
336 ..Default::default()
337 };
338
339 let scenario = locator
340 .create_scenario(&task, "test run label", &delve)
341 .await
342 .unwrap();
343
344 let config: DelveLaunchRequest = serde_json::from_value(scenario.config).unwrap();
345
346 assert_eq!(
347 config,
348 DelveLaunchRequest {
349 request: "launch".to_string(),
350 mode: "debug".to_string(),
351 program: "./cmd/myapp".to_string(),
352 build_flags: vec![
353 "-race".to_string(),
354 "-ldflags".to_string(),
355 "-X main.version=1.0".to_string()
356 ],
357 args: vec![
358 "--config".to_string(),
359 "production.yaml".to_string(),
360 "--verbose".to_string(),
361 ],
362 env: {
363 let mut env = HashMap::default();
364 env.insert("GO_ENV".to_string(), "production".to_string());
365 env
366 },
367 cwd: Some("/project/root".to_string()),
368 }
369 );
370 }
371
372 #[gpui::test]
373 async fn test_go_locator_test(_: &mut TestAppContext) {
374 let locator = GoLocator;
375 let delve = DebugAdapterName("Delve".into());
376
377 // Test with tags and run flag
378 let task_with_tags = TaskTemplate {
379 label: "test".into(),
380 command: "go".into(),
381 args: vec![
382 "test".to_string(),
383 "-tags".to_string(),
384 "integration,unit".to_string(),
385 "-run".to_string(),
386 "Foo".to_string(),
387 ".".to_string(),
388 ],
389 ..Default::default()
390 };
391 let result = locator
392 .create_scenario(&task_with_tags, "", &delve)
393 .await
394 .unwrap();
395
396 let config: DelveLaunchRequest = serde_json::from_value(result.config).unwrap();
397
398 assert_eq!(
399 config,
400 DelveLaunchRequest {
401 request: "launch".to_string(),
402 mode: "test".to_string(),
403 program: ".".to_string(),
404 build_flags: vec!["-tags".to_string(), "integration,unit".to_string(),],
405 args: vec![
406 "-test.run".to_string(),
407 "Foo".to_string(),
408 "-test.v".to_string()
409 ],
410 env: HashMap::default(),
411 cwd: None,
412 }
413 );
414 }
415
416 #[gpui::test]
417 async fn test_skip_unsupported_go_commands(_: &mut TestAppContext) {
418 let locator = GoLocator;
419 let task = TaskTemplate {
420 label: "go clean".into(),
421 command: "go".into(),
422 args: vec!["clean".into()],
423 env: Default::default(),
424 cwd: Some("${ZED_WORKTREE_ROOT}".into()),
425 use_new_terminal: false,
426 allow_concurrent_runs: false,
427 reveal: RevealStrategy::Always,
428 reveal_target: RevealTarget::Dock,
429 hide: HideStrategy::Never,
430 shell: Shell::System,
431 tags: vec![],
432 show_summary: true,
433 show_command: true,
434 };
435
436 let scenario = locator
437 .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
438 .await;
439 assert!(scenario.is_none());
440 }
441}