Skip to content

Impossible to both inherit the stderr/stdout and collect the exit code of the subprocess #12643

Open

Description

@Veetaha

I'd like to collect the exit code of the subprocess, but let it inherit the stdout/stderr.

Now that run-external automatically captures the stderr/stdout when there is a | after it, it is impossible to do.

In the version of nushell 0.87.1 my code that achieved the desired behavior was this, but now it's broken:

let result = (run-external $cmd $args | complete)
if $result.exit_code != 0 {
  # Print the exit code and throw an error...
}

No workarounds available

@IanManske suggested doing smth like the following, but it doesn't work:

try { run-external $cmd $args }
    if $env.LAST_EXIT_CODE != 0 {
}

As you can see, the LAST_EXIT_CODE is always set to zero

try { run-external bash '-c' 'echo foo && false' }; $env.LAST_EXIT_CODE 

Also when I experimented with it, the LAST_EXIT_CODE seemed to point to the previously executed command in the REPL instead of the prev. statement. See the following, here the LAST_EXIT_CODE was 1 because prev. REPL command was a syntax error
image

Activity

  1. fdncred commented on Apr 24, 2024

    @fdncred
    Contributor

    I think maybe I'm missing the point here. What's not working?

    ❯ let ext = "bash"
    ❯ let arg = "-c"
    ❯ let cmd = "echo foo && false"
    ❯ run-external $ext $arg $cmd | complete
    ╭───────────┬─────╮
    │ stdout    │ foo │
    │           │     │
    │ stderr    │     │
    │ exit_code │ 1   │
    ╰───────────┴─────╯
    ❯ let cmd = "echo foo && true"
    ❯ run-external $ext $arg $cmd | complete
    ╭───────────┬─────╮
    │ stdout    │ foo │
    │           │     │
    │ stderr    │     │
    │ exit_code │ 0   │
    ╰───────────┴─────╯
  2. Veetaha commented on Apr 24, 2024

    @Veetaha
    Author

    @fdncred the main point is to inherit the stdout/stderr - not capture it, meaning stdout/stderr must be propagated to the current process that spawned the child process. For example, when I spawn terraform apply I want to see its logs in real time, because it takes a lot of time time to run and also reports its progress on stdout/stderr. But I also want to know its exit to code to figure out if it failed or not.

    One of the problems is that --redirect-stdout/stderr parameters became deprecated in run-external, and behavior of capturing the stdout/stderr became implicit, and I can't opt out of it.

  3. fdncred commented on Apr 24, 2024

    @fdncred
    Contributor

    I think it's this PR #11934 that changed that part. @IanManske Any ideas how to help out here?

    There's also a tee command that may help #11928
    This PR also explains some new functionality with redirection for externals #11708

  4. IanManske commented on Apr 24, 2024

    @IanManske
    Member

    @Veetaha I made a PR that would allow you to get the exit code in a catch block if the external command fails. In the future, there will hopefully be a rework of I/O redirection and/or exit codes that should make this more intuitive and reliable.

  5. added
    status:needs-design
    A:redirection-pipe
    on Apr 25, 2024
  6. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    The exit code is available as $e.exit_code in catch since #12648.
    try { ^bash -c "exit 3" }; $env.LAST_EXIT_CODE without a catch still gives 0 on 0.117.

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:redirection-pipestatus:needs-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions