@@ -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