@@ -13,6 +13,7 @@ use std::{
1313 env, fs,
1414 path:: { Path , PathBuf } ,
1515 thread,
16+ time:: SystemTime ,
1617} ;
1718
1819const APP_NAME : & str = "conda" ;
@@ -21,6 +22,7 @@ pub fn get_conda_environment_paths(
2122 env_vars : & EnvVariables ,
2223 conda_executable : & Option < PathBuf > ,
2324) -> Vec < PathBuf > {
25+ let start = SystemTime :: now ( ) ;
2426 let mut env_paths = thread:: scope ( |s| {
2527 let mut envs = vec ! [ ] ;
2628 for thread in [
@@ -43,7 +45,7 @@ pub fn get_conda_environment_paths(
4345 // & then iterate through the list of envs in the envs directory.
4446 // let env_paths = vec![];
4547 let mut threads = vec ! [ ] ;
46- for path in env_paths {
48+ for path in env_paths. iter ( ) . filter ( |f| f . exists ( ) ) {
4749 let path = path. clone ( ) ;
4850 threads. push ( thread:: spawn ( move || get_environments ( & path) ) ) ;
4951 }
@@ -57,6 +59,10 @@ pub fn get_conda_environment_paths(
5759
5860 result. sort ( ) ;
5961 result. dedup ( ) ;
62+ trace ! (
63+ "Time taken to get conda environment paths: {:?}" ,
64+ start. elapsed( ) . unwrap( )
65+ ) ;
6066 result
6167}
6268
@@ -68,13 +74,24 @@ fn get_conda_environment_paths_from_conda_rc(env_vars: &EnvVariables) -> Vec<Pat
6874 // Use the conda rc directories as well.
6975 let mut env_dirs = vec ! [ ] ;
7076 for rc_file_dir in get_conda_rc_search_paths ( env_vars) {
77+ if !rc_file_dir. exists ( ) {
78+ continue ;
79+ }
80+
7181 if let Some ( conda_rc) = Condarc :: from_path ( & rc_file_dir) {
7282 trace ! (
7383 "Conda environments in .condarc {:?} {:?}" ,
7484 conda_rc. files,
7585 conda_rc. env_dirs
7686 ) ;
77- env_dirs. append ( & mut conda_rc. env_dirs . clone ( ) ) ;
87+ env_dirs. append (
88+ & mut conda_rc
89+ . env_dirs
90+ . clone ( )
91+ . into_iter ( )
92+ . filter ( |f| f. exists ( ) )
93+ . collect ( ) ,
94+ ) ;
7895 }
7996
8097 if rc_file_dir. is_dir ( ) {
@@ -151,6 +168,9 @@ fn get_conda_environment_paths_from_known_paths(env_vars: &EnvVariables) -> Vec<
151168 }
152169 }
153170 env_paths. append ( & mut env_vars. known_global_search_locations . clone ( ) ) ;
171+ env_paths. sort ( ) ;
172+ env_paths. dedup ( ) ;
173+ let env_paths = env_paths. into_iter ( ) . filter ( |f| f. exists ( ) ) . collect ( ) ;
154174 trace ! ( "Conda environments in known paths {:?}" , env_paths) ;
155175 env_paths
156176}
@@ -217,7 +237,9 @@ pub fn get_conda_envs_from_environment_txt(env_vars: &EnvVariables) -> Vec<PathB
217237 for line in reader. lines ( ) {
218238 let line = norm_case ( & PathBuf :: from ( line. to_string ( ) ) ) ;
219239 trace ! ( "Conda env in environments.txt file {:?}" , line) ;
220- envs. push ( line) ;
240+ if line. exists ( ) {
241+ envs. push ( line) ;
242+ }
221243 }
222244 }
223245 }
@@ -364,7 +386,7 @@ pub fn get_known_conda_install_locations(
364386 }
365387 if let Some ( home) = env_vars. home . clone ( ) {
366388 // https://stackoverflow.com/questions/35709497/anaconda-python-where-are-the-virtual-environments-stored
367- for prefix in [
389+ let mut prefixes = vec ! [
368390 home. clone( ) ,
369391 // https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195
370392 home. join( "opt" ) ,
@@ -375,9 +397,14 @@ pub fn get_known_conda_install_locations(
375397 PathBuf :: from( "/usr/share" ) ,
376398 PathBuf :: from( "/usr/local" ) ,
377399 PathBuf :: from( "/usr" ) ,
378- PathBuf :: from ( "/opt/homebrew" ) ,
379- PathBuf :: from ( "/home/linuxbrew/.linuxbrew" ) ,
380- ] {
400+ ] ;
401+ if std:: env:: consts:: OS == "macos" {
402+ prefixes. push ( PathBuf :: from ( "/opt/homebrew" ) ) ;
403+ } else {
404+ prefixes. push ( PathBuf :: from ( "/home/linuxbrew/.linuxbrew" ) ) ;
405+ }
406+
407+ for prefix in prefixes {
381408 known_paths. push ( prefix. clone ( ) . join ( "anaconda" ) ) ;
382409 known_paths. push ( prefix. clone ( ) . join ( "anaconda3" ) ) ;
383410 known_paths. push ( prefix. clone ( ) . join ( "miniconda" ) ) ;
@@ -395,8 +422,7 @@ pub fn get_known_conda_install_locations(
395422 }
396423 known_paths. sort ( ) ;
397424 known_paths. dedup ( ) ;
398-
399- known_paths
425+ known_paths. into_iter ( ) . filter ( |f| f. exists ( ) ) . collect ( )
400426}
401427
402428pub fn get_conda_dir_from_exe ( conda_executable : & Option < PathBuf > ) -> Option < PathBuf > {
0 commit comments