Skip to content

IR: allow subexpression with redirection. - #15617

Merged
sholderbach merged 4 commits into
nushell:mainfrom
WindSoilder:subexpression_redirection
Apr 24, 2025
Merged

sholderbach merged 4 commits into
nushell:mainfrom
WindSoilder:subexpression_redirection

Conversation

@WindSoilder

Copy link
Copy Markdown
Contributor

Description

Try to fixes #15326 in another way.

The main point of this change is to avoid duplicate write and close a redirected file. So during compile, if compiler know current element is a sub-expression(defined by private is_subexpression function), it will no longer invoke finish_redirection.

In this way, we can avoid duplicate finish_redirection.

User-Facing Changes

(^echo aa) o> /tmp/aaa will no longer raise an error.

Here is the IR after the pr:

# 3 registers, 12 instructions, 11 bytes of data
# 1 file used for redirection
   0: load-literal           %1, string("aaa")
   1: open-file              file(0), %1, append = false
   2: load-literal           %1, glob-pattern("echo", no_expand = false)
   3: load-literal           %2, glob-pattern("true", no_expand = false)
   4: push-positional        %1
   5: push-positional        %2
   6: redirect-out           file(0)
   7: redirect-err           caller
   8: call                   decl 135 "run-external", %0
   9: write-file             file(0), %0
  10: close-file             file(0)
  11: return                 %0

Tests + Formatting

Added 3 tests.

After Submitting

Maybe need to update doc nushell/nushell.github.io#1876

@WindSoilder WindSoilder added the notes:fixes Noted in "Bug fixes" section label Apr 23, 2025

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for quickly coming up with a new approach Wind!

The structure in the compiler seems plausible to me but I am confused by the test.

Comment thread crates/nu-engine/src/compile/mod.rs Resolved
@sholderbach
sholderbach merged commit 7add38f into nushell:main Apr 24, 2025
@sholderbach sholderbach added the A:ir-compiler Issues/pull requests related to compiling Nushell parser output to internal representation (IR) label Apr 24, 2025
@github-actions github-actions Bot added this to the v0.104.0 milestone Apr 24, 2025
fixerer pushed a commit to fixerer/nushell that referenced this pull request Sep 13, 2025
*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.
WindSoilder pushed a commit that referenced this pull request Sep 19, 2025
### Description

Attempt to fix issue
[#16582](#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](#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).

### Release notes summary - What our users need to know

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](https://github.com/user-attachments/files/22308084/before.txt)

IR after changes: 

[after.txt](https://github.com/user-attachments/files/22308085/after.txt)

### Tests

Added 8 tests.

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

---------

Co-authored-by: Fixerer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:ir-compiler Issues/pull requests related to compiling Nushell parser output to internal representation (IR) notes:fixes Noted in "Bug fixes" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IR evaluation error when redirecting parenthesized expression to a file

2 participants