From 237a68560539358a8f41461f87ad9deb07aa2a4e Mon Sep 17 00:00:00 2001 From: Nils Feierabend Date: Fri, 4 Apr 2025 13:35:36 +0200 Subject: [PATCH] Consider PATH when running command is nuscript in windows (#15486) Fixes #15476 # Description Consider PATH when checking for potential_nuscript_in_windows to allow executing scripts which are in PATH without having to full path address them. It previously only checked the current working directory so only relative paths to cwd and full path worked. The current implementation runs this then through cmd.exe /D /C which can run it with assoc and ftype set for nushell scripts. We could instead run it through nu as `std::env::current_exe()` avoiding the cmd call and the need for assoc and ftype (see: https://github.com/mztikk/nushell/commit/8b25173f02eb5043d796a27f29e19abad69cd6e2). But ive left the current implementation for this intact to not change implementation details, avoid a bigger change and leave this open for discussion here since im not sure if this has any major implications. # User-Facing Changes This would now run every external command through PATH an additional time on windows, so potentially twice. I dont think this has any bigger effect. # Tests + Formatting # After Submitting --- crates/nu-command/src/system/run_external.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 38b9fb165e..03e2d3de02 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -78,6 +78,8 @@ impl Command for External { _ => Path::new(&*name_str).to_owned(), }; + let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; + // On Windows, the user could have run the cmd.exe built-in "assoc" command // Example: "assoc .nu=nuscript" and then run the cmd.exe built-in "ftype" command // Example: "ftype nuscript=C:\path\to\nu.exe '%1' %*" and then added the nushell @@ -88,7 +90,7 @@ impl Command for External { // easy way to do this is to run cmd.exe with the script as an argument. let potential_nuscript_in_windows = if cfg!(windows) { // let's make sure it's a .nu script - if let Some(executable) = which(&expanded_name, "", cwd.as_ref()) { + if let Some(executable) = which(&expanded_name, &paths, cwd.as_ref()) { let ext = executable .extension() .unwrap_or_default() @@ -133,7 +135,6 @@ impl Command for External { } else { // Determine the PATH to be used and then use `which` to find it - though this has no // effect if it's an absolute path already - let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; let Some(executable) = which(&expanded_name, &paths, cwd.as_ref()) else { return Err(command_not_found( &name_str,