From ed10aafa6feada12c7d0f4f441393165ccbffef0 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 30 Jun 2020 05:39:11 +1200 Subject: [PATCH] Bubble errors even if pipeline isn't used (#2080) --- crates/nu-cli/src/commands/autoview.rs | 10 ++-- .../src/commands/classified/internal.rs | 56 +++++++++++-------- crates/nu-cli/src/commands/command.rs | 15 ++--- crates/nu-cli/src/commands/enter.rs | 2 +- crates/nu-cli/src/commands/save.rs | 2 +- crates/nu-cli/src/context.rs | 6 +- tests/shell/pipeline/commands/external.rs | 10 ++++ 7 files changed, 64 insertions(+), 37 deletions(-) diff --git a/crates/nu-cli/src/commands/autoview.rs b/crates/nu-cli/src/commands/autoview.rs index 417d75e5e..07255d2d5 100644 --- a/crates/nu-cli/src/commands/autoview.rs +++ b/crates/nu-cli/src/commands/autoview.rs @@ -121,7 +121,7 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; } } @@ -138,7 +138,7 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; } else { out!("{}", s); @@ -161,7 +161,7 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; } else { out!("{}\n", s); @@ -236,7 +236,7 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; } else { use pretty_hex::*; @@ -298,7 +298,7 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; } else { out!("{:?}", item); diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-cli/src/commands/classified/internal.rs index 6a51f86ed..c76d3fd7a 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-cli/src/commands/classified/internal.rs @@ -37,7 +37,7 @@ pub(crate) async fn run_internal_command( &scope, objects, ) - .await + .await? }; let head = Arc::new(command.args.head.clone()); @@ -89,37 +89,49 @@ pub(crate) async fn run_internal_command( scope: (&*scope).clone(), }, }; - let mut result = converter + let result = converter .run( new_args.with_input(vec![tagged_contents]), &context.registry, ) .await; - let result_vec: Vec> = - result.drain_vec().await; - let mut output = vec![]; - for res in result_vec { - match res { - Ok(ReturnSuccess::Value(Value { - value: UntaggedValue::Table(list), - .. - })) => { - for l in list { - output.push(Ok(l)); + match result { + Ok(mut result) => { + let result_vec: Vec> = + result.drain_vec().await; + + let mut output = vec![]; + for res in result_vec { + match res { + Ok(ReturnSuccess::Value(Value { + value: UntaggedValue::Table(list), + .. + })) => { + for l in list { + output.push(Ok(l)); + } + } + Ok(ReturnSuccess::Value(Value { + value, + .. + })) => { + output + .push(Ok(value + .into_value(contents_tag.clone()))); + } + Err(e) => output.push(Err(e)), + _ => {} } } - Ok(ReturnSuccess::Value(Value { value, .. })) => { - output.push(Ok( - value.into_value(contents_tag.clone()) - )); - } - Err(e) => output.push(Err(e)), - _ => {} + + futures::stream::iter(output).to_input_stream() + } + Err(e) => { + context.add_error(e); + InputStream::empty() } } - - futures::stream::iter(output).to_input_stream() } else { InputStream::one(tagged_contents) } diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index b5a138eda..fe38e3eee 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -343,18 +343,19 @@ impl Command { self.0.usage() } - pub async fn run(&self, args: CommandArgs, registry: &CommandRegistry) -> OutputStream { + pub async fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { if args.call_info.switch_present("help") { let cl = self.0.clone(); let registry = registry.clone(); - OutputStream::one(Ok(ReturnSuccess::Value( + Ok(OutputStream::one(Ok(ReturnSuccess::Value( UntaggedValue::string(get_help(&*cl, ®istry)).into_value(Tag::unknown()), - ))) + )))) } else { - match self.0.run(args, registry).await { - Ok(stream) => stream, - Err(err) => OutputStream::one(Err(err)), - } + self.0.run(args, registry).await } } diff --git a/crates/nu-cli/src/commands/enter.rs b/crates/nu-cli/src/commands/enter.rs index 4f6074142..572427eb7 100644 --- a/crates/nu-cli/src/commands/enter.rs +++ b/crates/nu-cli/src/commands/enter.rs @@ -158,7 +158,7 @@ async fn enter( }; let mut result = converter .run(new_args.with_input(vec![tagged_contents]), ®istry) - .await; + .await?; let result_vec: Vec> = result.drain_vec().await; diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index 0b520eef0..b88d9a8d6 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -232,7 +232,7 @@ async fn save( scope, }, }; - let mut result = converter.run(new_args.with_input(input), ®istry).await; + let mut result = converter.run(new_args.with_input(input), ®istry).await?; let result_vec: Vec> = result.drain_vec().await; if converter.is_binary() { diff --git a/crates/nu-cli/src/context.rs b/crates/nu-cli/src/context.rs index 726984758..a643ae32c 100644 --- a/crates/nu-cli/src/context.rs +++ b/crates/nu-cli/src/context.rs @@ -184,6 +184,10 @@ impl Context { self.current_errors.lock().clone() } + pub(crate) fn add_error(&self, err: ShellError) { + self.current_errors.lock().push(err); + } + pub(crate) fn maybe_print_errors(&mut self, source: Text) -> bool { let errors = self.current_errors.clone(); let mut errors = errors.lock(); @@ -232,7 +236,7 @@ impl Context { args: hir::Call, scope: &Scope, input: InputStream, - ) -> OutputStream { + ) -> Result { let command_args = self.command_args(args, input, name_tag, scope); command.run(command_args, self.registry()).await } diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index 4d9b7aedf..14e64f0bb 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -10,6 +10,16 @@ fn shows_error_for_command_not_found() { assert!(actual.err.contains("Command not found")); } +#[test] +fn shows_error_for_command_not_found_in_pipeline() { + let actual = nu!( + cwd: ".", + "ferris_is_not_here.exe | echo done" + ); + + assert!(actual.err.contains("Command not found")); +} + #[test] fn automatically_change_directory() { use nu_test_support::playground::Playground;