From fc20d015934ceca2a3eaf0a45e161dc190bd6f5d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 3 Dec 2023 23:53:26 -0500 Subject: [PATCH] Ignore empty `VIRTUAL_ENV` variables (#536) I'm not sure how my interpreter gets into this state, but it's certainly wrong to respect these. --- crates/puffin-cli/src/commands/clean.rs | 4 ++-- crates/puffin-interpreter/src/lib.rs | 6 +++--- crates/puffin-interpreter/src/virtual_env.rs | 22 +++++++------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/crates/puffin-cli/src/commands/clean.rs b/crates/puffin-cli/src/commands/clean.rs index eefaf7a02..0b13001c9 100644 --- a/crates/puffin-cli/src/commands/clean.rs +++ b/crates/puffin-cli/src/commands/clean.rs @@ -31,10 +31,10 @@ pub(crate) fn clean(cache: &Cache, mut printer: Printer) -> Result { { if entry.file_type()?.is_dir() { fs::remove_dir_all(entry.path()) - .with_context(|| format!("Failed to clear cache at {}", cache.root().display()))?; + .with_context(|| format!("Failed to clear cache at: {}", cache.root().display()))?; } else { fs::remove_file(entry.path()) - .with_context(|| format!("Failed to clear cache at {}", cache.root().display()))?; + .with_context(|| format!("Failed to clear cache at: {}", cache.root().display()))?; } } diff --git a/crates/puffin-interpreter/src/lib.rs b/crates/puffin-interpreter/src/lib.rs index 5d64d9cb5..80875b9f7 100644 --- a/crates/puffin-interpreter/src/lib.rs +++ b/crates/puffin-interpreter/src/lib.rs @@ -15,17 +15,17 @@ mod virtual_env; pub enum Error { #[error("Expected {0} to be a virtual environment, but pyvenv.cfg is missing")] MissingPyVenvCfg(PathBuf), - #[error("Your virtualenv at {0} is broken. It contains a pyvenv.cfg but no python at {1}")] + #[error("Detected a broken virtualenv at: {0}. It contains a pyvenv.cfg but no Python binary at: {1}")] BrokenVenv(PathBuf, PathBuf), #[error("Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them.")] Conflict, - #[error("Couldn't find a virtualenv or conda environment (Looked for VIRTUAL_ENV, CONDA_PREFIX and .venv)")] + #[error("Failed to locate a virtualenv or Conda environment (checked: VIRTUAL_ENV, CONDA_PREFIX, and .venv)")] NotFound, #[error(transparent)] Io(#[from] io::Error), #[error("Invalid modified date on {0}")] SystemTime(PathBuf, #[source] SystemTimeError), - #[error("Failed to query python interpreter at {interpreter}")] + #[error("Failed to query python interpreter at: {interpreter}")] PythonSubcommandLaunch { interpreter: PathBuf, #[source] diff --git a/crates/puffin-interpreter/src/virtual_env.rs b/crates/puffin-interpreter/src/virtual_env.rs index a9ced6da2..7853980b1 100644 --- a/crates/puffin-interpreter/src/virtual_env.rs +++ b/crates/puffin-interpreter/src/virtual_env.rs @@ -32,17 +32,6 @@ impl Virtualenv { }) } - pub fn from_virtualenv(platform: Platform, root: &Path, cache: &Cache) -> Result { - let platform = PythonPlatform::from(platform); - let executable = platform.venv_python(root); - let interpreter = Interpreter::query(&executable, platform.0, cache)?; - - Ok(Self { - root: root.to_path_buf(), - interpreter, - }) - } - /// Creating a new venv from a python interpreter changes this pub fn new_prefix(venv: &Path, interpreter: &Interpreter) -> Self { Self { @@ -91,17 +80,20 @@ impl Virtualenv { /// Locate the current virtual environment. pub(crate) fn detect_virtual_env(target: &PythonPlatform) -> Result, Error> { - match (env::var_os("VIRTUAL_ENV"), env::var_os("CONDA_PREFIX")) { + match ( + env::var_os("VIRTUAL_ENV").filter(|value| !value.is_empty()), + env::var_os("CONDA_PREFIX").filter(|value| !value.is_empty()), + ) { (Some(dir), None) => { debug!( - "Found a virtualenv through VIRTUAL_ENV at {}", + "Found a virtualenv through VIRTUAL_ENV at: {}", Path::new(&dir).display() ); return Ok(Some(PathBuf::from(dir))); } (None, Some(dir)) => { debug!( - "Found a virtualenv through CONDA_PREFIX at {}", + "Found a virtualenv through CONDA_PREFIX at: {}", Path::new(&dir).display() ); return Ok(Some(PathBuf::from(dir))); @@ -126,7 +118,7 @@ pub(crate) fn detect_virtual_env(target: &PythonPlatform) -> Result