Skip to content

Stderr/stdout of external commands in try-catch not propagated correctly #15802

Closed as not planned

Description

@samox73

Describe the bug

When a nushell built-in command throws an error, we can catch it and see its value:

❯ try { ls arst } catch {|e| print $"err: ($e)"}
err: {msg: Not found, debug: Io(IoError { kind: Std(NotFound), span: Span { start: 337662, end: 337666 }, path: Some("/arst"), additional_context: None, location: None }), raw: Io(IoError { kind: Std(NotFound), span: Span { start: 337662, end: 337666 }, path: Some("/arst"), additional_context: None, location: None }), rendered: Error: nu::shell::io::not_found

  × Not found
   ╭─[entry #249:1:10]
 1 │ try { ls arst } catch {|e| print $"err: ($e)"}
   ·          ──┬─
   ·            ╰── Not found
   ╰────
  help: '/arst' does not exist
, json: {"msg":"Not found","labels":[{"text":"Not found","span":{"start":337662,"end":337666}}],"code":"nu::shell::io::not_found","url":null,"help":"'/arst' does not exist","inner":[]}}

However, if we try the same with an external command, e.g. in my case I used parquet2json

❯ try { parquet2json } catch {|e| print $"err: ($e)"}
A command-line tool for streaming Parquet as line-delimited JSON

Usage: parquet2json <FILE> <COMMAND>

Commands:
  cat       Outputs data as JSON lines
  schema    Outputs the Thrift schema
  rowcount  Outputs only the total row count
  help      Print this message or the help of the given subcommand(s)

Arguments:
  <FILE>  Location of Parquet input file (file path, HTTP or S3 URL)

Options:
  -h, --help     Print help
  -V, --version  Print version
err: {msg: External command had a non-zero exit code, debug: NonZeroExitCode { exit_code: 2, span: Span { start: 337705, end: 337717 } }, raw: NonZeroExitCode { exit_code: 2, span: Span { start: 337705, end: 337717 } }, rendered: Error: nu::shell::non_zero_exit_code

  × External command had a non-zero exit code
   ╭─[entry #250:1:7]
 1 │ try { parquet2json } catch {|e| print $"err: ($e)"}
   ·       ──────┬─────
   ·             ╰── exited with code 2
   ╰────
, json: {"msg":"External command had a non-zero exit code","labels":[{"text":"exited with code 2","span":{"start":337705,"end":337717}}],"code":"nu::shell::non_zero_exit_code","url":null,"help":null,"inner":[]}, exit_code: 2}

we can see that the error is written to stderr.

I couldn't find any docs whether this is intentional. I get that this discrepancy between built-in and external commands might be a design choice, which is fine. However, it gets really funky if we ignore the stdout of an external command in a try-catch:

❯ try { parquet2json | ignore } catch {|e| print $"err: ($e)"}
A command-line tool for streaming Parquet as line-delimited JSON

Usage: parquet2json <FILE> <COMMAND>

Commands:
  cat       Outputs data as JSON lines
  schema    Outputs the Thrift schema
  rowcount  Outputs only the total row count
  help      Print this message or the help of the given subcommand(s)

Arguments:
  <FILE>  Location of Parquet input file (file path, HTTP or S3 URL)

Options:
  -h, --help     Print help
  -V, --version  Print version

How to reproduce

Execute

try { echo "arst"; parquet2json | ignore } catch {|e| print $"err: ($e)"}

and watch the whole stdout of the try-catch block disappear.

Expected behavior

The ignore pipeline should ignore stdout of the external command, not the whole try-catch block.

Ideally I also think that the stderr of external commands should be captured by the error in the catch block.

Configuration

key value
version 0.104.0
major 0
minor 104
patch 0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.86.0 (05f9846f8 2025-03-31) (Homebrew)
cargo_version cargo 1.86.0
build_time 2025-04-29 23:31:45 +00:00
build_rust_channel release
allocator standard
features default, sqlite, trash
installed_plugins

Activity

  1. added
    status:needs-triage
    on May 22, 2025
  2. changed the title [-]Error of external commands in try-catch not propagated correctly[/-] [+]Stderr/stdout of external commands in try-catch not propagated correctly[/+] on May 22, 2025
  3. Bahex commented on May 22, 2025

    @Bahex
    Member

    The issue is, we can't know whether an external command failed or not until it terminates. So anything it writes to stderr is will be already printed on your terminal before an error is thrown.

    There is a few ways to handle stderr output and non-0 exit codes from externals:

    • complete: this command will do exactly what you want, it captures the stdout, stderr and exit code of external commands. The downside of this command is you won't get any output until the command terminates.
      Also, because it handles the exit code of the command itself, it doesn't throw an error.
      parquet2json | complete
    • rediractions: If you want to completely ignore stderr and have streaming output from stdout, you can redirect stderr to /dev/null (or $null_device from std/util to be cross platform)
      Check out help pipe-and-redirect for details.
      use std/util null_device
      parquet2json e> $null_device | ... # stdout can be used as usual
    • There is also tee --stderr to copy the stderr stream to another command.
  4. added
    A:external-commands
    A:redirection-pipe
    A:error-handling
    and removed
    status:needs-triage
    on May 22, 2025
  5. samox73 commented on May 24, 2025

    @samox73
    Author

    Thanks @Bahex, the problem with parquet2json | complete is that it cannot be used when a command produces too large of an output. I already used the workaround of writing stdout to /dev/null and saving the error to a file.

    However, is my last example not a bug? Using | ignore inside a try block should not affect catch, right?

  6. Bahex commented on May 24, 2025

    @Bahex
    Member

    @samox73 ignore swallowing the errors from external commands is strange, thanks for pointing that out.

    I'm not sure if that was intended, as ignore doesn't swallow internal errors.

  7. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    Since #18306 ignore --show-errors keeps the failure of the external, so the catch runs.
    The stderr can't go into the error itself since it already streamed to the terminal, as Bahex explained.

    AI:X EXECUTE

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-handlingA:external-commandsA:redirection-pipe

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions