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
17 changes: 9 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ vite_str = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab0
vite_task = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab090fb5c5d85ecc7798a0fe9b6f1f26da" }
vite_workspace = { git = "https://github.com/voidzero-dev/vite-task", rev = "d66556ab090fb5c5d85ecc7798a0fe9b6f1f26da" }
wax = "0.6.0"
which = "8.0.0"

napi = { version = "3.0.0", default-features = false, features = ["async", "error_anyhow"] }
napi-build = "2"
Expand Down
3 changes: 3 additions & 0 deletions crates/vite_error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ pub enum Error {
#[error("Unsupported hash algorithm: {0}")]
UnsupportedHashAlgorithm(Str),

#[error("Cannot find binary path for command '{0}'")]
CannotFindBinaryPath(Str),

#[error(transparent)]
Anyhow(#[from] anyhow::Error),
}
1 change: 1 addition & 0 deletions crates/vite_install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ vite_glob = { workspace = true }
vite_path = { workspace = true }
vite_str = { workspace = true }
vite_workspace = { workspace = true }
which = { workspace = true, features = ["tracing"] }

[target.'cfg(target_os = "windows")'.dependencies]
reqwest = { workspace = true, features = ["stream", "native-tls-vendored", "json"] }
Expand Down
40 changes: 38 additions & 2 deletions crates/vite_install/src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ async fn download_package_manager(
let cache_dir = get_cache_dir()?;
let bin_name = package_manager_type.to_string();
// $CACHE_DIR/vite/package_manager/pnpm/10.0.0
let target_dir = cache_dir.join(format!("package_manager/{bin_name}/{version}"));
let target_dir = cache_dir.join("package_manager").join(&bin_name).join(version);
let install_dir = target_dir.join(&bin_name);

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

let mut cmd = Command::new(bin_name);
// Resolve the command path using which crate
// If PATH is provided in envs, use which_in to search in custom paths
// Otherwise, use which to search in system PATH
let paths = envs.get("PATH");
let bin_path = which::which_in(bin_name, paths, cwd.as_ref())
.map_err(|_| Error::CannotFindBinaryPath(bin_name.into()))?;

let mut cmd = Command::new(bin_path);
cmd.args(args)
.envs(envs)
.current_dir(cwd.as_ref())
Expand Down Expand Up @@ -1865,4 +1872,33 @@ mod tests {
assert!(matcher.is_match("src/app.ts"), "Should ignore source files");
}
}

mod run_command_tests {
use super::*;

#[tokio::test]
async fn test_run_command_and_find_binary_path() {
let temp_dir = create_temp_dir();
let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let envs = HashMap::from([("PATH".to_string(), format_path_env(&temp_dir_path))]);
let result =
run_command("npm", &["--version".to_string()], &envs, &temp_dir_path).await;
assert!(result.is_ok(), "Should run command successfully, but got error: {:?}", result);
}

#[tokio::test]
async fn test_run_command_and_not_find_binary_path() {
let temp_dir = create_temp_dir();
let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let envs = HashMap::from([("PATH".to_string(), format_path_env(&temp_dir_path))]);
let result =
run_command("npm-not-exists", &["--version".to_string()], &envs, &temp_dir_path)
.await;
assert!(result.is_err(), "Should not find binary path, but got: {:?}", result);
assert_eq!(
result.unwrap_err().to_string(),
"Cannot find binary path for command 'npm-not-exists'"
);
}
}
}
Loading