Skip to content

Commit ae335aa

Browse files
committed
Fix to detect venv with pipenv installed in it as pipenv environment
1 parent 52b4e23 commit ae335aa

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

crates/pet-pipenv/src/lib.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,35 @@ mod env_variables;
2020

2121
fn get_pipenv_project(env: &PythonEnv) -> Option<PathBuf> {
2222
if let Some(prefix) = &env.prefix {
23-
get_pipenv_project_from_prefix(prefix)
24-
} else {
25-
// If the parent is bin or script, then get the parent.
26-
let bin = env.executable.parent()?;
27-
if bin.file_name().unwrap_or_default() == Path::new("bin")
28-
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
29-
{
30-
get_pipenv_project_from_prefix(env.executable.parent()?.parent()?)
31-
} else {
32-
get_pipenv_project_from_prefix(env.executable.parent()?)
23+
if let Some(project) = get_pipenv_project_from_prefix(prefix) {
24+
return Some(project);
25+
}
26+
}
27+
28+
// We can also have a venv in the workspace that has pipenv installed in it.
29+
// In such cases, the project is the workspace folder containing the venv.
30+
if let Some(project) = &env.project {
31+
if project.join("Pipfile").exists() {
32+
return Some(project.clone());
3333
}
3434
}
35+
36+
// If the parent is bin or script, then get the parent.
37+
let bin = env.executable.parent()?;
38+
if bin.file_name().unwrap_or_default() == Path::new("bin")
39+
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
40+
{
41+
get_pipenv_project_from_prefix(env.executable.parent()?.parent()?)
42+
} else {
43+
get_pipenv_project_from_prefix(env.executable.parent()?)
44+
}
3545
}
3646

3747
fn get_pipenv_project_from_prefix(prefix: &Path) -> Option<PathBuf> {
3848
let project_file = prefix.join(".project");
49+
if !project_file.exists() {
50+
return None;
51+
}
3952
let contents = fs::read_to_string(project_file).ok()?;
4053
let project_folder = norm_case(PathBuf::from(contents.trim().to_string()));
4154
if project_folder.exists() {
@@ -45,12 +58,24 @@ fn get_pipenv_project_from_prefix(prefix: &Path) -> Option<PathBuf> {
4558
}
4659
}
4760

61+
fn is_pipenv_from_project(env: &PythonEnv) -> bool {
62+
if let Some(project) = &env.project {
63+
if project.join("Pipfile").exists() {
64+
return true;
65+
}
66+
}
67+
false
68+
}
69+
4870
fn is_pipenv(env: &PythonEnv, env_vars: &EnvVariables) -> bool {
4971
if let Some(project_path) = get_pipenv_project(env) {
5072
if project_path.join(env_vars.pipenv_pipfile.clone()).exists() {
5173
return true;
5274
}
5375
}
76+
if is_pipenv_from_project(env) {
77+
return true;
78+
}
5479
// If we have a Pipfile, then this is a pipenv environment.
5580
// Else likely a virtualenvwrapper or the like.
5681
if let Some(project_path) = get_pipenv_project(env) {

0 commit comments

Comments
 (0)