Skip to content

Errors in each loops not handled well #8615

@aidalgol

Description

@aidalgol

Describe the bug

An error in an each loop does not stop the loop, and when used in a nu script, does not stop the program.

How to reproduce

[1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
1
2
3
Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #269:1:1]
 1 │ [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·  ┬
   ·  ╰── source value
   ╰────

Error:
  × stop!
   ╭─[entry #269:1:1]
 1 │ [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·                                ─────┬────
   ·                                     ╰── originates from here
   ╰────

When this is in a nu script, it does not cause the process to return a non-zero exit code, which is especially bad when using nushell scripts within complex pipelines (such as in a build system).

Create a file junk.nu with the following contents.

#!/usr/bin/env nu

def main [] {
  [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
}

Set the executable bit on it, and then in a bash shell, run it.

./junk.nu
1
2
3
Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[/home/aidan/junk.nu:3:1]
 3 │ def main [] {
 4 │   [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·    ┬
   ·    ╰── source value
 5 │ }
   ╰────

Error:
  × stop!
   ╭─[/home/aidan/junk.nu:3:1]
 3 │ def main [] {
 4 │   [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·                                  ─────┬────
   ·                                       ╰── originates from here
 5 │ }
   ╰────


~echo $?
0

Expected behavior

Something more like this, where only the first print is executed, and then the error immediately after terminates the entire loop.

❯ [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
1
Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #1:1:1]
 1 │ [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·  ┬
   ·  ╰── source value
   ╰────

Error:
  × stop!
   ╭─[entry #1:1:1]
 1 │ [1 2 3] | each { |n| print $n; error make {msg: "stop!"} }
   ·                                ─────┬────
   ·                                     ╰── originates from here
   ╰────

And when run in a script, for the exit code to be non-zero.

Screenshots

No response

Configuration

key value
version 0.77.0
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.67.1 (d5a82bbd2 2023-02-07) (built from a source tarball)
cargo_version cargo 1.67.0
build_time 1980-01-01 00:00:00 +00:00
build_rust_channel release
features default, zip
installed_plugins

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-handlingHow errors in externals/nu code are caught or handled programmatically (see also unhelpful-error)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions