From 52d69bb02103fe41434ed3a441233ad7c95d3a31 Mon Sep 17 00:00:00 2001 From: Leonhard Kipp Date: Mon, 12 Apr 2021 04:37:36 +0200 Subject: [PATCH] Fix #3213 Rest arg is not optional (#3303) --- crates/nu-engine/src/whole_stream_command.rs | 7 +++++++ tests/shell/pipeline/commands/internal.rs | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/crates/nu-engine/src/whole_stream_command.rs b/crates/nu-engine/src/whole_stream_command.rs index 20f1430aac..b37a8b65cf 100644 --- a/crates/nu-engine/src/whole_stream_command.rs +++ b/crates/nu-engine/src/whole_stream_command.rs @@ -137,6 +137,13 @@ impl WholeStreamCommand for Arc { UntaggedValue::Table(elements).into_value(Span::new(start, end)), ); } + } else if block.params.rest_positional.is_some() { + //If there is a rest arg, but no args were provided, + //we have to set $rest to an empty table + ctx.scope.add_var( + "$rest", + UntaggedValue::Table(Vec::new()).into_value(Span::new(0, 0)), + ); } if let Some(args) = evaluated.args.named { for named in &block.params.named { diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 61f8b60189..936b734f67 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -278,6 +278,19 @@ fn run_custom_command_with_rest_and_flag() { assert_eq!(actual.out, r#"["world","hello","yay"]"#); } +#[test] +fn run_custom_command_with_empty_rest() { + let actual = nu!( + cwd: ".", + r#" + def rest-me-with-empty-rest [...rest: string] { echo $rest }; rest-me-with-empty-rest + "# + ); + + assert_eq!(actual.out, r#""#); + assert_eq!(actual.err, r#""#); +} + #[test] fn set_variable() { let actual = nu!(