Skip to content

Commit a8f0352

Browse files
committed
fix: refine conda environment name retrieval for external environments to ensure safe activation
1 parent 8fb7ab6 commit a8f0352

File tree

1 file changed

+9
-46
lines changed

1 file changed

+9
-46
lines changed

crates/pet-conda/src/environments.rs

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ fn get_conda_env_name(
222222
* And example is `# cmd: conda create -n sample``
223223
*
224224
* This function returns the name of the conda environment based on how it was created:
225-
* - If created with --prefix/-p (path-based): returns None (activation must use full path)
226225
* - If created with --name/-n (name-based): returns the folder name (can use named activation)
227-
* - If we can't determine: returns the folder name (preserve existing behavior)
226+
* - Otherwise: returns None (activation must use full path for safety)
227+
*
228+
* This is used for external environments (not under conda_dir). We only return a name
229+
* if we can positively confirm it was created with -n/--name, because that's the only
230+
* case where name-based activation works reliably for external environments.
228231
*/
229232
fn get_conda_env_name_from_history_file(env_path: &Path, prefix: &Path) -> Option<String> {
230233
let name = prefix
@@ -239,33 +242,15 @@ fn get_conda_env_name_from_history_file(env_path: &Path, prefix: &Path) -> Optio
239242
// # cmd: /Users/donjayamanne/miniconda3/bin/conda create -n conda1
240243
// # cmd: /usr/bin/conda create --prefix ./prefix-envs/.conda1 python=3.12 -y
241244

242-
// Check if environment was created with --prefix/-p (path-based)
243-
// In this case, return None so activation uses full path
244-
if is_path_based_conda_env(&line) {
245-
return None;
246-
}
247-
248-
// Check if environment was created with --name/-n (name-based)
249-
// In this case, return the folder name for named activation
245+
// Only return a name if we can confirm it was created with -n/--name
246+
// This matches the original behavior where we checked for the exact name in the cmd
250247
if is_name_based_conda_env(&line) {
251248
return Some(name.clone());
252249
}
253250
}
254251
}
255-
// If we can't determine from history, return folder name (preserve existing behavior)
256-
name
257-
}
258-
259-
/// Check if the conda create command used --prefix or -p (path-based environment)
260-
fn is_path_based_conda_env(cmd_line: &str) -> bool {
261-
// Look for " -p " or " --prefix " after "create"
262-
// We need to be careful to match the flag, not just any occurrence of -p
263-
let lower = cmd_line.to_lowercase();
264-
if let Some(create_idx) = lower.find(" create ") {
265-
let after_create = &lower[create_idx..];
266-
return after_create.contains(" -p ") || after_create.contains(" --prefix ");
267-
}
268-
false
252+
// If we can't confirm name-based creation, return None for safe path-based activation
253+
None
269254
}
270255

271256
/// Check if the conda create command used --name or -n (name-based environment)
@@ -403,28 +388,6 @@ mod tests {
403388
);
404389
}
405390

406-
#[test]
407-
fn test_is_path_based_conda_env() {
408-
// Path-based environments use --prefix or -p
409-
assert!(is_path_based_conda_env(
410-
"# cmd: /usr/bin/conda create --yes --prefix .conda python=3.12"
411-
));
412-
assert!(is_path_based_conda_env(
413-
"# cmd: /usr/bin/conda create -p .conda python=3.12"
414-
));
415-
assert!(is_path_based_conda_env(
416-
"# cmd: C:\\Users\\user\\miniconda3\\Scripts\\conda.exe create --prefix .conda python=3.9"
417-
));
418-
419-
// Name-based environments use --name or -n
420-
assert!(!is_path_based_conda_env(
421-
"# cmd: /usr/bin/conda create -n myenv python=3.12"
422-
));
423-
assert!(!is_path_based_conda_env(
424-
"# cmd: /usr/bin/conda create --name myenv python=3.12"
425-
));
426-
}
427-
428391
#[test]
429392
fn test_is_name_based_conda_env() {
430393
// Name-based environments use --name or -n

0 commit comments

Comments
 (0)