Skip to content

Commit 9ce4b34

Browse files
committed
refactor(env): eliminate redundant find_system_tool call in doctor
Thread the system node path from check_shim_mode() into check_current_resolution() instead of scanning PATH twice. Also add debug_assert in JsRuntime::from_system() to catch empty binary filenames from malformed paths.
1 parent 3673f94 commit 9ce4b34

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

crates/vite_global_cli/src/commands/env/doctor.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
114114

115115
// Section: Configuration
116116
print_section("Configuration");
117-
let shim_mode = check_shim_mode().await;
117+
let (shim_mode, system_node_path) = check_shim_mode().await;
118118

119119
// Check env sourcing: IDE-relevant profiles first, then all shell profiles
120120
#[cfg(not(windows))]
@@ -128,7 +128,7 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
128128

129129
// Section: Version Resolution
130130
print_section("Version Resolution");
131-
check_current_resolution(&cwd, shim_mode).await;
131+
check_current_resolution(&cwd, shim_mode, system_node_path).await;
132132

133133
// Section: Conflicts (conditional)
134134
check_conflicts();
@@ -247,8 +247,8 @@ fn shim_filename(tool: &str) -> String {
247247
}
248248
}
249249

250-
/// Check and display shim mode. Returns the current mode for use by other checks.
251-
async fn check_shim_mode() -> ShimMode {
250+
/// Check and display shim mode. Returns the mode and any found system node path.
251+
async fn check_shim_mode() -> (ShimMode, Option<AbsolutePathBuf>) {
252252
let config = match load_config().await {
253253
Ok(c) => c,
254254
Err(e) => {
@@ -257,10 +257,12 @@ async fn check_shim_mode() -> ShimMode {
257257
"Node.js mode",
258258
&format!("config error: {e}").yellow().to_string(),
259259
);
260-
return ShimMode::default();
260+
return (ShimMode::default(), None);
261261
}
262262
};
263263

264+
let mut system_node_path = None;
265+
264266
match config.shim_mode {
265267
ShimMode::Managed => {
266268
print_check(&output::CHECK.green().to_string(), "Node.js mode", "managed");
@@ -275,6 +277,7 @@ async fn check_shim_mode() -> ShimMode {
275277
// Check if system Node.js is available
276278
if let Some(system_node) = shim::find_system_tool("node") {
277279
print_check(" ", "System Node.js", &system_node.as_path().display().to_string());
280+
system_node_path = Some(system_node);
278281
} else {
279282
print_check(
280283
&output::WARN_SIGN.yellow().to_string(),
@@ -285,7 +288,7 @@ async fn check_shim_mode() -> ShimMode {
285288
}
286289
}
287290

288-
config.shim_mode
291+
(config.shim_mode, system_node_path)
289292
}
290293

291294
/// Check profile files for env sourcing and classify where it was found.
@@ -585,12 +588,16 @@ fn print_ide_setup_guidance(bin_dir: &vite_path::AbsolutePath) {
585588
}
586589

587590
/// Check current directory version resolution.
588-
async fn check_current_resolution(cwd: &AbsolutePathBuf, shim_mode: ShimMode) {
591+
async fn check_current_resolution(
592+
cwd: &AbsolutePathBuf,
593+
shim_mode: ShimMode,
594+
system_node_path: Option<AbsolutePathBuf>,
595+
) {
589596
print_check(" ", "Directory", &cwd.as_path().display().to_string());
590597

591-
// In system-first mode, show system Node.js version instead of managed resolution
598+
// In system-first mode, show system Node.js info instead of managed resolution
592599
if shim_mode == ShimMode::SystemFirst {
593-
if let Some(system_node) = shim::find_system_tool("node") {
600+
if let Some(system_node) = system_node_path {
594601
let version = get_node_version(&system_node).await;
595602
print_check(" ", "Source", "system PATH");
596603
print_check(" ", "Version", &version.bright_green().to_string());

crates/vite_js_runtime/src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl JsRuntime {
7979
let binary_filename: Str = Str::from(
8080
binary_path.as_path().file_name().unwrap_or_default().to_string_lossy().as_ref(),
8181
);
82+
debug_assert!(!binary_filename.is_empty(), "binary_path has no filename: {binary_path:?}");
8283
Self {
8384
runtime_type,
8485
version: "system".into(),

0 commit comments

Comments
 (0)