Skip to content

Commit 1537a16

Browse files
committed
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`.
1 parent f586845 commit 1537a16

File tree

1 file changed

+15
-2
lines changed
  • crates/pet-virtualenv/src

1 file changed

+15
-2
lines changed

crates/pet-virtualenv/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
55

66
use pet_core::{
77
env::PythonEnv,
@@ -40,6 +40,19 @@ pub fn is_virtualenv_dir(path: &Path) -> bool {
4040
path = path.join("bin");
4141
}
4242
}
43+
44+
// Never consider global locations to be virtualenvs
45+
// in case there is a false positive match from checks below.
46+
if vec![
47+
PathBuf::from(r"/bin"),
48+
PathBuf::from(r"/usr/bin"),
49+
PathBuf::from(r"/usr/local/bin"),
50+
]
51+
.contains(&path)
52+
{
53+
return false;
54+
}
55+
4356
// Check if there are any activate.* files in the same directory as the interpreter.
4457
//
4558
// env
@@ -62,7 +75,7 @@ pub fn is_virtualenv_dir(path: &Path) -> bool {
6275
.unwrap_or_default()
6376
.to_str()
6477
.unwrap_or_default()
65-
.starts_with("activate")
78+
.starts_with("activate.")
6679
{
6780
return true;
6881
}

0 commit comments

Comments
 (0)