use super::*;
pub(in crate::app) fn project_python(root: &Path) -> PathBuf {
project_python_on(root, std::env::var_os("PATH").as_deref())
.unwrap_or_else(|| PathBuf::from("python3"))
}
pub(in crate::app) fn project_python_on(
root: &Path,
path_var: Option<&std::ffi::OsStr>,
) -> Option<PathBuf> {
crate::python_env::venv_python_in(root)
.or_else(|| crate::proc::find::on_path_in("python3", path_var))
}
pub(in crate::app) fn find_python_venv(
start: &Path,
workspace_root: &Path,
) -> Option<(PathBuf, PathBuf)> {
crate::python_env::nearest_venv(start, workspace_root)
}
pub(in crate::app) fn project_python_for(dir: &Path, workspace_root: &Path) -> PathBuf {
find_python_venv(dir, workspace_root)
.map(|(py, _)| py)
.unwrap_or_else(|| project_python(workspace_root))
}
pub(in crate::app) fn debuggee_python(
dir: &Path,
workspace_root: &Path,
path_var: Option<&std::ffi::OsStr>,
) -> Option<PathBuf> {
find_python_venv(dir, workspace_root)
.map(|(py, _)| py)
.or_else(|| project_python_on(workspace_root, path_var))
}
pub(in crate::app) fn debuggee_python_note(python: &Path) -> Result<Option<String>, String> {
use crate::dap::install::{DEBUGPY_MIN_PYTHON, python_version, pyvenv_cfg};
use crate::dap::remote_attach::MIN_ATTACH_VERSION;
let Some(version) = python_version(python) else {
return Ok(None);
};
let (major, minor) = (version.major, version.minor);
let label = pyvenv_cfg(python)
.and_then(|cfg| Some(cfg.parent()?.file_name()?.to_string_lossy().into_owned()))
.unwrap_or_else(|| python.display().to_string());
if (major, minor) < (DEBUGPY_MIN_PYTHON.major, DEBUGPY_MIN_PYTHON.minor) {
return Err(format!(
"Python {major}.{minor} ({label}) is too old to debug: debugpy needs {}.{}+",
DEBUGPY_MIN_PYTHON.major, DEBUGPY_MIN_PYTHON.minor
));
}
if !version.supports_remote_attach() {
return Ok(Some(format!(
"under Python {major}.{minor} ({label}); attach needs {}.{}+",
MIN_ATTACH_VERSION.major, MIN_ATTACH_VERSION.minor
)));
}
Ok(None)
}
pub(in crate::app) fn with_python_note(lead: String, note: Option<&str>) -> String {
match note {
Some(note) => format!("{lead} {note}"),
None => lead,
}
}
pub(in crate::app) fn launch_request_python(
request: &serde_json::Value,
adapter_dir: &Path,
) -> Option<PathBuf> {
let args = &request["arguments"];
let python = args.get("python").or_else(|| args.get("pythonPath"))?;
let python = PathBuf::from(python.as_str().or_else(|| python.get(0)?.as_str())?);
if python.is_absolute() || python.components().count() < 2 {
return Some(python);
}
let program_dir = args["program"]
.as_str()
.and_then(|p| Path::new(p).parent())
.filter(|dir| !dir.as_os_str().is_empty());
let launch_dir = match args["cwd"].as_str() {
Some(cwd) => adapter_dir.join(cwd),
None => program_dir.map_or_else(|| adapter_dir.to_path_buf(), |d| adapter_dir.join(d)),
};
Some(launch_dir.join(python))
}
pub(in crate::app) fn debug_test_python(
request: &serde_json::Value,
root: &Path,
adapter_py: &Path,
project_found: bool,
) -> Option<PathBuf> {
launch_request_python(request, root).filter(|py| project_found || py != adapter_py)
}
pub(in crate::app) fn config_project_python(
rc: &crate::dap::configs::ResolvedConfig,
cwd: &Path,
workspace_root: &Path,
adapter_python: &Path,
) -> PathBuf {
let program = rc.program.as_deref().filter(|_| rc.module.is_none());
let dir = program
.map(|p| crate::dap::configs::absolute_in(p, cwd))
.and_then(|p| p.parent().map(Path::to_path_buf))
.unwrap_or_else(|| cwd.to_path_buf());
debuggee_python(&dir, workspace_root, std::env::var_os("PATH").as_deref())
.unwrap_or_else(|| adapter_python.to_path_buf())
}