@@ -115,7 +115,12 @@ pub(crate) fn managed_npm_bin_for_global_command(
115115 global : bool ,
116116 node_bin_prefix : & AbsolutePath ,
117117) -> Option < AbsolutePathBuf > {
118- global. then ( || npm_bin_path_from_node_bin_prefix ( node_bin_prefix) )
118+ if !global {
119+ return None ;
120+ }
121+
122+ let npm_bin = npm_bin_path_from_node_bin_prefix ( node_bin_prefix) ;
123+ npm_bin. as_path ( ) . exists ( ) . then_some ( npm_bin)
119124}
120125
121126/// Build a PackageManager, converting PackageJsonNotFound into a friendly error message.
@@ -213,18 +218,24 @@ mod tests {
213218 use super :: * ;
214219
215220 #[ test]
216- fn test_managed_npm_bin_for_global_command_only_when_global ( ) {
217- let node_bin_prefix = if cfg ! ( windows) {
218- AbsolutePathBuf :: new ( "C:\\ node" . into ( ) ) . unwrap ( )
219- } else {
220- AbsolutePathBuf :: new ( "/node/bin" . into ( ) ) . unwrap ( )
221- } ;
222- let npm_bin = managed_npm_bin_for_global_command ( true , & node_bin_prefix) . unwrap ( ) ;
221+ fn test_managed_npm_bin_for_global_command_uses_existing_adjacent_npm ( ) {
222+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
223+ let node_bin_prefix = AbsolutePathBuf :: new ( temp_dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
223224 let expected = npm_bin_path_from_node_bin_prefix ( & node_bin_prefix) ;
225+ std:: fs:: write ( & expected, "" ) . unwrap ( ) ;
226+
227+ let npm_bin = managed_npm_bin_for_global_command ( true , & node_bin_prefix) . unwrap ( ) ;
224228 assert_eq ! ( npm_bin, expected) ;
225229 assert ! ( managed_npm_bin_for_global_command( false , & node_bin_prefix) . is_none( ) ) ;
226230 }
227231
232+ #[ test]
233+ fn test_managed_npm_bin_for_global_command_falls_back_when_adjacent_npm_is_missing ( ) {
234+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
235+ let node_bin_prefix = AbsolutePathBuf :: new ( temp_dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
236+ assert ! ( managed_npm_bin_for_global_command( true , & node_bin_prefix) . is_none( ) ) ;
237+ }
238+
228239 #[ test]
229240 fn test_has_vite_plus_in_dev_dependencies ( ) {
230241 let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
0 commit comments