Repository navigation
fix: honor redirects on eval, source and . - #196
Conversation
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
left a comment
There was a problem hiding this comment.
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/fstill contains onlyabc. - Expected: a nonzero status and a write-error diagnostic.
- Control:
printf de >> /tmp/freturns 1 with the file-size-limit diagnostic and preservesabc.
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.
Description
eval,sourceand.ignored their redirects.eval "echo hi" > fprinted to the terminal and leftfempty, andeval 'read x' < filenever saw the file, which matters more than it looks because Claude Code runs every command aseval '…' < /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
cdstick. 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' > fdoesn't exit,eval 'read m' <&0inside awhile readloop fails (a regression only Box has), and. /missingreturns 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, andbreakorreturninsideevaldon't reach the enclosing loop or function. This PR and #199 both add a small helper at the same spot insrc/os.rs, so whichever lands second needs a trivial rebase.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.