diff --git a/crates/nu-command/src/filters/upsert.rs b/crates/nu-command/src/filters/upsert.rs index ec852e7e5d..fd193e0df8 100644 --- a/crates/nu-command/src/filters/upsert.rs +++ b/crates/nu-command/src/filters/upsert.rs @@ -169,7 +169,7 @@ fn upsert( if let Some(v) = input.next() { pre_elems.push(v); } else { - return Err(ShellError::AccessBeyondEnd(idx - 1, *span)); + return Err(ShellError::AccessBeyondEnd(idx, *span)); } } diff --git a/crates/nu-command/tests/commands/upsert.rs b/crates/nu-command/tests/commands/upsert.rs index a1a1ae699c..7ee7a8fd97 100644 --- a/crates/nu-command/tests/commands/upsert.rs +++ b/crates/nu-command/tests/commands/upsert.rs @@ -87,3 +87,23 @@ fn uses_optional_index_argument_updating() { assert_eq!(actual.out, "[[a]; [8], [8]]"); } + +#[test] +fn index_does_not_exist() { + let actual = nu!( + cwd: ".", pipeline( + r#"[1,2,3] | upsert 4 4"# + )); + + assert!(actual.err.contains("index too large (max: 3)")); +} + +#[test] +fn upsert_empty() { + let actual = nu!( + cwd: ".", pipeline( + r#"[] | upsert 1 1"# + )); + + assert!(actual.err.contains("index too large (max: 0)")); +}