From 1537a16315bfcbac15ee0acbb4943e93e3390ce5 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Thu, 13 Mar 2025 09:38:25 -0700 Subject: [PATCH 1/2] Avoid misdetecting global Linux Python as virtualenv `/usr/bin` on Linux might contain unrelated 'activate.*` binaries which would make `is_virtualenv_dir` mistake it for a virtualenv: Environment (VirtualEnv) Executable : /usr/bin/python Symlinks : "/usr/bin/python" Environment (VirtualEnv) Executable : /bin/python Symlinks : "/bin/python" Environment (VirtualEnv) Executable : /usr/bin/python3 Symlinks : "/usr/bin/python3" Environment (VirtualEnv) Executable : /bin/python3 Symlinks : "/bin/python3" Environment (VirtualEnv) Executable : /usr/bin/python3.10 Symlinks : "/usr/bin/python3.10" Environment (VirtualEnv) Executable : /bin/python3.10 Symlinks : "/bin/python3.10" Environment (VirtualEnv) Executable : /usr/bin/python3.11 Symlinks : "/usr/bin/python3.11" This fix reduces the likelihood of this happening by excluding the common `bin` directories as well as tightening the `activate` test to avoid matching binaries like `activate-ssh-agent`. --- crates/pet-virtualenv/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/pet-virtualenv/src/lib.rs b/crates/pet-virtualenv/src/lib.rs index 82d5894c..06f83f20 100644 --- a/crates/pet-virtualenv/src/lib.rs +++ b/crates/pet-virtualenv/src/lib.rs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::path::Path; +use std::path::{Path, PathBuf}; use pet_core::{ env::PythonEnv, @@ -40,6 +40,19 @@ pub fn is_virtualenv_dir(path: &Path) -> bool { path = path.join("bin"); } } + + // Never consider global locations to be virtualenvs + // in case there is a false positive match from checks below. + if vec![ + PathBuf::from(r"/bin"), + PathBuf::from(r"/usr/bin"), + PathBuf::from(r"/usr/local/bin"), + ] + .contains(&path) + { + return false; + } + // Check if there are any activate.* files in the same directory as the interpreter. // // env @@ -62,7 +75,7 @@ pub fn is_virtualenv_dir(path: &Path) -> bool { .unwrap_or_default() .to_str() .unwrap_or_default() - .starts_with("activate") + .starts_with("activate.") { return true; } From aeab977be5bfe5821eea09edb737ff3a89266310 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Thu, 13 Mar 2025 12:48:46 -0700 Subject: [PATCH 2/2] fixup! Avoid misdetecting global Linux Python as virtualenv --- crates/pet-virtualenv/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pet-virtualenv/src/lib.rs b/crates/pet-virtualenv/src/lib.rs index 06f83f20..e11a0cff 100644 --- a/crates/pet-virtualenv/src/lib.rs +++ b/crates/pet-virtualenv/src/lib.rs @@ -43,7 +43,7 @@ pub fn is_virtualenv_dir(path: &Path) -> bool { // Never consider global locations to be virtualenvs // in case there is a false positive match from checks below. - if vec![ + if [ PathBuf::from(r"/bin"), PathBuf::from(r"/usr/bin"), PathBuf::from(r"/usr/local/bin"),