diff --git a/src/pi.rs b/src/pi.rs index 4cbf1af..8148078 100644 --- a/src/pi.rs +++ b/src/pi.rs @@ -201,16 +201,18 @@ fn discover() -> Option { /// The inherited `PATH`, then the user's login shell. Apps started from Finder /// do not inherit PATH entries added by shell rc files (nvm, fnm, volta), so -/// the shell lookup is what usually finds `pi` and `node`. Blocking. +/// the shell lookup can find programs such as `pi` there. Blocking. pub fn find_executable(name: &str) -> Option { - if let Some(path) = std::env::var_os("PATH") - && let Some(found) = std::env::split_paths(&path) - .map(|directory| directory.join(name)) - .find(|candidate| candidate.is_file()) - { - return Some(found); - } + find_on_path(name).or_else(|| find_in_login_shell(name)) +} + +fn find_on_path(name: &str) -> Option { + std::env::split_paths(&std::env::var_os("PATH")?) + .map(|directory| directory.join(name)) + .find(|candidate| candidate.is_file()) +} +fn find_in_login_shell(name: &str) -> Option { // `name` comes from our code or a plugin manifest: only plain names are looked up. if !name .chars() @@ -251,7 +253,9 @@ fn remember_node_directory(executable: &Path) { let directory = if beside_pi { None } else { - find_executable("node").and_then(|node| node.parent().map(Path::to_path_buf)) + find_in_login_shell("node") + .or_else(|| find_on_path("node")) + .and_then(|node| node.parent().map(Path::to_path_buf)) }; if let Ok(mut remembered) = NODE_DIRECTORY.write() { *remembered = directory; @@ -260,7 +264,7 @@ fn remember_node_directory(executable: &Path) { /// pi is usually a `#!/usr/bin/env node` script, so the child needs `node` on /// PATH: the executable's directory comes first (nvm puts `node` there), and -/// a node found elsewhere by the last check goes last. Does not block. +/// the node selected by the login shell comes before the inherited PATH. Does not block. pub fn path_with_executable_dir(executable: &Path) -> OsString { let node_directory = NODE_DIRECTORY .read() @@ -273,8 +277,6 @@ pub fn path_with_executable_dir(executable: &Path) -> OsString { ) } -/// `node_directory` goes after the inherited PATH: it only fills the gap when -/// nothing else provides `node` and never changes which tools win. fn child_path( executable: &Path, inherited: Option<&OsStr>, @@ -284,15 +286,15 @@ fn child_path( .parent() .map(|directory| vec![directory.to_path_buf()]) .unwrap_or_default(); - if let Some(path) = inherited { - directories.extend(std::env::split_paths(path)); - } - if let Some(node_directory) = node_directory - && !directories - .iter() - .any(|directory| directory == node_directory) + if let Some(directory) = node_directory + && Some(directory) != executable.parent() { - directories.push(node_directory.to_path_buf()); + directories.push(directory.to_path_buf()); + } + if let Some(path) = inherited { + directories.extend( + std::env::split_paths(path).filter(|dir| Some(dir.as_path()) != node_directory), + ); } std::env::join_paths(directories).unwrap_or_default() } @@ -487,23 +489,23 @@ mod tests { } #[test] - fn node_directory_goes_after_the_inherited_path() { + fn node_directory_goes_before_the_inherited_path() { let path = child_path( Path::new("/pi/bin/pi"), Some(OsStr::new("/usr/bin:/bin")), Some(Path::new("/mise/node/bin")), ); - assert_eq!(path, OsString::from("/pi/bin:/usr/bin:/bin:/mise/node/bin")); + assert_eq!(path, OsString::from("/pi/bin:/mise/node/bin:/usr/bin:/bin")); } #[test] - fn node_directory_already_on_the_path_keeps_its_place() { + fn node_directory_already_on_the_path_moves_forward_once() { let path = child_path( Path::new("/pi/bin/pi"), Some(OsStr::new("/pyenv/shims:/usr/bin")), Some(Path::new("/usr/bin")), ); - assert_eq!(path, OsString::from("/pi/bin:/pyenv/shims:/usr/bin")); + assert_eq!(path, OsString::from("/pi/bin:/usr/bin:/pyenv/shims")); } #[test]