From 3b6c4c1bb5e8d2b10ab26a4bb5809dd7c93c4a2d Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Fri, 12 Aug 2022 21:27:50 -0700 Subject: [PATCH] run_external: only suggest alternative commands when file not found (#6311) --- crates/nu-command/src/system/run_external.rs | 61 +++++++++++--------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 5c37848094..2cdbb6f189 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -205,34 +205,43 @@ impl ExternalCommand { match child { Err(err) => { - // recommend a replacement if the user tried a deprecated command - let command_name_lower = self.name.item.to_lowercase(); - let deprecated = crate::deprecated_commands(); - if deprecated.contains_key(&command_name_lower) { - let replacement = match deprecated.get(&command_name_lower) { - Some(s) => s.clone(), - None => "".to_string(), - }; - return Err(ShellError::DeprecatedCommand( - command_name_lower, - replacement, + match err.kind() { + // If file not found, try suggesting alternative commands to the user + std::io::ErrorKind::NotFound => { + // recommend a replacement if the user tried a deprecated command + let command_name_lower = self.name.item.to_lowercase(); + let deprecated = crate::deprecated_commands(); + if deprecated.contains_key(&command_name_lower) { + let replacement = match deprecated.get(&command_name_lower) { + Some(s) => s.clone(), + None => "".to_string(), + }; + return Err(ShellError::DeprecatedCommand( + command_name_lower, + replacement, + self.name.span, + )); + } + + let suggestion = suggest_command(&self.name.item, engine_state); + let label = match suggestion { + Some(s) => format!("did you mean '{s}'?"), + None => "can't run executable".into(), + }; + + Err(ShellError::ExternalCommand( + label, + err.to_string(), + self.name.span, + )) + } + // otherwise, a default error message + _ => Err(ShellError::ExternalCommand( + "can't run executable".into(), + err.to_string(), self.name.span, - )); + )), } - - // If we try to run an external but can't, there's a good chance - // that the user entered the wrong command name - let suggestion = suggest_command(&self.name.item, engine_state); - let label = match suggestion { - Some(s) => format!("did you mean '{s}'?"), - None => "can't run executable".into(), - }; - - Err(ShellError::ExternalCommand( - label, - err.to_string(), - self.name.span, - )) } Ok(mut child) => { if !input.is_nothing() {