diff --git a/crates/nu-command/src/filters/first.rs b/crates/nu-command/src/filters/first.rs index 2f4ac233e..34587abfc 100644 --- a/crates/nu-command/src/filters/first.rs +++ b/crates/nu-command/src/filters/first.rs @@ -97,6 +97,7 @@ fn first_helper( // the first N elements is covered by `take` let return_single_element = rows.is_none(); let rows_desired: usize = match rows { + Some(i) if i < 0 => return Err(ShellError::NeedsPositiveValue(head)), Some(x) => x as usize, None => 1, }; diff --git a/crates/nu-command/tests/commands/first.rs b/crates/nu-command/tests/commands/first.rs index 620c206f0..4c3e1bc05 100644 --- a/crates/nu-command/tests/commands/first.rs +++ b/crates/nu-command/tests/commands/first.rs @@ -92,3 +92,16 @@ fn works_with_binary_list() { assert_eq!(actual.out, "true"); } + +#[test] +fn errors_on_negative_rows() { + let actual = nu!( + cwd: ".", pipeline( + r#" + [1, 2, 3] + | first -10 + "# + )); + + assert!(actual.err.contains("use a positive value")); +}