Repository navigation
fix: run command, eval, and similar builtins as pipeline stages - #357
Conversation
…builtins failing as pipeline stages
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. The pipeline fix works, but it also changes single-command lines: forms that failed with 127 on main now run in a discarded subshell and return 0.
- 🔴
run() { "$@"; }; run source ./env.sh,command eval 'x=1'andcommand exit 3silently do nothing. Applying the three new checks only whenlen > 1restores main's behaviour and keeps the new tests green. - 🟡 A
command NAMEstage buffers its output: over 1 MiB it emits nothing, and non-UTF-8 bytes are replaced. - 🟡 With a function named like one of these builtins, the builtin runs with the wrong positional parameters (one-line fix).
- 🟡 Two mutations of the new code pass every suite; two more case lines catch them.
The policy side holds: with a refusing kernel or fallback, no refused program ran through a command, eval, exec or . stage in about 175 scripts, and each program was checked once.
What I verified, appendix
Reviewed d15f3aa against dbb6189, on aarch64 Linux, debug builds. bash is 5.2.15.
Verified
cargo test --workspace --all-targetson d15f3aa: exit 0, 2735 passed, 0 failed, 0 ignored.cargo fmt --check: exit 0.cargo clippy --workspace --all-targets: exit 0, no warnings on added lines.- The three new tests fail on main. With the PR's test files copied onto dbb6189,
command_checkis 31 passed, 2 failed, anddifferentialfails only onexecutor_builtins_in_pipeline. executor_builtins_in_pipeline.expectedequals bash's output for the.sh.- Env prefixes, redirects and heredocs on such a stage match bash, and so does the Claude Code wrapper shape from #323 (
grep() { command grep "$@"; }; echo hi | grep -c hi). - Nothing leaks to the parent shell from
exit,eval 'x=1',cd,export,.,break 2in a stage. Already-expanded words are not expanded again. - Recursion through
evaland.stages stops at "maximum recursion depth exceeded" without aborting. - 12 literal single-command forms and the 124 differential cases on main give identical output on both revisions.
Not tested: Python, Node and WASM bindings; platforms other than Linux.
Reading order: src/exec.rs 3834, 3877, 4208-4224, then 4289-4315.
Appendix, non-blocking (4)
echo | breakandecho | continueleave$?at the previous status; bash gives 0.false; for i in 1; do echo | break; echo "st=$?"; doneprintsst=1(Err(_) => state_proc.last_exit,src/exec.rs:4311).set -x; echo hi | eval catprints+ eval cattwice; bash prints it once.run_pipelinetraces the stage and theevalarm traces it again.- Pre-existing, not filed: a function stage on main also drops all of its output over 1 MiB (
f() { cat; }; cat a a a | f | wc -cprints0). It is the mechanism behind the second finding. I can open an issue if you want one. - Pre-existing:
echo hi | builtin eval catfails with "not a shell builtin" on both revisions.
…ction precedence, streaming, check timing
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
Approved at d10dd81. All four findings from my last review are fixed; I re-ran each repro against this commit.
| Finding | Status |
|---|---|
| 🔴 Single-command lines ran in a discarded subshell | Fixed. The five scripts print what main prints. |
🟡 command NAME stage buffered its output |
Fixed. 1.2 MB and 5 MB pass through intact, bytes are kept, and a refusal stops the pipeline before any stage runs. |
| 🟡 Function named like one of these builtins | Fixed. The function runs, as on main and in bash. |
| 🟡 Unpinned behaviours | Fixed, with one follow-on from the unwrap (inline). |
Two things remain, neither blocking:
- 🟡 Two parts of the new code are not pinned by a test; two case lines cover them (inline).
- Nested forms still hand the fallback the wrapper word, as on main. With
toolrefused,echo | exec command tool aasks the fallback aboutcommandand calls it, and so do the single linescommand command tool aandx=command; $x tool a. Having thecommandandexecarms re-enterexecute_pipeline_checkedwould close these; a follow-up is fine.
I filed #382 for the function-stage buffering, as you asked.
What I verified, appendix
Reviewed d10dd81 against d15f3aa (previous review) and dbb6189 (main), on aarch64 Linux, debug builds. bash is 5.2.15.
Verified
cargo test --workspace --all-targetson d10dd81: exit 0, 2738 passed, 0 failed, 0 ignored.cargo fmt --check: exit 0.cargo clippy --workspace --all-targets: exit 0.- The new tests fail on d15f3aa. With this commit's
tests/on d15f3aa'ssrc/:a_refused_command_stage_stops_the_pipeline_before_any_stage_runsfails,differentialfails oncommand_stage_streamsandexecutor_builtins_in_pipeline, and both..._does_not_silently_discard_its_effecttests fail. - Both case files'
.expectedequal bash's output. - Probes on the unwrap, 14 groups. 11 match bash, apart from the shell's name in error messages:
command command cat,command eval/exit/.in a stage,command read/cd/export, a name from an expansion, empty and missing names, a function NAME aftercommand, env prefixes and redirects, statuses withpipefailand!, three-stage pipelines with$(...)and background, skipping the function only in the stage that sayscommand, andcommand sh -c. - The other 3 differ from bash.
command -v catprintscatwhere bash prints a path, andcommand -V evalis worded differently.set -x; echo hi | command catprints the two stages' trace lines in the other order. Both are the same on d15f3aa apart from its extra+ cattrace line. The third is the function namedcommand, in the appendix. - Policy: I re-ran the 176 embedder scripts from the first review. In none did a refused program run or finish under its own name. 93 scripts differ from d15f3aa:
- 42 are refusals that now stop the pipeline up front: the other stages get
command_droppedor are never checked. - 31 single-command forms give main's result again.
- 14 change only the order of the check events.
- 2 cancelled
command sleepstages now getcommand_dropped; on d15f3aa they got neither notification. - 1 failed redirect now checks the
commandstage before failing. - 3 are the nested forms above (
echo | exec command tool a,echo | exec exec tool a,echo | eval exec command tool a), which d15f3aa refused. On main the fallback was asked aboutexecorevalfor these.
- 42 are refusals that now stop the pipeline up front: the other stages get
Not tested: Python, Node and WASM bindings; platforms other than Linux.
Appendix, non-blocking (1)
- With a function named
command,command() { echo fn "$@"; }; echo hi | command catprintshi; main and bash printfn cat. The word is dropped before the function lookup. A single-command line already runs the builtin on main.
Description
When
command,eval,exec,source/.,exit,return,breakorcontinueran in a pipeline with more than one stage, the shell didn't recognise them and reported "command not found". Soecho hi | command grep -c hifailed, and so didcommand echo x | cat. That breaks Claude Code's grep and find wrappers when they fall back tocommand grep. In an embedder with a command fallback it was worse: the shell handed the fallback a program literally namedcommand, so a policy that refused the real program was never asked about it.Now those stages run the same way they do when they're alone on the line, in the stage's own subshell, and the kernel and the fallback are asked about the program that
commandactually runs. Nothing changes for single-command lines.Related Issues
Part of #323
Documentation PR
None.
Type of Change
Bug fix
Testing
I added a differential case for these builtins in pipelines and two tests that check what the fallback is asked about. On main all three failed. With the change they pass, and so does the full test suite.
This doesn't cover
exec -aortypesetfrom the same issue, which stay open there.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.