diff --git a/crates/nu-cli/src/commands/do_.rs b/crates/nu-cli/src/commands/do_.rs index a618c630e5..615ce95f17 100644 --- a/crates/nu-cli/src/commands/do_.rs +++ b/crates/nu-cli/src/commands/do_.rs @@ -61,16 +61,18 @@ async fn do_( registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); + let is_last = raw_args.call_info.args.is_last; let mut context = Context::from_raw(&raw_args, ®istry); let scope = raw_args.call_info.scope.clone(); let ( DoArgs { ignore_errors, - block, + mut block, }, input, ) = raw_args.process(®istry).await?; + block.set_is_last(!is_last); let result = run_block( &block, diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index 8a23c58dd5..3d0696ee62 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -40,7 +40,9 @@ impl WholeStreamCommand for AliasCommand { ) -> Result { let call_info = args.call_info.clone(); let registry = registry.clone(); - let block = self.block.clone(); + let mut block = self.block.clone(); + block.set_is_last(!call_info.args.is_last); + let alias_command = self.clone(); let mut context = Context::from_args(&args, ®istry); let input = args.input; diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-cli/src/evaluate/evaluator.rs index b8f84d9e01..3ae34a6fcf 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-cli/src/evaluate/evaluator.rs @@ -196,6 +196,9 @@ async fn evaluate_invocation( let input = InputStream::empty(); + let mut block = block.clone(); + block.set_is_last(true); + let result = run_block(&block, &mut context, input, it, vars, env).await?; let output = result.into_vec().await; diff --git a/crates/nu-protocol/src/hir.rs b/crates/nu-protocol/src/hir.rs index 404c7b018a..dac2b03642 100644 --- a/crates/nu-protocol/src/hir.rs +++ b/crates/nu-protocol/src/hir.rs @@ -200,6 +200,28 @@ impl Block { commands.expand_it_usage(); } } + + pub fn set_is_last(&mut self, is_last: bool) { + if let Some(pipeline) = self.block.last_mut() { + if let Some(command) = pipeline.list.last_mut() { + if let ClassifiedCommand::Internal(internal) = command { + internal.args.is_last = is_last; + } + } + } + } + + pub fn get_is_last(&mut self) -> Option { + if let Some(pipeline) = self.block.last_mut() { + if let Some(command) = pipeline.list.last_mut() { + if let ClassifiedCommand::Internal(internal) = command { + return Some(internal.args.is_last); + } + } + } + + None + } } #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)] diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 69e58caeb9..8e18c2c174 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -84,13 +84,25 @@ fn it_expansion_of_invocation() { assert_eq!(actual.out, "4"); } +#[test] +fn invocation_properly_redirects() { + let actual = nu!( + cwd: ".", + r#" + echo $(nu --testbin cococo "hello") | str collect + "# + ); + + assert_eq!(actual.out, "hello"); +} + #[test] fn argument_invocation() { let actual = nu!( cwd: ".", r#" - echo "foo" | echo $(echo $it) - "# + echo "foo" | echo $(echo $it) + "# ); assert_eq!(actual.out, "foo");