diff --git a/crates/ide-dap/src/adapter.rs b/crates/ide-dap/src/adapter.rs index f83f5869c..47766051e 100644 --- a/crates/ide-dap/src/adapter.rs +++ b/crates/ide-dap/src/adapter.rs @@ -391,9 +391,10 @@ pub const DEBUGPY_MISSING: &str = "debugpy is not installed"; /// `PYTHONPATH`. Neither: an `unavailable` error naming the interpreter. pub async fn prepare_debugpy(spec: &mut AdapterSpec) -> IdeResult<()> { let (command, cwd) = (spec.command.clone(), spec.cwd.clone()); - let Some(python) = tokio::task::spawn_blocking(move || resolve_binary(&command, &cwd)).await.ok().flatten() else { + let Some(python) = tokio::task::spawn_blocking(move || debugpy_python(&command, &cwd)).await.ok().flatten() else { return Ok(()); // Reported as a missing adapter when it is started. }; + spec.command = python.to_string_lossy().into_owned(); let mut probe = Command::new(&python); probe.current_dir(&spec.cwd); ide_core::process::prepare(probe.as_std_mut()); @@ -426,6 +427,46 @@ pub async fn prepare_debugpy(spec: &mut AdapterSpec) -> IdeResult<()> { ))) } +/// The interpreter ` -m debugpy.adapter` runs with. On Windows a bare `python3` (the +/// built-in `python.toml`'s) or `python` may name an App Execution Alias in `WindowsApps`, +/// which only opens the Store unless the Store's Python is installed: every match on `PATH` +/// of the command, then `python`, then the `py` launcher, the first that is no alias or runs. +fn debugpy_python(command: &str, cwd: &Path) -> Option { + let found = resolve_binary(command, cwd); + if !cfg!(windows) || !matches!(command, "python3" | "python") { + return found; + } + let pathext = std::env::var("PATHEXT").ok(); + let dirs = search_dirs(cwd); + let mut candidates: Vec = Vec::new(); + for name in [command, "python", "py"] { + for dir in &dirs { + if let Some(p) = find_binary_in(name, cwd, std::slice::from_ref(dir), true, pathext.as_deref()) + && !candidates.contains(&p) + { + candidates.push(p); + } + } + } + let runs = |p: &Path| { + let mut c = std::process::Command::new(p); + ide_core::process::prepare(&mut c); + c.arg("--version").stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); + c.status().is_ok_and(|s| s.success()) + }; + pick_python(candidates, runs).or(found) +} + +/// The first candidate that is no App Execution Alias, or is one that `runs`. +fn pick_python(candidates: Vec, runs: impl Fn(&Path) -> bool) -> Option { + candidates.into_iter().find(|p| !is_app_execution_alias(p) || runs(p)) +} + +/// A Windows App Execution Alias (`%LOCALAPPDATA%\Microsoft\WindowsApps\python3.exe`). +fn is_app_execution_alias(path: &Path) -> bool { + path.parent().and_then(Path::file_name).is_some_and(|d| d.eq_ignore_ascii_case("WindowsApps")) +} + /// GDB's DAP interpreter (`-i dap`) shipped in GDB 14; older ones exit with /// "Interpreter `dap' unrecognized". const GDB_MIN_MAJOR: u32 = 14; @@ -859,6 +900,19 @@ async fn connect_with_retries( mod tests { use super::*; + /// Windows: a Store alias on `PATH` before the real interpreter is skipped unless it runs + /// (the Store's Python is installed), so the Python Debug Server starts the real one. + #[test] + fn debugpy_skips_store_aliases_that_do_not_run() { + let alias = PathBuf::from("C:/Users/me/AppData/Local/Microsoft/WindowsApps/python3.exe"); + let real = PathBuf::from("C:/Program Files/Python313/python.exe"); + assert!(is_app_execution_alias(&alias) && !is_app_execution_alias(&real)); + let both = vec![alias.clone(), real.clone()]; + assert_eq!(pick_python(both.clone(), |_| false), Some(real.clone())); + assert_eq!(pick_python(both, |_| true), Some(alias.clone())); + assert_eq!(pick_python(vec![alias], |_| false), None); + } + /// A user's `languages/python.toml` `[debugger]` is the one debugpy runs with: no other /// language (a template one) declares debugpy before Python. #[test]