1use crate::*;
2use anyhow::Context as _;
3use dap::{DebugRequest, StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
4use fs::RemoveOptions;
5use futures::{StreamExt, TryStreamExt};
6use gpui::http_client::AsyncBody;
7use gpui::{AsyncApp, SharedString};
8use json_dotpath::DotPaths;
9use language::LanguageName;
10use paths::debug_adapters_dir;
11use serde_json::Value;
12use smol::fs::File;
13use smol::io::AsyncReadExt;
14use smol::lock::OnceCell;
15use std::ffi::OsString;
16use std::net::Ipv4Addr;
17use std::str::FromStr;
18use std::{
19 collections::HashMap,
20 ffi::OsStr,
21 path::{Path, PathBuf},
22};
23use util::{ResultExt, maybe};
24
25#[derive(Default)]
26pub(crate) struct PythonDebugAdapter {
27 base_venv_path: OnceCell<Result<Arc<Path>, String>>,
28 debugpy_whl_base_path: OnceCell<Result<Arc<Path>, String>>,
29}
30
31impl PythonDebugAdapter {
32 const ADAPTER_NAME: &'static str = "Debugpy";
33 const DEBUG_ADAPTER_NAME: DebugAdapterName =
34 DebugAdapterName(SharedString::new_static(Self::ADAPTER_NAME));
35
36 const LANGUAGE_NAME: &'static str = "Python";
37
38 async fn generate_debugpy_arguments(
39 host: &Ipv4Addr,
40 port: u16,
41 user_installed_path: Option<&Path>,
42 user_args: Option<Vec<String>>,
43 ) -> Result<Vec<String>> {
44 let mut args = if let Some(user_installed_path) = user_installed_path {
45 log::debug!(
46 "Using user-installed debugpy adapter from: {}",
47 user_installed_path.display()
48 );
49 vec![user_installed_path.to_string_lossy().to_string()]
50 } else {
51 let adapter_path = paths::debug_adapters_dir().join(Self::DEBUG_ADAPTER_NAME.as_ref());
52 let path = adapter_path
53 .join("debugpy")
54 .join("adapter")
55 .to_string_lossy()
56 .into_owned();
57 log::debug!("Using pip debugpy adapter from: {path}");
58 vec![path]
59 };
60
61 args.extend(if let Some(args) = user_args {
62 args
63 } else {
64 vec![format!("--host={}", host), format!("--port={}", port)]
65 });
66 Ok(args)
67 }
68
69 async fn request_args(
70 &self,
71 delegate: &Arc<dyn DapDelegate>,
72 task_definition: &DebugTaskDefinition,
73 ) -> Result<StartDebuggingRequestArguments> {
74 let request = self.request_kind(&task_definition.config).await?;
75
76 let mut configuration = task_definition.config.clone();
77 if let Ok(console) = configuration.dot_get_mut("console") {
78 // Use built-in Zed terminal if user did not explicitly provide a setting for console.
79 if console.is_null() {
80 *console = Value::String("integratedTerminal".into());
81 }
82 }
83
84 if let Some(obj) = configuration.as_object_mut() {
85 obj.entry("cwd")
86 .or_insert(delegate.worktree_root_path().to_string_lossy().into());
87 }
88
89 Ok(StartDebuggingRequestArguments {
90 configuration,
91 request,
92 })
93 }
94
95 async fn fetch_wheel(&self, delegate: &Arc<dyn DapDelegate>) -> Result<Arc<Path>, String> {
96 let download_dir = debug_adapters_dir().join(Self::ADAPTER_NAME).join("wheels");
97 std::fs::create_dir_all(&download_dir).map_err(|e| e.to_string())?;
98 let system_python = self.base_venv_path(delegate).await?;
99
100 let installation_succeeded = util::command::new_smol_command(system_python.as_ref())
101 .args([
102 "-m",
103 "pip",
104 "download",
105 "debugpy",
106 "--only-binary=:all:",
107 "-d",
108 download_dir.to_string_lossy().as_ref(),
109 ])
110 .output()
111 .await
112 .map_err(|e| format!("{e}"))?
113 .status
114 .success();
115 if !installation_succeeded {
116 return Err("debugpy installation failed (could not fetch Debugpy's wheel)".into());
117 }
118
119 let wheel_path = std::fs::read_dir(&download_dir)
120 .map_err(|e| e.to_string())?
121 .find_map(|entry| {
122 entry.ok().filter(|e| {
123 e.file_type().is_ok_and(|typ| typ.is_file())
124 && Path::new(&e.file_name()).extension() == Some("whl".as_ref())
125 })
126 })
127 .ok_or_else(|| String::from("Did not find a .whl in {download_dir}"))?;
128
129 util::archive::extract_zip(
130 &debug_adapters_dir().join(Self::ADAPTER_NAME),
131 File::open(&wheel_path.path())
132 .await
133 .map_err(|e| e.to_string())?,
134 )
135 .await
136 .map_err(|e| e.to_string())?;
137
138 Ok(Arc::from(wheel_path.path()))
139 }
140
141 async fn maybe_fetch_new_wheel(&self, delegate: &Arc<dyn DapDelegate>) {
142 let latest_release = delegate
143 .http_client()
144 .get(
145 "https://pypi.org/pypi/debugpy/json",
146 AsyncBody::empty(),
147 false,
148 )
149 .await
150 .log_err();
151 maybe!(async move {
152 let response = latest_release.filter(|response| response.status().is_success())?;
153
154 let download_dir = debug_adapters_dir().join(Self::ADAPTER_NAME);
155 std::fs::create_dir_all(&download_dir).ok()?;
156
157 let mut output = String::new();
158 response
159 .into_body()
160 .read_to_string(&mut output)
161 .await
162 .ok()?;
163 let as_json = serde_json::Value::from_str(&output).ok()?;
164 let latest_version = as_json.get("info").and_then(|info| {
165 info.get("version")
166 .and_then(|version| version.as_str())
167 .map(ToOwned::to_owned)
168 })?;
169 let dist_info_dirname: OsString = format!("debugpy-{latest_version}.dist-info").into();
170 let is_up_to_date = delegate
171 .fs()
172 .read_dir(&debug_adapters_dir().join(Self::ADAPTER_NAME))
173 .await
174 .ok()?
175 .into_stream()
176 .any(async |entry| {
177 entry.is_ok_and(|e| e.file_name().is_some_and(|name| name == dist_info_dirname))
178 })
179 .await;
180
181 if !is_up_to_date {
182 delegate
183 .fs()
184 .remove_dir(
185 &debug_adapters_dir().join(Self::ADAPTER_NAME),
186 RemoveOptions {
187 recursive: true,
188 ignore_if_not_exists: true,
189 },
190 )
191 .await
192 .ok()?;
193 self.fetch_wheel(delegate).await.ok()?;
194 }
195 Some(())
196 })
197 .await;
198 }
199
200 async fn fetch_debugpy_whl(
201 &self,
202 delegate: &Arc<dyn DapDelegate>,
203 ) -> Result<Arc<Path>, String> {
204 self.debugpy_whl_base_path
205 .get_or_init(|| async move {
206 self.maybe_fetch_new_wheel(delegate).await;
207 Ok(Arc::from(
208 debug_adapters_dir()
209 .join(Self::ADAPTER_NAME)
210 .join("debugpy")
211 .join("adapter")
212 .as_ref(),
213 ))
214 })
215 .await
216 .clone()
217 }
218
219 async fn base_venv_path(&self, delegate: &Arc<dyn DapDelegate>) -> Result<Arc<Path>, String> {
220 self.base_venv_path
221 .get_or_init(|| async {
222 let base_python = Self::system_python_name(delegate)
223 .await
224 .ok_or_else(|| String::from("Could not find a Python installation"))?;
225
226 let did_succeed = util::command::new_smol_command(base_python)
227 .args(["-m", "venv", "zed_base_venv"])
228 .current_dir(
229 paths::debug_adapters_dir().join(Self::DEBUG_ADAPTER_NAME.as_ref()),
230 )
231 .spawn()
232 .map_err(|e| format!("{e:#?}"))?
233 .status()
234 .await
235 .map_err(|e| format!("{e:#?}"))?
236 .success();
237 if !did_succeed {
238 return Err("Failed to create base virtual environment".into());
239 }
240
241 const DIR: &str = if cfg!(target_os = "windows") {
242 "Scripts"
243 } else {
244 "bin"
245 };
246 Ok(Arc::from(
247 paths::debug_adapters_dir()
248 .join(Self::DEBUG_ADAPTER_NAME.as_ref())
249 .join("zed_base_venv")
250 .join(DIR)
251 .join("python3")
252 .as_ref(),
253 ))
254 })
255 .await
256 .clone()
257 }
258 async fn system_python_name(delegate: &Arc<dyn DapDelegate>) -> Option<String> {
259 const BINARY_NAMES: [&str; 3] = ["python3", "python", "py"];
260 let mut name = None;
261
262 for cmd in BINARY_NAMES {
263 name = delegate
264 .which(OsStr::new(cmd))
265 .await
266 .map(|path| path.to_string_lossy().to_string());
267 if name.is_some() {
268 break;
269 }
270 }
271 name
272 }
273
274 async fn get_installed_binary(
275 &self,
276 delegate: &Arc<dyn DapDelegate>,
277 config: &DebugTaskDefinition,
278 user_installed_path: Option<PathBuf>,
279 user_args: Option<Vec<String>>,
280 python_from_toolchain: Option<String>,
281 ) -> Result<DebugAdapterBinary> {
282 let tcp_connection = config.tcp_connection.clone().unwrap_or_default();
283 let (host, port, timeout) = crate::configure_tcp_connection(tcp_connection).await?;
284
285 let python_path = if let Some(toolchain) = python_from_toolchain {
286 Some(toolchain)
287 } else {
288 Self::system_python_name(delegate).await
289 };
290
291 let python_command = python_path.context("failed to find binary path for Python")?;
292 log::debug!("Using Python executable: {}", python_command);
293
294 let arguments = Self::generate_debugpy_arguments(
295 &host,
296 port,
297 user_installed_path.as_deref(),
298 user_args,
299 )
300 .await?;
301
302 log::debug!(
303 "Starting debugpy adapter with command: {} {}",
304 python_command,
305 arguments.join(" ")
306 );
307
308 Ok(DebugAdapterBinary {
309 command: Some(python_command),
310 arguments,
311 connection: Some(adapters::TcpArguments {
312 host,
313 port,
314 timeout,
315 }),
316 cwd: Some(delegate.worktree_root_path().to_path_buf()),
317 envs: HashMap::default(),
318 request_args: self.request_args(delegate, config).await?,
319 })
320 }
321}
322
323#[async_trait(?Send)]
324impl DebugAdapter for PythonDebugAdapter {
325 fn name(&self) -> DebugAdapterName {
326 Self::DEBUG_ADAPTER_NAME
327 }
328
329 fn adapter_language_name(&self) -> Option<LanguageName> {
330 Some(SharedString::new_static("Python").into())
331 }
332
333 async fn config_from_zed_format(&self, zed_scenario: ZedDebugConfig) -> Result<DebugScenario> {
334 let mut args = json!({
335 "request": match zed_scenario.request {
336 DebugRequest::Launch(_) => "launch",
337 DebugRequest::Attach(_) => "attach",
338 },
339 "subProcess": true,
340 "redirectOutput": true,
341 });
342
343 let map = args.as_object_mut().unwrap();
344 match &zed_scenario.request {
345 DebugRequest::Attach(attach) => {
346 map.insert("processId".into(), attach.process_id.into());
347 }
348 DebugRequest::Launch(launch) => {
349 map.insert("program".into(), launch.program.clone().into());
350 map.insert("args".into(), launch.args.clone().into());
351 if !launch.env.is_empty() {
352 map.insert("env".into(), launch.env_json());
353 }
354
355 if let Some(stop_on_entry) = zed_scenario.stop_on_entry {
356 map.insert("stopOnEntry".into(), stop_on_entry.into());
357 }
358 if let Some(cwd) = launch.cwd.as_ref() {
359 map.insert("cwd".into(), cwd.to_string_lossy().into_owned().into());
360 }
361 }
362 }
363
364 Ok(DebugScenario {
365 adapter: zed_scenario.adapter,
366 label: zed_scenario.label,
367 config: args,
368 build: None,
369 tcp_connection: None,
370 })
371 }
372
373 fn dap_schema(&self) -> serde_json::Value {
374 json!({
375 "properties": {
376 "request": {
377 "type": "string",
378 "enum": ["attach", "launch"],
379 "description": "Debug adapter request type"
380 },
381 "autoReload": {
382 "default": {},
383 "description": "Configures automatic reload of code on edit.",
384 "properties": {
385 "enable": {
386 "default": false,
387 "description": "Automatically reload code on edit.",
388 "type": "boolean"
389 },
390 "exclude": {
391 "default": [
392 "**/.git/**",
393 "**/.metadata/**",
394 "**/__pycache__/**",
395 "**/node_modules/**",
396 "**/site-packages/**"
397 ],
398 "description": "Glob patterns of paths to exclude from auto reload.",
399 "items": {
400 "type": "string"
401 },
402 "type": "array"
403 },
404 "include": {
405 "default": [
406 "**/*.py",
407 "**/*.pyw"
408 ],
409 "description": "Glob patterns of paths to include in auto reload.",
410 "items": {
411 "type": "string"
412 },
413 "type": "array"
414 }
415 },
416 "type": "object"
417 },
418 "debugAdapterPath": {
419 "description": "Path (fully qualified) to the python debug adapter executable.",
420 "type": "string"
421 },
422 "django": {
423 "default": false,
424 "description": "Django debugging.",
425 "type": "boolean"
426 },
427 "jinja": {
428 "default": null,
429 "description": "Jinja template debugging (e.g. Flask).",
430 "enum": [
431 false,
432 null,
433 true
434 ]
435 },
436 "justMyCode": {
437 "default": true,
438 "description": "If true, show and debug only user-written code. If false, show and debug all code, including library calls.",
439 "type": "boolean"
440 },
441 "logToFile": {
442 "default": false,
443 "description": "Enable logging of debugger events to a log file. This file can be found in the debugpy extension install folder.",
444 "type": "boolean"
445 },
446 "pathMappings": {
447 "default": [],
448 "items": {
449 "label": "Path mapping",
450 "properties": {
451 "localRoot": {
452 "default": "${ZED_WORKTREE_ROOT}",
453 "label": "Local source root.",
454 "type": "string"
455 },
456 "remoteRoot": {
457 "default": "",
458 "label": "Remote source root.",
459 "type": "string"
460 }
461 },
462 "required": [
463 "localRoot",
464 "remoteRoot"
465 ],
466 "type": "object"
467 },
468 "label": "Path mappings.",
469 "type": "array"
470 },
471 "redirectOutput": {
472 "default": true,
473 "description": "Redirect output.",
474 "type": "boolean"
475 },
476 "showReturnValue": {
477 "default": true,
478 "description": "Show return value of functions when stepping.",
479 "type": "boolean"
480 },
481 "subProcess": {
482 "default": false,
483 "description": "Whether to enable Sub Process debugging",
484 "type": "boolean"
485 },
486 "consoleName": {
487 "default": "Python Debug Console",
488 "description": "Display name of the debug console or terminal",
489 "type": "string"
490 },
491 "clientOS": {
492 "default": null,
493 "description": "OS that VS code is using.",
494 "enum": [
495 "windows",
496 null,
497 "unix"
498 ]
499 }
500 },
501 "required": ["request"],
502 "allOf": [
503 {
504 "if": {
505 "properties": {
506 "request": {
507 "enum": ["attach"]
508 }
509 }
510 },
511 "then": {
512 "properties": {
513 "connect": {
514 "label": "Attach by connecting to debugpy over a socket.",
515 "properties": {
516 "host": {
517 "default": "127.0.0.1",
518 "description": "Hostname or IP address to connect to.",
519 "type": "string"
520 },
521 "port": {
522 "description": "Port to connect to.",
523 "type": [
524 "number",
525 "string"
526 ]
527 }
528 },
529 "required": [
530 "port"
531 ],
532 "type": "object"
533 },
534 "listen": {
535 "label": "Attach by listening for incoming socket connection from debugpy",
536 "properties": {
537 "host": {
538 "default": "127.0.0.1",
539 "description": "Hostname or IP address of the interface to listen on.",
540 "type": "string"
541 },
542 "port": {
543 "description": "Port to listen on.",
544 "type": [
545 "number",
546 "string"
547 ]
548 }
549 },
550 "required": [
551 "port"
552 ],
553 "type": "object"
554 },
555 "processId": {
556 "anyOf": [
557 {
558 "default": "${command:pickProcess}",
559 "description": "Use process picker to select a process to attach, or Process ID as integer.",
560 "enum": [
561 "${command:pickProcess}"
562 ]
563 },
564 {
565 "description": "ID of the local process to attach to.",
566 "type": "integer"
567 }
568 ]
569 }
570 }
571 }
572 },
573 {
574 "if": {
575 "properties": {
576 "request": {
577 "enum": ["launch"]
578 }
579 }
580 },
581 "then": {
582 "properties": {
583 "args": {
584 "default": [],
585 "description": "Command line arguments passed to the program. For string type arguments, it will pass through the shell as is, and therefore all shell variable expansions will apply. But for the array type, the values will be shell-escaped.",
586 "items": {
587 "type": "string"
588 },
589 "anyOf": [
590 {
591 "default": "${command:pickArgs}",
592 "enum": [
593 "${command:pickArgs}"
594 ]
595 },
596 {
597 "type": [
598 "array",
599 "string"
600 ]
601 }
602 ]
603 },
604 "console": {
605 "default": "integratedTerminal",
606 "description": "Where to launch the debug target: internal console, integrated terminal, or external terminal.",
607 "enum": [
608 "externalTerminal",
609 "integratedTerminal",
610 "internalConsole"
611 ]
612 },
613 "cwd": {
614 "default": "${ZED_WORKTREE_ROOT}",
615 "description": "Absolute path to the working directory of the program being debugged. Default is the root directory of the file (leave empty).",
616 "type": "string"
617 },
618 "autoStartBrowser": {
619 "default": false,
620 "description": "Open external browser to launch the application",
621 "type": "boolean"
622 },
623 "env": {
624 "additionalProperties": {
625 "type": "string"
626 },
627 "default": {},
628 "description": "Environment variables defined as a key value pair. Property ends up being the Environment Variable and the value of the property ends up being the value of the Env Variable.",
629 "type": "object"
630 },
631 "envFile": {
632 "default": "${ZED_WORKTREE_ROOT}/.env",
633 "description": "Absolute path to a file containing environment variable definitions.",
634 "type": "string"
635 },
636 "gevent": {
637 "default": false,
638 "description": "Enable debugging of gevent monkey-patched code.",
639 "type": "boolean"
640 },
641 "module": {
642 "default": "",
643 "description": "Name of the module to be debugged.",
644 "type": "string"
645 },
646 "program": {
647 "default": "${ZED_FILE}",
648 "description": "Absolute path to the program.",
649 "type": "string"
650 },
651 "purpose": {
652 "default": [],
653 "description": "Tells extension to use this configuration for test debugging, or when using debug-in-terminal command.",
654 "items": {
655 "enum": [
656 "debug-test",
657 "debug-in-terminal"
658 ],
659 "enumDescriptions": [
660 "Use this configuration while debugging tests using test view or test debug commands.",
661 "Use this configuration while debugging a file using debug in terminal button in the editor."
662 ]
663 },
664 "type": "array"
665 },
666 "pyramid": {
667 "default": false,
668 "description": "Whether debugging Pyramid applications.",
669 "type": "boolean"
670 },
671 "python": {
672 "default": "${command:python.interpreterPath}",
673 "description": "Absolute path to the Python interpreter executable; overrides workspace configuration if set.",
674 "type": "string"
675 },
676 "pythonArgs": {
677 "default": [],
678 "description": "Command-line arguments passed to the Python interpreter. To pass arguments to the debug target, use \"args\".",
679 "items": {
680 "type": "string"
681 },
682 "type": "array"
683 },
684 "stopOnEntry": {
685 "default": false,
686 "description": "Automatically stop after launch.",
687 "type": "boolean"
688 },
689 "sudo": {
690 "default": false,
691 "description": "Running debug program under elevated permissions (on Unix).",
692 "type": "boolean"
693 },
694 "guiEventLoop": {
695 "default": "matplotlib",
696 "description": "The GUI event loop that's going to run. Possible values: \"matplotlib\", \"wx\", \"qt\", \"none\", or a custom function that'll be imported and run.",
697 "type": "string"
698 }
699 }
700 }
701 }
702 ]
703 })
704 }
705
706 async fn get_binary(
707 &self,
708 delegate: &Arc<dyn DapDelegate>,
709 config: &DebugTaskDefinition,
710 user_installed_path: Option<PathBuf>,
711 user_args: Option<Vec<String>>,
712 cx: &mut AsyncApp,
713 ) -> Result<DebugAdapterBinary> {
714 if let Some(local_path) = &user_installed_path {
715 log::debug!(
716 "Using user-installed debugpy adapter from: {}",
717 local_path.display()
718 );
719 return self
720 .get_installed_binary(delegate, config, Some(local_path.clone()), user_args, None)
721 .await;
722 }
723
724 let base_path = config
725 .config
726 .get("cwd")
727 .and_then(|cwd| {
728 cwd.as_str()
729 .map(Path::new)?
730 .strip_prefix(delegate.worktree_root_path())
731 .ok()
732 })
733 .unwrap_or_else(|| "".as_ref())
734 .into();
735 let toolchain = delegate
736 .toolchain_store()
737 .active_toolchain(
738 delegate.worktree_id(),
739 base_path,
740 language::LanguageName::new(Self::LANGUAGE_NAME),
741 cx,
742 )
743 .await;
744
745 let debugpy_path = self
746 .fetch_debugpy_whl(delegate)
747 .await
748 .map_err(|e| anyhow::anyhow!("{e}"))?;
749 if let Some(toolchain) = &toolchain {
750 log::debug!(
751 "Found debugpy in toolchain environment: {}",
752 debugpy_path.display()
753 );
754 return self
755 .get_installed_binary(
756 delegate,
757 config,
758 None,
759 user_args,
760 Some(toolchain.path.to_string()),
761 )
762 .await;
763 }
764
765 self.get_installed_binary(delegate, config, None, user_args, None)
766 .await
767 }
768
769 fn label_for_child_session(&self, args: &StartDebuggingRequestArguments) -> Option<String> {
770 let label = args
771 .configuration
772 .get("name")?
773 .as_str()
774 .filter(|label| !label.is_empty())?;
775 Some(label.to_owned())
776 }
777}
778
779#[cfg(test)]
780mod tests {
781 use util::path;
782
783 use super::*;
784 use std::{net::Ipv4Addr, path::PathBuf};
785
786 #[gpui::test]
787 async fn test_debugpy_install_path_cases() {
788 let host = Ipv4Addr::new(127, 0, 0, 1);
789 let port = 5678;
790
791 // Case 1: User-defined debugpy path (highest precedence)
792 let user_path = PathBuf::from("/custom/path/to/debugpy/src/debugpy/adapter");
793 let user_args =
794 PythonDebugAdapter::generate_debugpy_arguments(&host, port, Some(&user_path), None)
795 .await
796 .unwrap();
797
798 // Case 2: Venv-installed debugpy (uses -m debugpy.adapter)
799 let venv_args = PythonDebugAdapter::generate_debugpy_arguments(&host, port, None, None)
800 .await
801 .unwrap();
802
803 assert_eq!(user_args[0], "/custom/path/to/debugpy/src/debugpy/adapter");
804 assert_eq!(user_args[1], "--host=127.0.0.1");
805 assert_eq!(user_args[2], "--port=5678");
806
807 let expected_suffix = path!("debug_adapters/Debugpy/debugpy/adapter");
808 assert!(venv_args[0].ends_with(expected_suffix));
809 assert_eq!(venv_args[1], "--host=127.0.0.1");
810 assert_eq!(venv_args[2], "--port=5678");
811
812 // The same cases, with arguments overridden by the user
813 let user_args = PythonDebugAdapter::generate_debugpy_arguments(
814 &host,
815 port,
816 Some(&user_path),
817 Some(vec!["foo".into()]),
818 )
819 .await
820 .unwrap();
821 let venv_args = PythonDebugAdapter::generate_debugpy_arguments(
822 &host,
823 port,
824 None,
825 Some(vec!["foo".into()]),
826 )
827 .await
828 .unwrap();
829
830 assert!(user_args[0].ends_with("src/debugpy/adapter"));
831 assert_eq!(user_args[1], "foo");
832
833 assert!(venv_args[0].ends_with(expected_suffix));
834 assert_eq!(venv_args[1], "foo");
835
836 // Note: Case 3 (GitHub-downloaded debugpy) is not tested since this requires mocking the Github API.
837 }
838}