Repository navigation
feat(set): support set -o NAME and pipefail - #217
Conversation
68c3d0e to
330e382
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
@ferdingler Approve, with two should-fixes. set -euo pipefail works now, and pipefail matches bash 5.2 on every pipeline shape I tried (false | true, 3 stages, ( exit 3 ) | …, { …; } | cat, !, if, $(…), subshell non-leak, host-write failures). With pipefail off, results match main except for one improvement (noted below). CI is green, and 1650/1650 shell_integration tests pass locally at 330e382.
- 🟡 A bad option makes
setfail open:set -euo pipefialkeeps running with no flags set, where bash and main both exit 2 (set.rs:70). - 🟡 Under pipefail,
return Non the left of a pipe is lost:f() { { return 3; } | cat; }; freturns 0 instead of 3 (exec.rs:1996→captured_status,:1793).
Each fix is about one line, and I checked both against bash and the test suite (details below).
✅ What I verified, Questions, Appendix
Verified (head 330e382, compared against main d4c57a7 and /usr/bin/bash 5.2)
cargo test --test shell_integration: 1650 passed, 0 failed. CI rollup: SUCCESS.- Pipefail on, bash = PR:
false|true→1,true|false|true→1,( exit 3 )|true→3,{ false; }|cat→1,! false|true→0,if false|true→else branch,"$(false|true; echo $?)"→1,(set -o pipefail); false|true→0,set +o pipefailinside a subshell doesn't leak. - Pipefail off, main = PR = bash:
false|true→0,{ false; }|cat→0,true|false→1. - Host writes (330e382):
echo hi > RO | truegives 0 with pipefail off (same as main and bash) and 1 with it on (same as bash).true | echo hi > ROgives 1 in both modes.set -eo pipefail; echo hi > RO | true; echo reachedexits 1, like bash. The newsettle_host_writescall atexec.rs:3802doesn't settle twice:HostWrites::settletakes the closed writes out of the list (os.rs:221-227), so the caller's second settle finds nothing. set -o/set +ooutput for the four supported options matches bash's lines, andset +ooutput can be passed toeval.- Both suggested fixes: applied one at a time, rebuilt, re-ran the repro and the suite (see the inline comments).
- Adversarial pass: the first run timed out at 900s. A re-run finished from its saved battery output (~300 cases compared across bash, main and the PR).
Questions (non-blocking)
- Pipefail now stays on across
Shell.runcalls, asset -ealready does (the bind test attests/shell_integration.rs:6843-6857depends on that). #207's P2 list calls this exact behaviour a gotcha. Is that intended, with a reset planned in a separate change?
Reading order: src/builtins/set.rs → run_pipeline status assembly (exec.rs:3770-3815) → the two compound-left paths (exec.rs:1990-2015, 2240-2270) → tests.
Appendix: non-blocking (5)
- ⚪ Improvement worth a line in the PR body: with pipefail off, an empty or assignment-only last stage now returns 0 (
false | FOO=1; echo $?: main 1, PR 0, bash 0).stage_exitsstarts each stage at 0, whereas main carried the previous stage's status forward. - ⚪ Pre-existing, already covered by open #232:
x=$(false); echo $?prints 0 on main. Soset -euo pipefail; x=$(a | b)won't abort until #232 lands. Worth adding a pipefail case to that PR. - ⚪ Documented follow-up:
set -o pipefail -- a b→--: unsupported option(bash sets$#=2). Main has the same gap forset -e -- a b. - ⚪ Under pipefail, a writer cut off by
headexits with status 1 (e.g.sort big | head -1→ 1 plussort: pipe closed), where bash gives 141. The pipeline fails in both shells, so only the code differs. - ⚪ Test gaps: there's no test that
(set -o pipefail)doesn't leak, or for( exit 3 ) | catunder pipefail. I checked both by hand and they're correct.
Evidence uploaded under strands-agents/shell/pr/217/: repro-pr217.sh / .out, shell_integration-test.log, fix-verification.txt.
|
@strandly-the-agent Please review this PR 5383e37 fixes the compound-return status and adds the missing regression coverage. The separate atomic-versus-sequential set-option choice is still pending, and its thread remains open. |
strandly-the-agent
left a comment
There was a problem hiding this comment.
@ferdingler Still approve at 5383e37. Status of my earlier findings:
- ✅ Fixed:
return Non the left of a pipe under pipefail (exec.rs:1793). Both repros now return 3, like bash. I also tested the fix's other caller, command substitution (exec.rs:1864), and found no regressions. - ⏸️ Open, waiting on an owner decision: whether
setapplies options all at once or one by one (set.rs:70). My view hasn't changed, and I won't re-raise it. It doesn't block the PR.
Locally, shell_integration passes 1655/1655 tests, including the 5 new regression tests. CI was still queued when I posted.
Fix verification (bash 5.2 vs 330e382 vs 5383e37)
| Script | bash | before | after |
|---|---|---|---|
set -o pipefail; f() { { return 3; } | cat; }; f; echo $? |
3 | 0 | 3 |
set -o pipefail; f() { echo $({ return 3; } | cat; echo $?); }; f |
3 | 0 | 3 |
set -e -o pipefail; f() { { return 3; } | cat; echo unreachable; }; f |
exits | unreachable |
exits |
set -o pipefail; f() { { false; return; } | cat; }; f; echo $? |
1 | 1 | 1 |
set -o pipefail; f() { { return 0; } | false; }; f; echo $? |
1 | 1 | 1 |
f() { { return 3; } | cat; }; f with pipefail off |
0 | 0 | 0 |
$(…) path: f() { v=$(echo a; return 4); echo "v=$v"; } |
v=a | v=a | v=a |
$(…) path: f() { echo "[$(return 3; echo no)]"; } |
[] | [] | [] |
return in a loop body piped to cat |
loop 0 ×2 | same | same |
One difference, not worth acting on: a return outside any function ({ return 3; } \| cat at top level) now gives 3. Bash gives 2 plus an error, and before this commit the PR gave 0. That input isn't plausible. Separately, f() { $(return 3); echo $?; } prints 0 where bash prints 3. That was the same before this commit, so it's pre-existing and not related to this fix.
Evidence uploaded under strands-agents/shell/pr/217/: followup-repro.sh, followup-repro.out, followup-test.log.
|
@strandly-the-agent Please review this PR Both should-fix findings are addressed in 6f5e258: compound return status is preserved, and set applies options in order like Bash. The new regressions were checked failing before and passing after the fixes. Full Rust validation and ten Bash probes pass. Options still persist across Shell calls; session reset remains separate work. |
strandly-the-agent
left a comment
There was a problem hiding this comment.
@ferdingler Approve at 6f5e258. Both should-fixes are now addressed:
- ✅
setapplies options in order (set.rs:68). The change is the same patch I tested earlier.set -euo pipefialexits 2 with nothing printed, the same as bash. This holds inside a function too. - ✅
return Non the left of a pipe (5383e37): still returns 3.
Locally, shell_integration passes 1657/1657 tests. CI was still queued when I posted. The updated PR body is accurate.
Probes (bash 5.2 vs 5383e37 vs 6f5e258) and one non-blocking note
These all match bash at 6f5e258:
set -euo pipefial; echo unreachableexits 2 with no output. Before this commit it printedunreachable.set -eo bogusexits 2.set -o pipefail +o bogus; false|truegives 1, andset +o pipefail -o bogus(with pipefail on) gives 0.set -ux -o bogusleavesuandxon.set -e; set -o bogusexits 2.set -o nounset bogussets$1=bogus.set -e -oapplies-e, then prints the option list.f() { set -eo bogus; echo in-f; }; fexits 2.
⚪ One place this goes further than bash, not blocking: bash rejects an unknown single-letter option before it applies any of the cluster. Only -o NAME options are applied one at a time. So in bash, set -eZ leaves -e off and the script keeps going, while here it now turns -e on and exits 2. The same goes for set -uZ, which leaves u on here. This only matters for a letter bash itself rejects, and it errs on the strict side, so I'd leave it alone. The catch is a letter that bash accepts but Shell doesn't, like set -euE: bash runs it fine, but here it now exits 2.
Evidence uploaded under strands-agents/shell/pr/217/: followup2-repro.sh, followup2-repro.out, followup2-test.log.
|
Both fixes are now in. The unknown-letter cluster behavior also matches current main: its parser applies |
Description
Agent scripts often start with
set -euo pipefail, but Shell rejected-oafter partially enabling the earlier flags. That stopped the script before its first useful command.set -o NAMEandset +o NAMEnow support errexit, nounset, xtrace, and pipefail, including clustered flags and option listings. Pipefail returns the rightmost failure, including compound commands,return, and deferred host-write errors; with pipefail off, an empty or assignment-only final stage now correctly returns success. Options are applied in order, like Bash, so a later invalid option keeps earlier changes and an enabled-estops the script.The full Rust suite passes, and Bash comparisons cover invalid options, ordinary and compound pipelines, subshells, negation, conditions, and host-write failures. The new invalid-option and compound-return tests fail without their fixes. Options retain their existing session lifetime, assignment-only command substitution still needs #232, and positional arguments after options (
set -o pipefail -- a b) remain a separate gap.Related Issues
Part of #207
Documentation PR
Type of Change
New feature
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.