node.rs

 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
35        if adapter.as_ref() != "JavaScript" {
36            return None;
37        }
38        if build_config.command != TYPESCRIPT_RUNNER_VARIABLE.template_value() {
39            return None;
40        }
41        let test_library = build_config.args.first()?;
42        let program_path = Path::new("$ZED_WORKTREE_ROOT")
43            .join("node_modules")
44            .join(".bin")
45            .join(test_library);
46
47        let mut args = if test_library == "jest"
48            || test_library == &TYPESCRIPT_JEST_TASK_VARIABLE.template_value()
49        {
50            vec!["--runInBand".to_owned()]
51        } else {
52            vec![]
53        };
54        args.extend(build_config.args[1..].iter().cloned());
55
56        let config = serde_json::json!({
57            "request": "launch",
58            "type": "pwa-node",
59            "program": program_path,
60            "args": args,
61            "cwd": build_config.cwd.clone(),
62            "runtimeArgs": ["--inspect-brk"],
63            "console": "integratedTerminal",
64        });
65
66        Some(DebugScenario {
67            adapter: adapter.0,
68            label: resolved_label.to_string().into(),
69            build: None,
70            config,
71            tcp_connection: None,
72        })
73    }
74
75    async fn run(&self, _: SpawnInTerminal) -> Result<DebugRequest> {
76        bail!("Python locator should not require DapLocator::run to be ran");
77    }
78}