1use std::{borrow::Cow, path::Path};
2
3use anyhow::{Result, bail};
4use async_trait::async_trait;
5use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
6use gpui::SharedString;
7
8use task::{DebugScenario, SpawnInTerminal, TaskTemplate, VariableName};
9
10pub(crate) struct NodeLocator;
11
12const TYPESCRIPT_RUNNER_VARIABLE: VariableName =
13 VariableName::Custom(Cow::Borrowed("TYPESCRIPT_RUNNER"));
14const TYPESCRIPT_JEST_TASK_VARIABLE: VariableName =
15 VariableName::Custom(Cow::Borrowed("TYPESCRIPT_JEST"));
16
17#[async_trait]
18impl DapLocator for NodeLocator {
19 fn name(&self) -> SharedString {
20 SharedString::new_static("Node")
21 }
22
23 /// Determines whether this locator can generate debug target for given task.
24 fn create_scenario(
25 &self,
26 build_config: &TaskTemplate,
27 resolved_label: &str,
28 adapter: DebugAdapterName,
29 ) -> Option<DebugScenario> {
30 // TODO(debugger) fix issues with `await` breakpoint step
31 if cfg!(not(debug_assertions)) {
32 return None;
33 }
34 if adapter.0.as_ref() != "JavaScript" {
35 return None;
36 }
37 if build_config.command != TYPESCRIPT_RUNNER_VARIABLE.template_value() {
38 return None;
39 }
40 let test_library = build_config.args.first()?;
41 let program_path = Path::new("$ZED_WORKTREE_ROOT")
42 .join("node_modules")
43 .join(".bin")
44 .join(test_library);
45
46 let mut args = if test_library == "jest"
47 || test_library == &TYPESCRIPT_JEST_TASK_VARIABLE.template_value()
48 {
49 vec!["--runInBand".to_owned()]
50 } else {
51 vec![]
52 };
53 args.extend(build_config.args[1..].iter().cloned());
54
55 let config = serde_json::json!({
56 "request": "launch",
57 "type": "pwa-node",
58 "runtimeExecutable": program_path,
59 "args": args,
60 "cwd": build_config.cwd.clone(),
61 "runtimeArgs": ["--inspect-brk"],
62 "console": "integratedTerminal",
63 });
64
65 Some(DebugScenario {
66 adapter: adapter.0,
67 label: resolved_label.to_string().into(),
68 build: None,
69 config,
70 tcp_connection: None,
71 })
72 }
73
74 async fn run(&self, _: SpawnInTerminal) -> Result<DebugRequest> {
75 bail!("JavaScript locator should not require DapLocator::run to be ran");
76 }
77}