Skip to content

Commit 0106c8d

Browse files
committed
fix(install): validate adjacent npm before use
1 parent 3e96036 commit 0106c8d

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

  • crates/vite_global_cli/src/commands

crates/vite_global_cli/src/commands/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ pub(crate) fn managed_npm_bin_for_global_command(
120120
}
121121

122122
let npm_bin = npm_bin_path_from_node_bin_prefix(node_bin_prefix);
123-
npm_bin.as_path().exists().then_some(npm_bin)
123+
vite_command::resolve_bin(
124+
npm_bin.as_path().as_os_str().to_string_lossy().as_ref(),
125+
None,
126+
node_bin_prefix,
127+
)
128+
.ok()
124129
}
125130

126131
/// Build a PackageManager, converting PackageJsonNotFound into a friendly error message.
@@ -218,17 +223,35 @@ mod tests {
218223
use super::*;
219224

220225
#[test]
221-
fn test_managed_npm_bin_for_global_command_uses_existing_adjacent_npm() {
226+
fn test_managed_npm_bin_for_global_command_uses_executable_adjacent_npm() {
222227
let temp_dir = tempfile::tempdir().unwrap();
223228
let node_bin_prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
224229
let expected = npm_bin_path_from_node_bin_prefix(&node_bin_prefix);
225230
std::fs::write(&expected, "").unwrap();
231+
#[cfg(unix)]
232+
{
233+
use std::os::unix::fs::PermissionsExt;
234+
let mut permissions = std::fs::metadata(&expected).unwrap().permissions();
235+
permissions.set_mode(0o755);
236+
std::fs::set_permissions(&expected, permissions).unwrap();
237+
}
226238

227239
let npm_bin = managed_npm_bin_for_global_command(true, &node_bin_prefix).unwrap();
228240
assert_eq!(npm_bin, expected);
229241
assert!(managed_npm_bin_for_global_command(false, &node_bin_prefix).is_none());
230242
}
231243

244+
#[cfg(unix)]
245+
#[test]
246+
fn test_managed_npm_bin_for_global_command_falls_back_when_adjacent_npm_is_not_executable() {
247+
let temp_dir = tempfile::tempdir().unwrap();
248+
let node_bin_prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
249+
let npm_bin = npm_bin_path_from_node_bin_prefix(&node_bin_prefix);
250+
std::fs::write(&npm_bin, "").unwrap();
251+
252+
assert!(managed_npm_bin_for_global_command(true, &node_bin_prefix).is_none());
253+
}
254+
232255
#[test]
233256
fn test_managed_npm_bin_for_global_command_falls_back_when_adjacent_npm_is_missing() {
234257
let temp_dir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)