From e15d1637b0b4f904410bf7bf613e59536be3894a Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:37:28 +0700 Subject: [PATCH] fix(cli): env remove finds the deleted venv it exists for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new integration test found it: removing an added venv by its directory answered 'not on the list' once the venv was gone — the very case remove is for — because interpreter_in resolves only an interpreter that exists and fell back to the bare directory. The directory is now also matched as the venv whose interpreter it would hold (envs::venv_interpreter), existing or not. tests/manual_envs.rs drives the binary with an app-data root of its own and no Python: a non-interpreter is refused and nothing stored; an added interpreter whose file is gone is listed with PD-ENV-001 and removed by its directory; removing it again is not an error. The first failed against the code before this fix. Co-Authored-By: Claude Opus 5.5 --- crates/pipdock-cli/src/run.rs | 16 +-- crates/pipdock-cli/tests/manual_envs.rs | 128 ++++++++++++++++++++++++ crates/pipdock-core/src/envs/mod.rs | 20 ++-- 3 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 crates/pipdock-cli/tests/manual_envs.rs diff --git a/crates/pipdock-cli/src/run.rs b/crates/pipdock-cli/src/run.rs index 7dcf0a5..3f325a5 100644 --- a/crates/pipdock-cli/src/run.rs +++ b/crates/pipdock-cli/src/run.rs @@ -1074,18 +1074,22 @@ pub async fn env_add(opts: &GlobalOpts, path: &Path) -> Result { /// /// Matched by identity, and by the path as stored when the file is gone — a deleted venv cannot be /// canonicalized into the identity it was added under, and it is exactly the entry someone wants -/// gone. Not being on the list is not an error: the list is what was asked about, and it is right. +/// gone. A directory is matched as the venv whose interpreter it would hold, existing or not: +/// `interpreter_in` resolves only what is there, and the first version of this command answered +/// "not on the list" for the one case it exists for — found by its own test. Not being on the list +/// is not an error: the list is what was asked about, and it is right. /// /// # Errors /// `PD-INT-001` when the store cannot be read or written. pub fn env_remove(opts: &GlobalOpts, path: &Path) -> Result { let store = Store::open(&app_data_dir())?; let interpreter = envs::interpreter_in(path); - let wanted = envs::env_hash(&interpreter); - let entry = store - .manual_envs()? - .into_iter() - .find(|m| m.env_hash == wanted || same_path(&m.interpreter, &interpreter)); + let candidates = [interpreter.clone(), envs::venv_interpreter(path)]; + let entry = store.manual_envs()?.into_iter().find(|m| { + candidates + .iter() + .any(|c| m.env_hash == envs::env_hash(c) || same_path(&m.interpreter, c)) + }); let removed = match &entry { Some(m) => store.remove_manual_env(&m.env_hash)?, None => false, diff --git a/crates/pipdock-cli/tests/manual_envs.rs b/crates/pipdock-cli/tests/manual_envs.rs new file mode 100644 index 0000000..062c5f5 --- /dev/null +++ b/crates/pipdock-cli/tests/manual_envs.rs @@ -0,0 +1,128 @@ +//! `env add`, `env remove` and `env list` over the list the GUI's *Add interpreter…* shares (1.6.0). +//! +//! The paths that need no Python, which are the ones a refactor is likeliest to break unseen: a +//! file that is not an interpreter is refused before anything runs it and nothing is stored; an +//! added interpreter whose file has since gone is still listed — with `PD-ENV-001`, because that is +//! the row a person needs in order to remove it — and is removed by the path it was added under. +//! The success path of `env add` probes a real interpreter, which `ci-integration.yml` has. +//! +//! Each test has an app-data root of its own: the store is a file, and tests run in parallel. + +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use std::path::{Path, PathBuf}; + +use assert_cmd::Command; +use pipdock_core::store::Store; + +fn scratch(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("pd-manual-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).expect("create scratch dir"); + dir +} + +/// `%LOCALAPPDATA%\PipDock\data`, as the binary will compute it from `LOCALAPPDATA`. +fn app_data(local: &Path) -> PathBuf { + local + .join(pipdock_core::APP_DATA_DIR_NAME) + .join(pipdock_core::APP_DATA_SUBDIR) +} + +fn pipdock(local: &Path) -> Command { + let mut cmd = Command::cargo_bin("pipdock").expect("the `pipdock` binary builds"); + cmd.env("NO_COLOR", "1") + .env("COLUMNS", "100") + .env("LOCALAPPDATA", local); + cmd +} + +fn manual_rows(local: &Path) -> Vec { + let out = pipdock(local) + .args(["env", "list", "--json"]) + .output() + .expect("runs"); + assert!(out.status.success(), "env list exits 0"); + let all: Vec = + serde_json::from_slice(&out.stdout).expect("env list --json is one JSON document"); + all.into_iter() + .filter(|row| row["source"] == "manual") + .collect() +} + +#[test] +fn another_program_is_refused_before_it_runs_and_nothing_is_stored() { + let local = scratch("refused"); + let other = local.join(if cfg!(windows) { + "notepad.exe" + } else { + "notepad" + }); + std::fs::write(&other, "").expect("an empty file that is not Python"); + + let out = pipdock(&local) + .args(["env", "add"]) + .arg(&other) + .output() + .expect("runs"); + assert_eq!( + out.status.code(), + Some(3), + "PD-ENV-001 is an environment error" + ); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!(stderr.contains("error[PD-ENV-001]"), "{stderr}"); + assert!(stderr.contains("is not a Python interpreter"), "{stderr}"); + let store = Store::open(&app_data(&local)).expect("the store opens"); + assert!( + store.manual_envs().expect("reads").is_empty(), + "a refused path is never listed" + ); +} + +#[test] +fn an_entry_whose_file_is_gone_is_listed_with_its_error_and_removed_by_its_path() { + let local = scratch("gone"); + let gone = local + .join("deleted-venv") + .join("Scripts") + .join("python.exe"); + { + let store = Store::open(&app_data(&local)).expect("the store opens"); + store + .add_manual_env( + &pipdock_core::envs::env_hash(&gone), + &gone.display().to_string(), + "2026-11-21T00:00:00Z", + ) + .expect("added, as a venv later deleted would have been"); + } + + let rows = manual_rows(&local); + assert_eq!(rows.len(), 1, "{rows:?}"); + assert_eq!(rows[0]["interpreter"], gone.display().to_string()); + assert_eq!(rows[0]["error"]["code"], "PD-ENV-001"); + + // By the environment directory, as a person would type it: the file cannot be canonicalized + // any more, so the stored path is what matches. + let venv = local.join("deleted-venv"); + let out = pipdock(&local) + .args(["env", "remove"]) + .arg(&venv) + .output() + .expect("runs"); + assert!(out.status.success()); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!(stdout.contains("removed from the list"), "{stdout}"); + assert!(manual_rows(&local).is_empty(), "gone from the listing"); + + // Removing it again is not an error: the list is right. + let out = pipdock(&local) + .args(["env", "remove", "--json"]) + .arg(&gone) + .output() + .expect("runs"); + assert!(out.status.success()); + let doc: serde_json::Value = serde_json::from_slice(&out.stdout).expect("one JSON document"); + assert_eq!(doc["removed"], false); +} diff --git a/crates/pipdock-core/src/envs/mod.rs b/crates/pipdock-core/src/envs/mod.rs index e3f58b8..cbb658b 100644 --- a/crates/pipdock-core/src/envs/mod.rs +++ b/crates/pipdock-core/src/envs/mod.rs @@ -318,12 +318,7 @@ pub fn interpreter_in(path: &Path) -> PathBuf { if path.is_file() { return path.to_path_buf(); } - let exe: PathBuf = if cfg!(windows) { - ["Scripts", "python.exe"].iter().collect() - } else { - ["bin", "python"].iter().collect() - }; - let candidate = path.join(&exe); + let candidate = venv_interpreter(path); if candidate.is_file() { candidate } else { @@ -331,6 +326,19 @@ pub fn interpreter_in(path: &Path) -> PathBuf { } } +/// Where a virtual environment's interpreter is, whether or not it is there: `\Scripts\python.exe` +/// on Windows, `/bin/python` elsewhere. `pipdock env remove ` needs the answer for a venv +/// that has been deleted — exactly the entry someone wants gone, and one [`interpreter_in`] can no +/// longer resolve. +#[must_use] +pub fn venv_interpreter(dir: &Path) -> PathBuf { + if cfg!(windows) { + dir.join("Scripts").join("python.exe") + } else { + dir.join("bin").join("python") + } +} + /// Where a discovery sweep currently is. /// /// ARCHITECTURE §7 names a `scan-progress` event and never says what it carries; this is the