Skip to content

Ensure we search in global paths on Windows #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions crates/pet-conda/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ pub fn get_conda_environment_paths(
let mut env_paths = thread::scope(|s| {
let mut envs = vec![];
for thread in [
s.spawn(|| {
get_conda_envs_from_environment_txt(env_vars)
.iter()
.map(PathBuf::from)
.collect::<Vec<PathBuf>>()
}),
s.spawn(|| get_conda_envs_from_environment_txt(env_vars)),
s.spawn(|| get_conda_environment_paths_from_conda_rc(env_vars)),
s.spawn(|| get_conda_environment_paths_from_known_paths(env_vars)),
s.spawn(|| get_conda_environment_paths_from_additional_paths(additional_env_dirs)),
Expand Down
121 changes: 76 additions & 45 deletions crates/pet-core/src/os_environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::{env, path::PathBuf};
use std::{
env,
path::PathBuf,
sync::{Arc, Mutex},
};

use pet_fs::path::norm_case;

Expand All @@ -14,10 +18,14 @@ pub trait Environment {
fn get_know_global_search_locations(&self) -> Vec<PathBuf>;
}

pub struct EnvironmentApi {}
pub struct EnvironmentApi {
global_search_locations: Arc<Mutex<Vec<PathBuf>>>,
}
impl EnvironmentApi {
pub fn new() -> Self {
EnvironmentApi {}
EnvironmentApi {
global_search_locations: Arc::new(Mutex::new(vec![])),
}
}
}
impl Default for EnvironmentApi {
Expand All @@ -38,7 +46,19 @@ impl Environment for EnvironmentApi {
get_env_var(key)
}
fn get_know_global_search_locations(&self) -> Vec<PathBuf> {
vec![]
if self.global_search_locations.lock().unwrap().is_empty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was missing

let mut paths =
env::split_paths(&self.get_env_var("PATH".to_string()).unwrap_or_default())
.into_iter()
.filter(|p| p.exists())
.collect::<Vec<PathBuf>>();

self.global_search_locations
.lock()
.unwrap()
.append(&mut paths);
}
self.global_search_locations.lock().unwrap().clone()
}
}

Expand All @@ -54,51 +74,62 @@ impl Environment for EnvironmentApi {
get_env_var(key)
}
fn get_know_global_search_locations(&self) -> Vec<PathBuf> {
let mut paths = env::split_paths(&self.get_env_var("PATH".to_string()).unwrap_or_default())
.collect::<Vec<PathBuf>>();
if self.global_search_locations.lock().unwrap().is_empty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached this instead of computing this everytime

let mut paths =
env::split_paths(&self.get_env_var("PATH".to_string()).unwrap_or_default())
.collect::<Vec<PathBuf>>();

vec![
PathBuf::from("/bin"),
PathBuf::from("/etc"),
PathBuf::from("/lib"),
PathBuf::from("/lib/x86_64-linux-gnu"),
PathBuf::from("/lib64"),
PathBuf::from("/sbin"),
PathBuf::from("/snap/bin"),
PathBuf::from("/usr/bin"),
PathBuf::from("/usr/games"),
PathBuf::from("/usr/include"),
PathBuf::from("/usr/lib"),
PathBuf::from("/usr/lib/x86_64-linux-gnu"),
PathBuf::from("/usr/lib64"),
PathBuf::from("/usr/libexec"),
PathBuf::from("/usr/local"),
PathBuf::from("/usr/local/bin"),
PathBuf::from("/usr/local/etc"),
PathBuf::from("/usr/local/games"),
PathBuf::from("/usr/local/lib"),
PathBuf::from("/usr/local/sbin"),
PathBuf::from("/usr/sbin"),
PathBuf::from("/usr/share"),
PathBuf::from("/home/bin"),
PathBuf::from("/home/sbin"),
PathBuf::from("/opt"),
PathBuf::from("/opt/bin"),
PathBuf::from("/opt/sbin"),
]
.iter()
.for_each(|p| {
if !paths.contains(p) {
paths.push(p.clone());
vec![
PathBuf::from("/bin"),
PathBuf::from("/etc"),
PathBuf::from("/lib"),
PathBuf::from("/lib/x86_64-linux-gnu"),
PathBuf::from("/lib64"),
PathBuf::from("/sbin"),
PathBuf::from("/snap/bin"),
PathBuf::from("/usr/bin"),
PathBuf::from("/usr/games"),
PathBuf::from("/usr/include"),
PathBuf::from("/usr/lib"),
PathBuf::from("/usr/lib/x86_64-linux-gnu"),
PathBuf::from("/usr/lib64"),
PathBuf::from("/usr/libexec"),
PathBuf::from("/usr/local"),
PathBuf::from("/usr/local/bin"),
PathBuf::from("/usr/local/etc"),
PathBuf::from("/usr/local/games"),
PathBuf::from("/usr/local/lib"),
PathBuf::from("/usr/local/sbin"),
PathBuf::from("/usr/sbin"),
PathBuf::from("/usr/share"),
PathBuf::from("/home/bin"),
PathBuf::from("/home/sbin"),
PathBuf::from("/opt"),
PathBuf::from("/opt/bin"),
PathBuf::from("/opt/sbin"),
]
.iter()
.for_each(|p| {
if !paths.contains(p) {
paths.push(p.clone());
}
});

if let Some(home) = self.get_user_home() {
paths.push(home.join(".local").join("bin"));
}
});

if let Some(home) = self.get_user_home() {
// PathBuf::from("~/.local/bin"),
paths.push(home.join(".local").join("bin"));
}
let mut paths = paths
.into_iter()
.filter(|p| p.exists())
.collect::<Vec<PathBuf>>();

paths
self.global_search_locations
.lock()
.unwrap()
.append(&mut paths);
}
self.global_search_locations.lock().unwrap().clone()
}
}

Expand Down
Loading