Skip to content

Commit 290f4ac

Browse files
committed
fix: run_command support Windows
1 parent 08631e2 commit 290f4ac

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ vite_str = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab0
7272
vite_task = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab090fb5c5d85ecc7798a0fe9b6f1f26da" }
7373
vite_workspace = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab090fb5c5d85ecc7798a0fe9b6f1f26da" }
7474
wax = "0.6.0"
75+
which = "8.0.0"
7576

7677
napi = { version = "3.0.0", default-features = false, features = ["async", "error_anyhow"] }
7778
napi-build = "2"

crates/vite_error/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ pub enum Error {
122122
#[error("Unsupported hash algorithm: {0}")]
123123
UnsupportedHashAlgorithm(Str),
124124

125+
#[error("Cannot find binary path for command '{0}'")]
126+
CannotFindBinaryPath(Str),
127+
125128
#[error(transparent)]
126129
Anyhow(#[from] anyhow::Error),
127130
}

crates/vite_install/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ vite_glob = { workspace = true }
3030
vite_path = { workspace = true }
3131
vite_str = { workspace = true }
3232
vite_workspace = { workspace = true }
33+
which = { workspace = true, features = ["tracing"] }
3334

3435
[target.'cfg(target_os = "windows")'.dependencies]
3536
reqwest = { workspace = true, features = ["stream", "native-tls-vendored", "json"] }

crates/vite_install/src/package_manager.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ async fn download_package_manager(
332332
let cache_dir = get_cache_dir()?;
333333
let bin_name = package_manager_type.to_string();
334334
// $CACHE_DIR/vite/package_manager/pnpm/10.0.0
335-
let target_dir = cache_dir.join(format!("package_manager/{bin_name}/{version}"));
335+
let target_dir = cache_dir.join("package_manager").join(&bin_name).join(version);
336336
let install_dir = target_dir.join(&bin_name);
337337

338338
// If all shims are already exists, return the target directory
@@ -559,7 +559,14 @@ pub(crate) async fn run_command(
559559
) -> Result<ExitStatus, Error> {
560560
println!("Running: {} {}", bin_name, args.join(" "));
561561

562-
let mut cmd = Command::new(bin_name);
562+
// Resolve the command path using which crate
563+
// If PATH is provided in envs, use which_in to search in custom paths
564+
// Otherwise, use which to search in system PATH
565+
let paths = envs.get("PATH");
566+
let bin_path = which::which_in(bin_name, paths, cwd.as_ref())
567+
.map_err(|_| Error::CannotFindBinaryPath(bin_name.into()))?;
568+
569+
let mut cmd = Command::new(bin_path);
563570
cmd.args(args)
564571
.envs(envs)
565572
.current_dir(cwd.as_ref())
@@ -1865,4 +1872,33 @@ mod tests {
18651872
assert!(matcher.is_match("src/app.ts"), "Should ignore source files");
18661873
}
18671874
}
1875+
1876+
mod run_command_tests {
1877+
use super::*;
1878+
1879+
#[tokio::test]
1880+
async fn test_run_command_and_find_binary_path() {
1881+
let temp_dir = create_temp_dir();
1882+
let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
1883+
let envs = HashMap::from([("PATH".to_string(), format_path_env(&temp_dir_path))]);
1884+
let result =
1885+
run_command("npm", &["--version".to_string()], &envs, &temp_dir_path).await;
1886+
assert!(result.is_ok(), "Should run command successfully, but got error: {:?}", result);
1887+
}
1888+
1889+
#[tokio::test]
1890+
async fn test_run_command_and_not_find_binary_path() {
1891+
let temp_dir = create_temp_dir();
1892+
let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
1893+
let envs = HashMap::from([("PATH".to_string(), format_path_env(&temp_dir_path))]);
1894+
let result =
1895+
run_command("npm-not-exists", &["--version".to_string()], &envs, &temp_dir_path)
1896+
.await;
1897+
assert!(result.is_err(), "Should not find binary path, but got: {:?}", result);
1898+
assert_eq!(
1899+
result.unwrap_err().to_string(),
1900+
"Cannot find binary path for command 'npm-not-exists'"
1901+
);
1902+
}
1903+
}
18681904
}

0 commit comments

Comments
 (0)