Skip to content
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

fix: Fix command PATH lookups. #638

Merged
merged 3 commits into from
Oct 22, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- [Rust](https://github.com/moonrepo/tools/blob/master/tools/rust/CHANGELOG.md)
- [TOML schema](https://github.com/moonrepo/tools/blob/master/tools/internal-schema/CHANGELOG.md)

## Unreleased

#### 🐞 Fixes

- Fixed an issue where command lookup within `PATH` may return an invalid result.

## 0.41.6

#### 🚀 Updates
Expand Down
24 changes: 14 additions & 10 deletions crates/system-env/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
Expand All @@ -7,6 +6,8 @@ use std::process::Command;
/// by checking `PATH` and cycling through `PATHEXT` extensions.
#[cfg(windows)]
pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {
use std::env;

let Ok(system_path) = env::var("PATH") else {
return None;
};
Expand All @@ -23,7 +24,7 @@ pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {
if has_ext {
let path = path_dir.join(name);

if path.exists() {
if path.exists() && path.is_file() {
return Some(path);
}
} else {
Expand All @@ -33,7 +34,7 @@ pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {

let path = path_dir.join(file_name);

if path.exists() {
if path.exists() && path.is_file() {
return Some(path);
}
}
Expand All @@ -44,8 +45,10 @@ pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {
}

/// Return an absolute path to the provided command by checking `PATH`.
#[cfg(not(windows))]
#[cfg(unix)]
pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {
use std::env;

let Ok(system_path) = env::var("PATH") else {
return None;
};
Expand All @@ -55,14 +58,19 @@ pub fn find_command_on_path<T: AsRef<OsStr>>(name: T) -> Option<PathBuf> {
for path_dir in env::split_paths(&system_path) {
let path = path_dir.join(name);

if path.exists() {
if path.exists() && path.is_file() {
return Some(path);
}
}

None
}

#[cfg(target_arch = "wasm32")]
pub fn find_command_on_path<T: AsRef<OsStr>>(_name: T) -> Option<PathBuf> {
None
}

/// Return true if the provided command/program (without extension)
/// is available on `PATH`.
pub fn is_command_on_path<T: AsRef<OsStr>>(name: T) -> bool {
Expand Down Expand Up @@ -91,12 +99,8 @@ pub fn create_process_command<T: AsRef<OsStr>, I: IntoIterator<Item = A>, A: AsR
find_command_on_path(bin).unwrap_or_else(|| bin.into())
};

let bin_ext = bin_path
.extension()
.map(|ext| ext.to_string_lossy().to_lowercase());

// If a Windows script, we must execute the command through powershell
match bin_ext.as_deref() {
match bin_path.extension().and_then(|ext| ext.to_str()) {
Some("ps1" | "cmd" | "bat") => {
// This conversion is unfortunate...
let args = args
Expand Down
4 changes: 2 additions & 2 deletions plugins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.