Skip to content

Misleading OK error code if there is an error in a for loop #8569

Closed

Description

@presidento

Describe the bug

If there is an (unhandled) error in a for loop, the exit code is 0.

How to reproduce

Let's have a exitcode.nu file with the contents:

for i in 1..10 {
    echo $"Calculating #($i)"
    nu -c '1 / 0'
}

Run it:

❯ nu exitcode.nu
Calculating #1
Error: nu::shell::division_by_zero (link)

  × Division by zero.
   ╭─[source:1:1]
 1 │ 1 / 0
   ·   ┬
   ·   ╰── division by zero
   ╰────


❯ echo $env.LAST_EXIT_CODE
0

Expected behavior

I expected nu to have a non-zero exit code, like when the error is outside a for loop.

Screenshots

No response

Configuration

key value
version 0.76.0
branch
commit_hash bc38a6a
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.67.1 (d5a82bbd2 2023-02-07)
cargo_version cargo 1.67.1 (8ecd4f20a 2023-01-10)
pkg_version 0.76.0
build_time 2023-02-23 08:03:29 -06:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins

Additional context

@amtoine please note that because of this bug, now tests.nu will pass, even if one of the tests fails.

Activity

  1. added
    category:bug
    A:error-handling
    on Mar 22, 2023
  2. presidento commented on Mar 22, 2023

    @presidento
    ContributorAuthor

    It is the same in version 0.77.1

  3. rgwood commented on Mar 23, 2023

    @rgwood
    Contributor

    If anyone wants to take a look at this, the relevant code is here:

    if exit_code != 0 {
    break;
    }

    And here:

    if exit_code != 0 {
    break;
    }

    for probably needs to return an error instead of just calling break.

  4. added this to the v0.98.0 milestone on Sep 7, 2024
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-handlingcategory:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions