Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 8af09aaf58 fix(tools): install debugpy when the project's Python has no pip
debugpy's private copy was made with `python -m pip` of the selected interpreter.
A virtualenv made by uv has no pip, so with such a project interpreter the
install failed with "No module named pip". The copy is a universal wheel and
doesn't need that interpreter's pip: when it has none, pip runs through
`uv tool run pip`, or another Python on PATH that has pip.
2026-11-16 11:25:03 +08:00
2 змінених файлів з 88 додано та 2 видалено

No files matched your search

+38 -2
Переглянути файл
@@ -1256,6 +1256,42 @@ impl Tools {
Err(IdeError::new(ErrorKind::Io, format!("pip install {package} failed ({status})")))
}
/// A command running pip, before its arguments. The interpreter's own pip when it has one;
/// a virtualenv made by uv has none, so then pip through `uv tool run`, or another Python
/// on `PATH` that has it.
fn pip_command(&self, python: PathBuf, emit: Emit) -> Command {
let has_pip = |python: &Path| {
let mut c = self.python_command(python);
c.args(["-m", "pip", "--version"]);
run_quiet(c, Duration::from_secs(20)).is_some_and(|(ok, _)| ok)
};
let module = |python: &Path| {
let mut c = self.python_command(python);
c.args(["-m", "pip"]);
c
};
if has_pip(&python) {
return module(&python);
}
let note = format!("{} has no pip", python.display());
if let Some(uv) = self.which("uv") {
emit(InstallEvent::Line { text: format!("{note}; running pip with uv") });
// pip runs in uv's own Python: the same private working directory applies.
let mut c = self.python_command(uv);
c.args(["tool", "run", "pip"]);
return c;
}
let other = ["python3", "python"]
.into_iter()
.filter_map(|name| ide_core::tool_path::find_command(name, &self.system_path, &self.root))
.find(|p| *p != python && has_pip(p));
if let Some(other) = other {
emit(InstallEvent::Line { text: format!("{note}; using {}", other.display()) });
return module(&other);
}
module(&python)
}
/// The universal (`py3-none-any`) wheel, so the copy works with every interpreter
/// version a project selects, not just the one that installed it.
fn install_pip_target(
@@ -1269,8 +1305,8 @@ impl Tools {
let _ = std::fs::remove_dir_all(&dir);
let lib = python_lib_dir(&self.root, spec.name);
std::fs::create_dir_all(&lib)?;
let mut c = self.python_command(self.python().expect("checked"));
c.args(["-m", "pip", "install", "--disable-pip-version-check", "--no-deps", "--upgrade", "--target"])
let mut c = self.pip_command(self.python().expect("checked"), emit);
c.args(["install", "--disable-pip-version-check", "--no-deps", "--upgrade", "--target"])
.arg(&lib)
.args(["--only-binary=:all:", "--platform", "any", "--implementation", "py"])
.arg(package);
+50
Переглянути файл
@@ -895,9 +895,14 @@ esac
}
}
/// Installs of one tool name share a process-wide slot: tests installing debugpy take turns.
#[cfg(unix)]
static DEBUGPY_INSTALL: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
#[cfg(unix)]
#[test]
fn selected_python_is_used_for_pip_and_uv_even_with_a_versioned_filename() {
let _guard = DEBUGPY_INSTALL.lock();
let env = Env::new();
env.script(&env.sys, "python3", "exit 99");
let sdk = env._tmp.path().join("python-sdk");
@@ -935,6 +940,51 @@ exit 1
assert!(args.contains(&format!("--python {}", sdk.join("python3.14").display())), "{args}");
}
/// A virtualenv made by uv has no pip: debugpy's private copy is made with pip through uv.
#[cfg(unix)]
#[test]
fn debugpy_installs_through_uv_when_the_selected_python_has_no_pip() {
let _guard = DEBUGPY_INSTALL.lock();
let env = Env::new();
let venv = env._tmp.path().join("project/.venv/bin");
env.script(&venv, "python", "echo \"$(basename \"$0\"): No module named pip\" >&2; exit 1");
let log = env._tmp.path().join("uv.log");
env.script(
&env.sys,
"uv",
&format!(
r#"
echo "$@" >> '{log}'
while [ $# -gt 0 ]; do
if [ "$1" = "--target" ]; then
/bin/mkdir -p "$2/debugpy" "$2/debugpy-1.8.0.dist-info"
/usr/bin/touch "$2/debugpy/__init__.py"
exit 0
fi
shift
done
exit 1
"#,
log = log.display()
),
);
let tools = env.tools().with_runtime(BTreeMap::from([("python".into(), venv.join("python"))]), BTreeMap::new());
let lines = Mutex::new(Vec::new());
let info = tools
.install("debugpy", &langs(), &CancellationToken::new(), &|e| {
if let InstallEvent::Line { text } = e {
lines.lock().push(text);
}
})
.unwrap();
assert_eq!(info.status, ToolStatus::Installed);
assert_eq!(info.version.as_deref(), Some("1.8.0"));
let args = fs::read_to_string(log).unwrap();
assert!(args.starts_with("tool run pip install "), "{args}");
assert!(args.contains("--platform any"), "{args}");
assert!(lines.lock().iter().any(|l| l.contains("has no pip")));
}
#[test]
fn a_cancelled_install_leaves_the_previous_one_in_place() {
let tmp = tempfile::tempdir().unwrap();