Skip to content

Fix ir eval error on branched block redirection (Fixes #16582)#16676

Merged
WindSoilder merged 6 commits intonushell:mainfrom
fixerer:redirection_for_blocks
Sep 19, 2025
Merged

Fix ir eval error on branched block redirection (Fixes #16582)#16676
WindSoilder merged 6 commits intonushell:mainfrom
fixerer:redirection_for_blocks

Conversation

@fixerer
Copy link
Copy Markdown
Contributor

@fixerer fixerer commented Sep 13, 2025

Description

Attempt to fix issue #16582 by avoiding duplicated write and close calls for an out>/err> redirected file for try-catch and if-else blocks.

Similarly to this PR a function to identify if a code block is being handeled, and if so skip the invocation of finish_redirection so that no additional writes are added. (The blocks perform redirections on their own and this then avoids the duplication).

Release notes summary - What our users need to know

Fixed IR evaluation error caused by redirecting output of branched blocks

The following commands will no longer raise ir-eval errors:

  • if true { echo "hey" } out> /dev/null
  • if false { echo "hey" } else { echo "ho" } out> /dev/null
  • try { 1/0 } catch { echo "hi" } out> /dev/null

IR before changes:
before.txt

IR after changes:
after.txt

Tests

Added 8 tests.

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

*Description*

Attempt to fix issue [nushell#16582](nushell#16582)
by avoiding duplicated `write` and `close` calls for an `out>`/`err>`
redirected file for `try-catch` and `if-else` blocks.

Similarly to [this PR](nushell#15617)
a function to identify if a code block is being handeled, and if so skip
the invocation of `finish_redirection` so that no additional writes are
added. (The blocks perform redirections on their own and this
then avoids the duplication).

*User-Facing Changes*

The following commands will no longer raise ir-eval errors:

 - `if true { echo "hey" } out> /dev/null`
 - `if false { echo "hey" } else { echo "ho" } out> /dev/null`
 - `try { 1/0 } catch { echo "hi" } out> /dev/null`

*Tests*

Added 8 tests.
Copy link
Copy Markdown
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this. Based on the tests it looks already pretty good for STDOUT. Unsure about the STDERR.

Would like to refer you to @WindSoilder for the details.

Question on the while construct - notably why it is allowed while
the for one isn't. Should this be the case, and what should the
output result be if so.
@fixerer fixerer force-pushed the redirection_for_blocks branch from f6767be to ad77ef3 Compare September 17, 2025 14:48
@WindSoilder WindSoilder added the notes:fixes Include the release notes summary in the "Bug fixes" section label Sep 17, 2025
@fixerer fixerer requested a review from WindSoilder September 18, 2025 12:05
@fixerer fixerer marked this pull request as ready for review September 18, 2025 13:19
@fixerer fixerer force-pushed the redirection_for_blocks branch from 2e0390d to 14e1536 Compare September 18, 2025 20:54
Copy link
Copy Markdown
Contributor

@WindSoilder WindSoilder left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good to me

@WindSoilder WindSoilder merged commit 75a8a2c into nushell:main Sep 19, 2025
16 checks passed
@github-actions github-actions bot added this to the v0.108.0 milestone Sep 19, 2025
@Bahex Bahex added the notes:ready The "Release notes summary" section of this PR is ready to be included in our release notes. label Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Include the release notes summary in the "Bug fixes" section notes:ready The "Release notes summary" section of this PR is ready to be included in our release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants