Skip to content

Errors in each loops not handled well #8615

Open

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

Activity

  1. added
    A:error-handling
    on Mar 28, 2023
  2. tv42 commented on Dec 11, 2023

    @tv42

    Related.. I think these are all aspects of the same underlying problem: #10633, #10856

  3. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    Half of this is fixed: the exit code is 1 now.
    The loop still runs all items when the result goes to the display on 0.117:

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

    prints 1, 2, 3 and only then the error.
    With | ignore or a let it stops after the first item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-handling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions