Repository navigation
fix(parser): treat a digit as a redirect fd only when it touches the operator - #230
Conversation
f371772 to
0a2f8f9
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested, one line to fix. The IO_NUMBER fix is right, and every case you claimed matches bash. But it also turns the target of >&/<& into an fd when that target sits directly before another redirect, so echo hi 2>&1>f now fails with expected filename after '>&'. It works on main and in bash. Suggested fix and two test cases are inline.
- Suite at
0a2f8f9:cargo test --workspace --all-targets→ 2201 passed, 0 failed. - I compared 151 redirect forms across PR, main and bash 5.2. The
>&N<op>family is the only regression. - With the one-line fix applied,
shell_integrationpasses 1625/1625. The two new cases fail without the fix.
What I verified
| command | PR | main | bash | PR + fix |
|---|---|---|---|---|
echo 3 > o; cat o |
3 |
(empty) | 3 |
3 |
echo a 2 > q; cat q |
a 2 |
a |
a 2 |
a 2 |
echo hi 2>&1>f; cat f |
parse error | hi |
hi |
hi |
echo x >&2>>f; cat f |
parse error | x |
x |
x |
{ echo a; } 2>&1>f; cat f |
parse error | a |
a |
a |
echo x >f 2>&1<f; cat f |
parse error | x |
x |
x |
echo x 2>&1, echo hi 1>&2, echo a 9>&- |
ok | ok | ok | ok |
- Also newly correct:
echo a 1 >f,echo \2>f, a line continuation before>, andecho 3 >o &. - Now an error where main silently accepted it:
echo hi >2>f,>>2>f,>|2>f,<2>f,cat <<2>f. Bash rejects all of these as syntax errors too, so this is fine. The fix leaves them unchanged. - Checked and unaffected:
case,for … in,(( )),$(( )), heredocs, compound and prefix redirects,exec 3>f,eval, command substitution.tok_name's slice can't panic, because fd is always 0–9. - Not run: clippy and fmt, since they aren't installed in my sandbox. The fix is a single condition line.
Reading order: the tokenizer change at src/parser.rs:1000-1009, then the two fd-prefix consumers at :1144 and :1771, then the test table.
Appendix: non-blocking, not filed (2)
- Pre-existing, identical on main:
x=2>fdoesn't createf, andf(){ …; } 2>edrops the redirect. Neither is touched by this PR. - Syntax errors exit 1, while bash exits 2. That's the same on main for every syntax error.
Evidence (artifacts)
Uploaded to strands-agents/shell/pr/230/: cargo-test-pr-head.log, cargo-test-with-fix.log, suggested-fix.diff, spotchecks.txt, adversarial-harness.sh, adversarial-results.txt.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The 2>&1>f regression is fixed in 8b48bca, and I found nothing new.
- ✅ Fixed:
2>&1>f,>&2>>f,{ …; } 2>&1>fand>f 2>&1<fgive the same output as main and bash again. The spaced variants (2>& 1>f,>& 2>f) also match bash. - Suite at
8b48bca:cargo test --workspace --all-targets→ 2204 passed, 0 failed. - My 151-case harness (PR vs main vs bash 5.2) now has 0 regressions. The case that regressed is back to OK, and the other buckets are unchanged.
Details
The one remaining PR-vs-bash difference I hit is a bare redirect line like 2>f on its own, which doesn't create the file. Main does the same, so it isn't this PR. Your new parser test, which checks that the adjacent and spaced forms produce the same syntax tree, is a good way to pin this down.
Artifacts in strands-agents/shell/pr/230/: cargo-test-8b48bca.log, spotchecks-8b48bca.txt, adversarial-results-8b48bca.txt.
|
Thanks for rechecking the adjacent and spaced cases. I am leaving the bare redirect-line behavior for a separate fix because it also happens on main and does not come from this change. |
Description
A number separated from a redirect was being treated as its file descriptor, so
echo 3 > owrote an empty file andecho a 2 > qlost the2. This silently changed ordinary command arguments.I changed parsing so an unquoted digit only supplies the descriptor when it touches the redirect operator. Digits after input or output duplication operators stay duplication targets, so adjacent redirects such as
2>&1>fcontinue to work. Multi-digit descriptors and the existing VFS reader-duplication limitation remain outside this parser change.I checked 32 commands against Bash for identical output and status, plus five invalid forms that both shells reject. I broke the duplication guard on purpose and the regression tests caught it. The full Rust suite, formatting, lint and documentation checks pass locally.
Related Issues
Part of #207
Documentation PR
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
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.