Skip to content
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ vt_str = { git = "https://github.com/voidzero-dev/vite-task.git", rev = "d05b1dc
vt_workspace = { git = "https://github.com/voidzero-dev/vite-task.git", rev = "d05b1dcdbaabaa69643ee0b89cebe3cd390957e9" }
walkdir = "2.5.0"
webpki-root-certs = "1.0.9"
which = "8.0.0"
which = "8.0.6"
winreg = "0.56.0"
xxhash-rust = "0.8.15"
zip = { version = "7.2", default-features = false, features = ["deflate-flate2-zlib-rs"] }
Expand Down
79 changes: 2 additions & 77 deletions crates/vp_command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::os::fd::{BorrowedFd, RawFd};
use std::{
collections::HashMap,
ffi::{OsStr, OsString},
path::Path,
process::{ExitStatus, Stdio},
};

Expand All @@ -22,50 +21,6 @@ use vt_path::{AbsolutePath, AbsolutePathBuf, RelativePathBuf};

mod ps1_shim;

/// Return whether a PATH entry is an ordinary relative path that should be resolved against the
/// command cwd. This includes `tools`, `./tools`, and `../tools`.
///
/// Windows drive-relative (`C:tools`) and root-relative (`\tools`) paths have distinct native
/// semantics. They are intentionally left unchanged, as are absolute drive and UNC paths.
fn is_plain_relative_path(path: &Path) -> bool {
#[cfg(windows)]
{
!path.has_root()
&& path.components().next().is_some_and(|component| {
matches!(
component,
std::path::Component::CurDir
| std::path::Component::ParentDir
| std::path::Component::Normal(_)
)
})
}

#[cfg(not(windows))]
{
!path.is_absolute()
}
}

fn resolve_bin_from_path_entry(
bin_name: &str,
path_entry: &Path,
cwd: &AbsolutePath,
) -> Option<std::path::PathBuf> {
if path_entry.starts_with("~") || !is_plain_relative_path(path_entry) {
// Preserve tilde expansion and Windows-special path semantics by passing the original
// entry through `which`. Since this entry came from `split_paths`, serializing it alone
// cannot introduce the command cwd's PATH separator.
let path_env = std::env::join_paths([path_entry]).ok()?;
which::which_in(bin_name, Some(path_env), cwd).ok()
} else {
// Search the absolute candidate directly. Re-serializing `cwd.join(path_entry)` into PATH
// would fail on Unix when cwd contains `:`, even though the relative PATH entry is valid.
let candidate = cwd.as_path().join(path_entry).join(bin_name);
which::which_in(candidate, None::<&OsStr>, cwd).ok()
}
}

/// Result of running a command with fspy tracking.
#[derive(Debug)]
pub struct FspyCommandResult {
Expand All @@ -84,29 +39,15 @@ pub fn resolve_bin(
path_env: Option<&OsStr>,
cwd: impl AsRef<AbsolutePath>,
) -> Result<AbsolutePathBuf, Error> {
let cwd = cwd.as_ref();
let current_path;
let path_env = if let Some(p) = path_env {
p
} else {
current_path = std::env::var_os("PATH").unwrap_or_default();
&current_path
};
let bin_path = Path::new(bin_name);
let path = if bin_path.is_absolute() || bin_path.components().count() > 1 {
// Preserve `which` semantics for an explicit program path: it is resolved directly against
// `cwd` and does not search PATH.
which::which_in(bin_name, Some(path_env), cwd)
.map_err(|_| Error::CannotFindBinaryPath(bin_name.into()))?
} else {
// `which` resolves relative PATH entries against the process cwd instead of the supplied
// command cwd. Search each entry in order so ordinary relative entries can be resolved
// against `cwd` without serializing that absolute path back into PATH.
std::env::split_paths(path_env)
.find_map(|entry| resolve_bin_from_path_entry(bin_name, &entry, cwd))
.ok_or_else(|| Error::CannotFindBinaryPath(bin_name.into()))?
};
let path = if is_plain_relative_path(&path) { cwd.as_path().join(path) } else { path };
let path = which::which_in(bin_name, Some(path_env), cwd.as_ref())
.map_err(|_| Error::CannotFindBinaryPath(bin_name.into()))?;
AbsolutePathBuf::new(path).ok_or_else(|| Error::CannotFindBinaryPath(bin_name.into()))
}

Expand Down Expand Up @@ -589,22 +530,6 @@ mod tests {
assert_eq!(resolved.into_path_buf(), bin_path);
}

#[cfg(windows)]
#[test]
fn test_windows_absolute_entries_are_not_plain_relative_paths() {
for entry in [r"C:\tools\bin", r"\\server\share\bin"] {
assert!(!is_plain_relative_path(Path::new(entry)));
}
}

#[cfg(windows)]
#[test]
fn test_windows_special_relative_entries_are_not_plain_relative_paths() {
for entry in [r"C:tools\bin", r"\tools\bin"] {
assert!(!is_plain_relative_path(Path::new(entry)));
}
}

mod run_command_tests {

use super::*;
Expand Down
Loading