Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(install): a venv never gets --user or --break-system-packages

purple11c95e79fb96
diff --git a/src-tauri/src/commands/install/custom_nodes.rs b/src-tauri/src/commands/install/custom_nodes.rs
index 8c70d866..94aed645 100644
--- a/src-tauri/src/commands/install/custom_nodes.rs
+++ b/src-tauri/src/commands/install/custom_nodes.rs
@@ -25,7 +25,8 @@
use std::fs;
use std::path::PathBuf;
use crate::python::python_command;
-use super::pip::is_permission_denied_pip_error;
+use super::pip::{is_permission_denied_pip_error, user_site_allowed};
+use super::venv::is_venv_python;
use std::process::Stdio;
#[cfg(target_os = "windows")]
@@ -333,9 +334,13 @@ pub(crate) fn install_node_requirements(
// controlnet_aux stranded the Motion install card, 2026-07-19).
// Retry into the per-user site — the same interpreter imports from
// there, no admin needed. The Windows twin of the PEP 668 --user
- // escape above; a venv Python never hits a permission error here,
- // and if the retry fails too we surface the original diagnosis.
- if is_permission_denied_pip_error(&combined) {
+ // escape above; if the retry fails too we surface the original
+ // diagnosis. A venv is asked first and never retried: pip refuses
+ // `--user` there, whether it is ComfyUI's own venv or a fallback
+ // Python that happens to be one.
+ if is_permission_denied_pip_error(&combined)
+ && user_site_allowed(is_venv_python(&python_bin))
+ {
println!(
"[Install] {} requirements hit a permission error — retrying into the user site (--user)",
node_name
diff --git a/src-tauri/src/commands/install/mod.rs b/src-tauri/src/commands/install/mod.rs
index 446e2710..7961a18b 100644
--- a/src-tauri/src/commands/install/mod.rs
+++ b/src-tauri/src/commands/install/mod.rs
@@ -241,7 +241,7 @@ mod tests {
// install's ROCm DLLs, and the environment check died on "Bad Image".
// The venv is built on every platform, not only behind PEP 668.
assert!(
- !install_body.contains("is_pep668_protected"),
+ !install_body.contains("pep668_protected"),
"the venv is only built where PEP 668 forces it again"
);
}
diff --git a/src-tauri/src/commands/install/pip.rs b/src-tauri/src/commands/install/pip.rs
index 298bb8da..b8562805 100644
--- a/src-tauri/src/commands/install/pip.rs
+++ b/src-tauri/src/commands/install/pip.rs
@@ -916,14 +916,26 @@ pub(crate) fn requirements_failure_reason_for(f: &PipFailure) -> &'static str {
}
}
+/// Whether pip may be handed `--user` for this interpreter at all.
+///
+/// The one rule every `--user` in the installers goes through: the PEP 668
+/// escape (`venv::PythonSite::pip_escape_args`), the retry after a refused
+/// write below, and the custom node requirements. A venv REFUSES `--user`
+/// ("Can not perform a '--user' install. User site-packages are not visible
+/// in this virtualenv"), and it never needs it either, because its own
+/// site-packages belongs to the user who built it.
+pub(crate) fn user_site_allowed(in_venv: bool) -> bool {
+ !in_venv
+}
+
/// Whether a failed pip run should be tried again into the per user site.
///
-/// Two conditions, and both matter. A venv REFUSES `--user` ("Can not perform
-/// a '--user' install"), so retrying there swaps one failure for another. And
-/// only a refused write is worth retrying at all: a network failure would just
-/// fail again, twice as slowly.
+/// Two conditions, and both matter. A venv refuses `--user` (see
+/// [`user_site_allowed`]), so retrying there swaps one failure for another.
+/// And only a refused write is worth retrying at all: a network failure would
+/// just fail again, twice as slowly.
pub(crate) fn should_retry_in_user_site(in_venv: bool, stderr: &str) -> bool {
- !in_venv && pip_failure_kind(stderr) == PipFailureKind::Permission
+ user_site_allowed(in_venv) && pip_failure_kind(stderr) == PipFailureKind::Permission
}
/// True iff a failed pip run died on filesystem permissions (admin-only
diff --git a/src-tauri/src/commands/install/venv.rs b/src-tauri/src/commands/install/venv.rs
index b0bf464f..7edb4ab1 100644
--- a/src-tauri/src/commands/install/venv.rs
+++ b/src-tauri/src/commands/install/venv.rs
@@ -9,10 +9,11 @@
//! Genau diese Mitfahrer sind der Grund für die Naht. faster-whisper und
//! Piper landen in ComfyUIs venv, weil das "LUs Python" ist; die Reparatur
//! löscht dieses venv im Ganzen. Wer eines von beidem ändert, muss das
-//! andere vor Augen haben — also stehen sie in einer Datei. `is_pep668_protected`
-//! gehört dazu, weil es die Frage beantwortet, ob überhaupt ein venv nötig
-//! ist: auf einer PEP-668-Distribution ist es der einzige Weg, überhaupt zu
-//! installieren.
+//! andere vor Augen haben, also stehen sie in einer Datei. `probe_python_site`
+//! gehört dazu, weil es die Frage beantwortet, in was für einen Python pip
+//! gerade schreibt: ein venv bekommt nie `--user` und nie
+//! `--break-system-packages`, ein PEP-668-geschütztes System-Python ohne venv
+//! bekommt beides.
use std::io::Read;
use std::path::{Path, PathBuf};
@@ -24,6 +25,7 @@ use std::sync::{Arc, Mutex};
use tracing::warn;
use super::children::{wait_or_cancel, TrackedInstallerChild};
+use super::pip::user_site_allowed;
use crate::os_error;
use crate::python::{venv_python_path, venv_python_path_named};
use crate::state::AppState;
@@ -31,37 +33,107 @@ use crate::state::AppState;
// ── PEP 668 / venv helpers (Bug E — rzgrozt Arch externally-managed) ─────────
-/// True iff the Python pointed to by `python_bin` is PEP 668 protected
-/// (Arch Linux, Debian 12+, Fedora 38+, Ubuntu 23.04+ ship Python with an
-/// `EXTERNALLY-MANAGED` marker file in the stdlib dir, which makes
-/// `python -m pip install ...` exit with
-/// `error: externally-managed-environment` unless `--break-system-packages`
-/// is passed). We probe by asking Python itself whether the marker exists
-/// — robust against distro-specific path layouts and avoids parsing locale
-/// dependent pip error strings.
-///
-/// Returns `false` on any probe error (Python missing, sysconfig broken,
-/// stdout unparseable). That is the safe default: a false negative just
-/// means we install without a venv exactly like LU did before this bug,
-/// which is fine on every distro that *isn't* PEP 668 protected.
-pub fn is_pep668_protected(python_bin: &str) -> bool {
+/// Python one-liner behind [`probe_python_site`]. Prints two flags, marker
+/// and venv, e.g. `1 0`. The marker is looked up in `sysconfig`'s stdlib dir,
+/// which inside a venv still points at the BASE interpreter's stdlib, so the
+/// marker of a PEP 668 system Python is visible from a venv built on it.
+/// pip itself skips the marker check inside a venv, so the venv flag has to
+/// be read here too. `real_prefix` covers the old virtualenv package.
+const PYTHON_SITE_PROBE: &str = "import os, sys, sysconfig; \
+ d = sysconfig.get_path('stdlib'); \
+ m = os.path.exists(os.path.join(d, 'EXTERNALLY-MANAGED')); \
+ v = sys.prefix != getattr(sys, 'base_prefix', sys.prefix) or hasattr(sys, 'real_prefix'); \
+ print(int(m), int(v))";
+
+/// What [`PYTHON_SITE_PROBE`] found out about one interpreter.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) struct PythonSite {
+ /// The `EXTERNALLY-MANAGED` marker sits in the (base) stdlib dir.
+ pub(crate) externally_managed: bool,
+ /// The interpreter is a venv (`sys.prefix != sys.base_prefix`).
+ pub(crate) in_venv: bool,
+}
+
+impl PythonSite {
+ /// True iff this is a PEP 668 protected SYSTEM Python (Arch Linux,
+ /// Debian 12+, Fedora 38+, Ubuntu 23.04+ ship Python with an
+ /// `EXTERNALLY-MANAGED` marker file in the stdlib dir, which makes
+ /// `python -m pip install ...` exit with
+ /// `error: externally-managed-environment` unless
+ /// `--break-system-packages` is passed). The probe asks Python itself
+ /// whether the marker exists, which is robust against distro-specific
+ /// path layouts and avoids parsing locale dependent pip error strings.
+ ///
+ /// A venv is NEVER protected, even when it was built on a protected
+ /// system Python (zzammerr, Ubuntu 24.04, 02.10.: the ComfyUI venv saw
+ /// the base interpreter's marker, so the voice installs passed `--user`
+ /// and pip answered "Can not perform a '--user' install. User
+ /// site-packages are not visible in this virtualenv").
+ pub(crate) fn pep668_protected(self) -> bool {
+ self.externally_managed && !self.in_venv
+ }
+
+ /// Extra pip flags for an install into this interpreter. Empty for
+ /// everything except a protected system Python, where the user-site
+ /// install with the escape hatch is the only way pip will write. A venv
+ /// gets neither flag: it needs no escape hatch, and pip refuses `--user`
+ /// inside it (see [`user_site_allowed`]).
+ pub(crate) fn pip_escape_args(self) -> Vec<&'static str> {
+ if self.pep668_protected() && user_site_allowed(self.in_venv) {
+ vec!["--break-system-packages", "--user"]
+ } else {
+ Vec::new()
+ }
+ }
+}
+
+/// Parse the probe's stdout (`"<marker> <venv>"`). `None` for anything else.
+fn parse_python_site_probe(stdout: &str) -> Option<PythonSite> {
+ let mut it = stdout.split_whitespace();
+ let flag = |s: &str| match s {
+ "1" => Some(true),
+ "0" => Some(false),
+ _ => None,
+ };
+ let site = PythonSite {
+ externally_managed: flag(it.next()?)?,
+ in_venv: flag(it.next()?)?,
+ };
+ it.next().is_none().then_some(site)
+}
+
+/// Ask `python_bin` itself whether it is PEP 668 marked and whether it is a
+/// venv. `None` on any probe error (Python missing, sysconfig broken, stdout
+/// unparseable), which every caller reads as "not protected, not a venv".
+/// That is the safe default: the install goes out as a plain `pip install`
+/// exactly like on Windows / macOS / Debian 11.
+pub(crate) fn probe_python_site(python_bin: &str) -> Option<PythonSite> {
if python_bin.is_empty() {
- return false;
+ return None;
}
let mut cmd = python_command(python_bin);
- cmd.args([
- "-c",
- "import os, sysconfig; \
- d = sysconfig.get_path('stdlib'); \
- print('YES' if os.path.exists(os.path.join(d, 'EXTERNALLY-MANAGED')) else 'NO')",
- ])
- .stdout(Stdio::piped())
- .stderr(Stdio::piped());
- let Ok(out) = cmd.output() else { return false };
+ cmd.args(["-c", PYTHON_SITE_PROBE])
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ let out = cmd.output().ok()?;
if !out.status.success() {
- return false;
+ return None;
}
- String::from_utf8_lossy(&out.stdout).trim() == "YES"
+ parse_python_site_probe(&String::from_utf8_lossy(&out.stdout))
+}
+
+/// True iff `python_bin` is a venv interpreter. `false` on any probe error.
+pub(crate) fn is_venv_python(python_bin: &str) -> bool {
+ probe_python_site(python_bin).is_some_and(|site| site.in_venv)
+}
+
+/// The pip flags to append for `python_bin`: one place for every installer
+/// that targets "LU's Python", which is the ComfyUI venv when one exists and
+/// the system Python otherwise. A failed probe gives no flags at all.
+pub(crate) fn pip_escape_args_for(python_bin: &str) -> Vec<&'static str> {
+ probe_python_site(python_bin)
+ .map(PythonSite::pip_escape_args)
+ .unwrap_or_default()
}
/// Create a venv inside `comfyui_dir/venv` using the system `python_bin`.
@@ -718,20 +790,119 @@ mod tests {
// diagnose path surfaces a useful hint when the marker error reaches
// the user despite the auto-venv path.
+ const SYSTEM_PLAIN: PythonSite = PythonSite { externally_managed: false, in_venv: false };
+ const SYSTEM_MARKED: PythonSite = PythonSite { externally_managed: true, in_venv: false };
+ const VENV_ON_MARKED: PythonSite = PythonSite { externally_managed: true, in_venv: true };
+ const VENV_ON_PLAIN: PythonSite = PythonSite { externally_managed: false, in_venv: true };
+
+ #[test]
+ fn only_a_marked_system_python_is_pep668_protected() {
+ assert!(SYSTEM_MARKED.pep668_protected(), "Ubuntu 24.04 /usr/bin/python3");
+ assert!(!VENV_ON_MARKED.pep668_protected(), "ComfyUI venv on Ubuntu 24.04");
+ assert!(!SYSTEM_PLAIN.pep668_protected(), "Windows, macOS, old distros");
+ assert!(!VENV_ON_PLAIN.pep668_protected(), "ComfyUI venv on Windows or macOS");
+ }
+
+ #[test]
+ fn pip_escape_args_per_case() {
+ // No ComfyUI venv on Ubuntu 24.04, Debian 12, Arch, Fedora 38+.
+ assert_eq!(
+ SYSTEM_MARKED.pip_escape_args(),
+ vec!["--break-system-packages", "--user"]
+ );
+ // zzammerr: the ComfyUI venv on Ubuntu 24.04. pip refuses `--user`
+ // in a venv, so neither flag may appear.
+ assert!(VENV_ON_MARKED.pip_escape_args().is_empty());
+ // Windows and macOS, with and without a venv: unchanged, no flags.
+ assert!(SYSTEM_PLAIN.pip_escape_args().is_empty());
+ assert!(VENV_ON_PLAIN.pip_escape_args().is_empty());
+ }
+
+ #[test]
+ fn no_case_ever_passes_user_into_a_venv() {
+ for externally_managed in [false, true] {
+ let site = PythonSite { externally_managed, in_venv: true };
+ let args = site.pip_escape_args();
+ assert!(!args.contains(&"--user"), "{site:?}");
+ assert!(!args.contains(&"--break-system-packages"), "{site:?}");
+ }
+ }
+
+ #[test]
+ fn parse_python_site_probe_reads_both_flags() {
+ assert_eq!(parse_python_site_probe("1 0\n"), Some(SYSTEM_MARKED));
+ assert_eq!(parse_python_site_probe("1 1\r\n"), Some(VENV_ON_MARKED));
+ assert_eq!(parse_python_site_probe("0 0\n"), Some(SYSTEM_PLAIN));
+ assert_eq!(parse_python_site_probe("0 1"), Some(VENV_ON_PLAIN));
+ }
+
+ #[test]
+ fn parse_python_site_probe_rejects_anything_else() {
+ for junk in ["", "1", "YES", "NO", "garbage", "1 2", "True False", "1 0 1"] {
+ assert_eq!(parse_python_site_probe(junk), None, "{junk:?}");
+ }
+ }
+
#[test]
- fn is_pep668_protected_returns_false_for_empty_bin() {
- // Empty sentinel from python.rs::get_python_bin must short-circuit
- // to false so a missing Python doesn't accidentally trigger venv
- // creation (which would also fail and confuse the error chain).
- assert!(!is_pep668_protected(""));
+ fn probe_script_asks_for_marker_and_venv() {
+ assert!(PYTHON_SITE_PROBE.contains("EXTERNALLY-MANAGED"));
+ assert!(PYTHON_SITE_PROBE.contains("base_prefix"));
+ assert!(PYTHON_SITE_PROBE.contains("real_prefix"));
}
#[test]
- fn is_pep668_protected_returns_false_for_garbage_bin() {
- // Probing a non-existent path can't crash — the function must
- // swallow the spawn error and return false so install proceeds as
- // it always did on systems that aren't PEP 668 protected.
- assert!(!is_pep668_protected("/definitely/not/a/real/python-9.99"));
+ fn a_failed_probe_gives_no_flags_and_no_venv() {
+ for bin in ["", "/definitely/not/a/real/python-9.99"] {
+ assert_eq!(probe_python_site(bin), None, "{bin:?}");
+ assert!(pip_escape_args_for(bin).is_empty(), "{bin:?}");
+ assert!(!is_venv_python(bin), "{bin:?}");
+ }
+ }
+
+ /// A stand-in interpreter that answers every call with one fixed line,
+ /// the way [`PYTHON_SITE_PROBE`] would on that kind of Python.
+ fn write_fake_python_answering(dir: &std::path::Path, name: &str, answer: &str) -> String {
+ #[cfg(not(windows))]
+ {
+ let path = dir.join(format!("{name}.sh"));
+ std::fs::write(&path, format!("#!/bin/sh\necho {answer}\n")).unwrap();
+ let mut perms = std::fs::metadata(&path).unwrap().permissions();
+ std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o755);
+ std::fs::set_permissions(&path, perms).unwrap();
+ path.to_string_lossy().to_string()
+ }
+ #[cfg(windows)]
+ {
+ let path = dir.join(format!("{name}.bat"));
+ std::fs::write(&path, format!("@echo off\r\necho {answer}\r\n")).unwrap();
+ path.to_string_lossy().to_string()
+ }
+ }
+
+ #[test]
+ fn a_venv_on_a_protected_base_installs_without_user_or_escape_hatch() {
+ // zzammerr, Ubuntu 24.04: the target is the ComfyUI venv, the marker
+ // of /usr/lib/python3.12 shines through, and the install must still
+ // go out as a plain `pip install`.
+ let dir = tempfile::tempdir().unwrap();
+ let venv = write_fake_python_answering(dir.path(), "fake-venv-python", "1 1");
+ assert_eq!(probe_python_site(&venv), Some(VENV_ON_MARKED));
+ assert!(pip_escape_args_for(&venv).is_empty());
+ assert!(is_venv_python(&venv));
+ }
+
+ #[test]
+ fn a_protected_system_python_installs_into_the_user_site() {
+ // No ComfyUI venv (never installed, or python3-venv is missing):
+ // the user site with the escape hatch is the only way in.
+ let dir = tempfile::tempdir().unwrap();
+ let system = write_fake_python_answering(dir.path(), "fake-system-python", "1 0");
+ assert_eq!(probe_python_site(&system), Some(SYSTEM_MARKED));
+ assert_eq!(
+ pip_escape_args_for(&system),
+ vec!["--break-system-packages", "--user"]
+ );
+ assert!(!is_venv_python(&system));
}
// ── ENG-14 (matrix point 83): `python -m venv`'s hint reaches stdout ───
@@ -977,14 +1148,14 @@ mod tests {
// and check the more interesting assertions.
// ── Phase 1: PEP 668 detection ──
- let detected = is_pep668_protected(&fake_python);
+ let detected = probe_python_site(&fake_python).is_some_and(PythonSite::pep668_protected);
assert!(
detected,
- "is_pep668_protected({}) returned false — was the EXTERNALLY-MANAGED \
+ "probe_python_site({}) saw no protected Python. Was the EXTERNALLY-MANAGED \
marker planted in this Python's stdlib?",
fake_python
);
- println!("[live E2E] ✓ is_pep668_protected detected the marker");
+ println!("[live E2E] ✓ probe_python_site detected the marker");
// ── Phase 2: create_comfyui_venv ──
let comfy_root = std::env::temp_dir().join("lu-pep668-live-comfyui");
diff --git a/src-tauri/src/commands/install/voice.rs b/src-tauri/src/commands/install/voice.rs
index 02732fa2..c1771b9e 100644
--- a/src-tauri/src/commands/install/voice.rs
+++ b/src-tauri/src/commands/install/voice.rs
@@ -25,7 +25,7 @@ use crate::os_error;
use crate::python::python_command;
use super::pip::pip_install_streaming_with_retry_cancellable;
-use super::venv::{is_pep668_protected, resolve_lu_python};
+use super::venv::{pip_escape_args_for, resolve_lu_python};
// ── Whisper (faster-whisper) installer (§24.9 — STT install affordance) ──────
@@ -104,11 +104,8 @@ pub fn install_whisper(
// bare `pip install` dies with externally-managed-environment. When that's
// the target (no ComfyUI venv absorbed it), install into the user site
// with the escape hatch so STT installs there too (joerack, Arch). No-op
- // on Windows/macOS/venv Pythons (not PEP 668 protected).
- if is_pep668_protected(&target_python) {
- args.push("--break-system-packages");
- args.push("--user");
- }
+ // on Windows/macOS and in any venv, which pip refuses `--user` for.
+ args.extend(pip_escape_args_for(&target_python));
// No cancel flag — this single pip install is short relative to the
// ComfyUI PyTorch download, so we run it to completion like install_python.
match pip_install_streaming_with_retry_cancellable(&args, &target_python, 3, &install_state, None) {
@@ -249,11 +246,8 @@ pub fn install_tts(
);
let mut args = build_tts_pip_args();
// PEP 668 escape hatch (Arch / Debian 12+ / Fedora 38+) — see
- // install_whisper. No-op on Windows/macOS/venv Pythons.
- if is_pep668_protected(&target_python) {
- args.push("--break-system-packages");
- args.push("--user");
- }
+ // install_whisper. No-op on Windows/macOS and in any venv.
+ args.extend(pip_escape_args_for(&target_python));
pip_install_streaming_with_retry_cancellable(&args, &target_python, 3, &install_state, None)
};
match pip_result {