Skip to content

fix: honor redirects on eval, source and . - #196

Merged
ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:fix/eval-source-redirects
Nov 4, 2026
Merged

ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:fix/eval-source-redirects

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Description

eval, source and . ignored their redirects. eval "echo hi" > f printed to the terminal and left f empty, and eval 'read x' < file never saw the file, which matters more than it looks because Claude Code runs every command as eval '…' < /dev/null.

Now the three builtins apply their redirects in order like bash does, a target that can't be opened fails with status 1 without running the body, and the body still runs in the current shell, so variables and cd stick. One visible difference from bash: under a redirect the body's output is collected and written when it finishes, the same way a redirected { …; } group already works here, so it isn't streamed, stdout and stderr don't interleave, and the output limit applies even when it goes to a file. Commands without redirects run exactly as before.

Evidence of how this is fixed in Box

Strands Box carries a vendored copy of Shell and mostly fixed this already: it applies these redirects the way a redirected group is (UPSTREAM.md note), and 16 of this PR's 19 tests pass on it. What's still different there: eval '…; exit 4' > f doesn't exit, eval 'read m' <&0 inside a while read loop fails (a regression only Box has), and . /missing returns 0.

Related Issues

None.

Documentation PR

None.

Type of Change

Bug fix

Testing

I checked every case against bash and added tests for them, and without the fix they fail, while the existing eval and source tests and the Rust, Python and Node suites pass. I found a few separate bugs that this doesn't touch: $(eval …) loses its output, and break or return inside eval don't reach the enclosing loop or function. This PR and #199 both add a small helper at the same spot in src/os.rs, so whichever lands second needs a trivial rebase.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

eval, source and . ignored their redirections. `eval 'echo hi' >f`
printed to the terminal and left f empty, `2>` did not move stderr, and
`eval 'read x' <file` did not read the file. Agents such as Claude Code
run each command as `eval '...' < /dev/null`, so stdin matters too.

These builtins now apply their redirections in order, as bash does, so
`2>&1 >f` sends stderr to the old stdout and stdout to f. A target that
cannot be opened fails with status 1 and the body does not run. The
body still runs in the current shell, so variables and cd persist.
Under a redirection, the body's stdout and stderr are buffered and
written when it ends, as for a redirected group, so the two streams
do not interleave.

@kevin-orellana kevin-orellana left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: I reproduced eval and source returning success after redirected output failed to reach its file. Could we report the forwarding error and return a failure status in a follow-up? Regression cases for both builtins, including append, would cover the affected paths.

Details

Tested the native Rust API on Amazon Linux 2023 ARM64 with Rust 1.97.1 and --no-default-features. The ordinary-file results below use PR head 6b2c7f5db5872f17db8a48df9e88cfdec720ecf0 combined locally with main at f5b232dcdf9462bb8022a352b47e189a23d8610a. This was an uncommitted merge. Main supplies the separate write-acknowledgement fix.

With max_file_size(4) and /tmp/f containing abc, run:

eval 'printf de' >> /tmp/f
  • Observed: status 0, no diagnostic, and /tmp/f still contains only abc.
  • Expected: a nonzero status and a write-error diagnostic.
  • Control: printf de >> /tmp/f returns 1 with the file-size-limit diagnostic and preserves abc.

Fresh writes through eval and a sourced script also returned 0 after output exceeded the file limit. Their ordinary-command controls returned 1. The final forwarding loop discards the write result. These ordinary-file observations apply to the combined revisions, not the PR head alone.

@ferdingler
ferdingler merged commit 3dcb69d into strands-agents:main Nov 4, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants