Repository navigation
Closed as not planned
Closed as not planned
Copy link
Labels
A:error-handlingHow errors in externals/nu code are caught or handled programmatically (see also unhelpful-error)A:external-commandsIssues related to external commandsA:redirection-pipeAll related to redirection to files or more complex pipelines with STDERR
Description
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 versionHow 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 |
Metadata
Metadata
Assignees
Labels
A:error-handlingHow errors in externals/nu code are caught or handled programmatically (see also unhelpful-error)A:external-commandsIssues related to external commandsA:redirection-pipeAll related to redirection to files or more complex pipelines with STDERR
Activity
[-]Error of external commands in try-catch not propagated correctly[/-][+]Stderr/stdout of external commands in try-catch not propagated correctly[/+]Bahex commentedon May 22, 2025
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.
/dev/null(or$null_devicefromstd/utilto be cross platform)Check out
help pipe-and-redirectfor details.tee --stderrto copy the stderr stream to another command.samox73 commentedon May 24, 2025
Thanks @Bahex, the problem with
parquet2json | completeis that it cannot be used when a command produces too large of an output. I already used the workaround of writing stdout to/dev/nulland saving the error to a file.However, is my last example not a bug? Using
| ignoreinside a try block should not affect catch, right?Bahex commentedon May 24, 2025
@samox73
ignoreswallowing the errors from external commands is strange, thanks for pointing that out.I'm not sure if that was intended, as
ignoredoesn't swallow internal errors.kronberger-droid commentedon Nov 14, 2026
Since #18306
ignore --show-errorskeeps the failure of the external, so thecatchruns.The stderr can't go into the error itself since it already streamed to the terminal, as Bahex explained.