Skip to content

refactor!: collecting a stream with an error raises an error - #16738

Merged
WindSoilder merged 2 commits into
nushell:mainfrom
Bahex:ListStream-collect-error
Sep 24, 2025
Merged

WindSoilder merged 2 commits into
nushell:mainfrom
Bahex:ListStream-collect-error

Conversation

@Bahex

@Bahex Bahex commented Sep 23, 2025 •

Copy link
Copy Markdown
Member

Sort of a follow up to #16732, makes the stream-collecting error handling universal.
This fixes similar bugs everywhere.

Release notes summary - What our users need to know

Collecting a stream that contains errors now raises an error itself

Previously, if an error was returned as a stream item and you collected that stream into a value, you would and up with a list that has an error item which you wouldn't know about until you interacted with that specific item.

You could even end up with a list entirely made up of errors:

let items = 1..10 | each {|n| error make { msg: $"Error ($n)" } }
items | describe
# list<error>

With this release that's no longer the case:

let items = 1..10 | each {|n| error make { msg: $"Error ($n)" } }
Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #1:1:13]
 1 │ let items = 1..10 | each {|n| error make { msg: $"Error ($n)" } }
   ·             ──┬──
   ·               ╰── source value
   ╰────

Error: 
  × Error 1
   ╭─[entry #1:1:31]
 1 │ let items = 1..10 | each {|n| error make { msg: $"Error ($n)" } }
   ·                               ─────┬────
   ·                                    ╰── originates from here
   ╰────

Simply attempting to collect such a stream immediately re-throws the first error encountered.

This also has the effect of preserving the context of errors raised some nested command calls like...

Errors raised in nested each calls preserve their context

Previously, errors raised in nested each calls (and other commands that returned streams) could lose their outer/caller context.

If the closure returned a stream, errors in the stream were not properly chained and lost this context. Running the following code:

0..1 | each {
    0..1 | each {|e|
        error make {msg: boom}
    }
}

we get:

Before After
error-chain-before error-chain-after
Error Chain ANSI Output

Before:

Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[source:2:2]
 1 │ 0..1 | each {
 2 │     0..1 | each {|e|
   ·     ──┬─
   ·       ╰── source value
 3 │         error make {msg: boom}
   ╰────

Error: 
  × boom
   ╭─[source:3:3]
 2 │     0..1 | each {|e|
 3 │         error make {msg: boom}
   ·         ─────┬────
   ·              ╰── originates from here
 4 │     }
   ╰────

After:

Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[source:1:1]
 1 │ 0..1 | each {
   · ──┬─
   ·   ╰── source value
 2 │     0..1 | each {|e|
   ╰────

Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[source:2:2]
 1 │ 0..1 | each {
 2 │     0..1 | each {|e|
   ·     ──┬─
   ·       ╰── source value
 3 │         error make {msg: boom}
   ╰────

Error: 
  × boom
   ╭─[source:3:3]
 2 │     0..1 | each {|e|
 3 │         error make {msg: boom}
   ·         ─────┬────
   ·              ╰── originates from here
 4 │     }
   ╰────

Tasks after submitting

N/A

Sort of a follow up to nushell#16732, makes the stream-collecting error 
handling universal.
This fixes similar bugs everywhere.
@132ikl

132ikl commented Sep 23, 2025

Copy link
Copy Markdown
Member

amazing! some are saying this is the best nushell PR yet

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I love the change too

@WindSoilder
WindSoilder merged commit 2c8bcc8 into nushell:main Sep 24, 2025
@github-actions github-actions Bot added this to the v0.108.0 milestone Sep 24, 2025
@Bahex Bahex added notes:breaking-changes Noted in Breaking Changes section A:error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) A:streaming Issues related to streaming data (or collecting data when it should be streamed) labels Oct 2, 2025
@Bahex Bahex mentioned this pull request Oct 2, 2025
@Bahex Bahex added the notes:ready Indicates Ready for Release notes label Oct 2, 2025
@Bahex
Bahex deleted the ListStream-collect-error branch March 22, 2026 13:03
fdncred pushed a commit that referenced this pull request Sep 10, 2026
…18976)

# Description

Fixes #18928. `length`, `columns`, and `is-empty` (and `is-not-empty`)
silently consumed `Value::Error` stream items instead of surfacing them,
producing wrong, silent results (e.g. `where` with a type error →
`length` reports the unfiltered row count, `columns` returns `[]`,
`is-empty` returns the wrong boolean).

Now all three propagate the first error encountered in the input stream:

- `length`: the `ListStream` count loop calls `unwrap_error()` on each
value, returning the inner `ShellError` instead of counting it.
- `columns`: scans both the `Value::List` and `ListStream` inputs for
`Value::Error` before computing columns, returning the inner error (a
list containing an error previously short-circuited `get_columns` to
`[]`).
- `is-empty` / `is-not-empty`: the `ListStream` and `Value` branches of
`empty()` return the inner error instead of treating an error item as
"not empty" (a bare `Value::Error` input previously fell through to
`is_empty()` → `false`).

This is consistent with the error propagation that #16738 established
for stream-collecting commands (`into_value`, `unwrap_error`); these
three commands bypassed it with manual iteration.

## Verification

- Three new integration tests (one per command), all asserted to raise
the propagated error.
- Confirmed each test reproduces the bug pre-fix:
  - `columns` returned `List([])` instead of erroring
  - `length` returned the wrong count
  - `is-empty` returned the wrong boolean
- Post-fix all pass.
- `cargo fmt --check -p nu-command`: clean
- `cargo clippy -p nu-command --all-targets`: clean
- Full `nu-command` tests: 2884 pass, 22 fail — all pre-existing Windows
symlink/privilege failures (e.g. `can not create symlink: A required
privilege is not held by the client`), unrelated to this change.

# User-Facing Changes

`[[name size]; [a 100b] [b 200b]] | where size <= 150 | length` now
errors with `nu::shell::operator_incompatible_types` instead of
returning `2`, and likewise for `columns` / `is-empty`.

# Tests + Formatting

- Added tests under `crates/nu-command/tests/commands/` (`length.rs`,
`empty.rs`, new `columns.rs`).
- `cargo fmt --check` and `cargo clippy` clean as noted above.

## AI disclosure

This change was authored with the assistance of an AI coding agent.
fdncred added a commit that referenced this pull request Nov 12, 2026
…19467)

## Description

A stream carries a failure as an item, so a command that read that item
and then dropped it also dropped the failure. `last`, `get 2`, `skip 1`
and others returned a normal result with exit code 0. This was the gap
#16738 and #18976 closed for collecting and for `length`, `columns` and
`is-empty`.

`exit` makes it worse. Since #18389 (0.114.0) `exit` travels as an error
instead of ending the process, so the same commands discarded it and the
script kept running. 0.113.1 exits 7 on the commands below, and 0.114.0
carries on with exit code 0.

Commands that read and drop an item now raise its error or pass it
along, and commands that bury an item in a record or list now pass the
error along instead. `;` no longer ignores an error returned as a single
value. A closure is never handed an `exit`, because `catch` can't handle
one and the closure could ignore it. `do --ignore-errors` no longer
ignores `exit`. Items that never ran stay unevaluated, so `first` and
`take` are as lazy as before.

## User-facing changes (Release notes)

- An error in a stream is no longer lost when a command drops that item.
This covers `last`, `get`, cell paths on a stream, `skip`, `skip while`,
`skip until`, `take while`, `take until`, `select`, `drop`, `drop nth`,
`slice`, `every`, `update`, `upsert`, `window`, `lines`, `zip` and `to
text`.
- `enumerate`, `wrap`, `zip`, `chunks`, `chunk-by`, `window` and `split
list` no longer hide a stream error inside the record or list they
build.
- An external now reports an error or `exit` in its input once it
finishes, so `boom | to text | ^cat` raises `boom` and `try` can catch
it. `do --ignore-errors` still ignores the error, but not `exit`.
- A closure that returns an error value now counts as failing in `take
while`, `take until`, `skip while`, `skip until`, `chunk-by`, `split
list` and `where`, where it used to count as `false`.
- `exit` inside a closure always ends the script again, whichever
command consumes the stream, and `do --ignore-errors` no longer ignores
it.
- `;` no longer discards an error returned as a single value, and
`catch` now sees errors from `first` and `last`.

### Breaking Changes
- a closure that fails in `take while`, `take until`, `skip while`,
`skip until`, `chunk-by` or `split list` now raises its error, as
`where` already does. It used to count as `false`, so `[{a: 1} {b: 2}] |
take while {|r| $r.a == 1 }` returned the first row and now reports the
missing column. Use `$r.a?` for optional columns.
- a script that relied on `last`, `skip` or similar to get past an error
now fails. To handle a failed item, use `each {|row| try { $row |
ignore; $row } catch { ... } }`.
- `reverse`, `shuffle`, `sort-by`, `drop` and `last` now raise an error
item instead of keeping it among the rows, so that `try` form has to
come before them.
- `to text` now raises an error instead of writing its debug form into
the text, and `each {|y| $y; 'ok' }` raises an error input instead of
discarding it at the `;`.

## Additional notes

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

Labels

A:error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) A:streaming Issues related to streaming data (or collecting data when it should be streamed) notes:breaking-changes Noted in Breaking Changes section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants