Repository navigation
fix(shell): a command that fails fails only itself; set -e works - #243
Merged
hyperpolymath merged 3 commits intoNov 18, 2026
Merged
Conversation
A call to a shell function pushes a frame: the caller's positional parameters, and the old value of each variable the body declares `local`. `execute_function_call` popped the frame after the body. A command in the body that returned an error left through `?` inside the body loop, before the pop. The caller then kept the function's `local` values and had the function's arguments as its own `$1`, `$2`, ... and `$#`. The call depth stayed raised as well, so `local` and `return` were accepted outside any function. A nested call left one frame behind for each function the error passed through. The body now runs in a function of its own, `run_function_body`. Its result, whichever it is, comes back to one place in `execute_function_call`, which pops the frame and then returns the result. The error still goes to the caller. The body loop and the pop are otherwise unchanged. This fault was noted in the review of #233 and left out of it. Where the fault shows. At the prompt: the interactive loop reports an error and reads the next line with the same state. It does not show with `-c` or a script file on `main`: there the first error ends the whole run, before a later command can show what the caller kept. That is a separate fault and is not touched here. So this line from the review f() { local x=inner; mkdir /no/such/parent/dir; }; x=outer; f; echo "$x" given to `-c` prints the error and exits 1 before and after this change, where bash 5.2.21 prints `outer`. With the function in a sourced file, the same commands at the prompt printed `inner` before this change. They print `outer` now, as bash does. Tests. Four in the new tests/function_frame_tests.rs run the built shell at its prompt through a pseudo-terminal: a `local` variable, the positional parameters, a nested call, and `local` and `return` at the prompt after a function failed. The `after:` lines they expect are what bash 5.2.21 prints for the same lines. They drive both prompts (the line editor of the default build and the plain prompt of --no-default-features) and are built on Linux only. One test in function_and_script_tests.rs calls the library and runs on every platform. All five fail on the code before this change, with both builds, and pass after it. For the pseudo-terminal, the `nix` crate (already a dependency, same version) is also listed under dev-dependencies with its `term` feature, so the feature is on for test builds only. Cargo.lock does not change. cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean. cargo test --no-fail-fast: 832 passed, 0 failed, 15 ignored, with OLDPWD and PWD set and with both unset. Also run as the CI matrix does: --all-features (839 passed) and --no-default-features (832 passed), no failures. No proof is added or changed, and no proof covers function frames. The evidence for this change is tests only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDjYffb3HcnwwDyrsF9
…into claude/command-boundary-errexit
A built-in that failed was an error that unwound through every command around it, to the script or the prompt. So `mkdir x || echo no` never ran `echo no`, a loop stopped at its first failure, a function stopped and its caller stopped with it, and a `-c` script or script file ended at the first built-in that failed. That last one stood in for `set -e`, which was not implemented: `set -e` only stored a variable, so `set -e; false; echo after` printed `after`. Every command is now a boundary (`ExecutableCommand::execute`). A command that fails prints `vsh: ` and the reason, and gives status 1; whatever ran it goes on. `execute` no longer returns `Err` for a failed command. `set -e` is honoured. A simple command that fails ends the shell with its status, and the EXIT trap still runs. It does not apply in the condition of `if`, `elif`, `while` and `until`, or to a command of an `&&` or `||` list before its last command, nor to a function called in one of those places (`ShellState::errexit_suppressed`, opened and closed by `as_condition`). `! cmd` is still not implemented. Three statuses change, to what bash gives: - `return` outside a function: 2 (was an error, so 1). - `eval` of a line that cannot be read: 2 (was 1). - `if` whose conditions all fail and that has no `else`: 0 (was 1). POSIX gives 0, and with `set -e` a 1 there would end the shell. Measured on the built shell, with bash 5.2.21 as reference: - 56 failure and `set -e` lines: 53 match bash (13 before). The other three are older faults this does not touch: a redirection from a file that does not exist gives 127, `shift` is not a built-in, and the built-in `ls` gives 1 for a missing file where `ls` gives 2. - The #233 battery: 69 of 128 match (67 before), no case gets worse. `builtin-05` (`set -e; false; echo notreached`) and `rm-02` (`rm nosuch; echo rc=$?`) now pass, and come off known_failing.txt; the count in the test goes from 61 to 59. Tests. The new tests/command_boundary_tests.rs runs the built shell on 28 lines in four groups: a failure fails only its own command (including the line from the review of #233), the statuses above, `set -e` ends the shell, and where `set -e` does not apply. Each expected result is what bash 5.2.21 gives. All four tests fail on the code before this change. Changed tests, each because the behaviour changed as described: - function_and_script_tests.rs: `return` and `local` outside a function, `source` of a missing file and a function whose last command fails now check a status, not an error from `execute`. - function_frame_tests.rs: the failure is reported as `vsh: ...`, not `Error: ...`. - builtin_strictness_tests.rs: an EXIT trap statement that runs and fails is reported as `vsh: ...` like any failed command; one that cannot be read still says `vsh: trap:`. CLAUDE.md, README.adoc and ROADMAP.adoc: the battery count is 69, `set -e` is off the list of what does not work, and the line saying a failed built-in ends a `-c` script is gone. cargo fmt --check and cargo clippy --all-targets -- -D warnings (also with --all-features) are clean. cargo test --no-fail-fast: 889 passed, 0 failed, 15 ignored, with OLDPWD and PWD set, with both unset, and with --no-default-features. The prompt tests ran 400 times against each of the two prompts with no failure. No proof covers this; the evidence is tests only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDjYffb3HcnwwDyrsF9
|
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Stacked on #233. The base is #233's branch, so the diff also shows the commit of #240 (
0a03ecc), which is merged in here. Once #233 and #240 are merged, retarget this tomain; the only new commit is86ab3fd.A built-in that failed was an error that unwound through every command around it, to the script or the prompt:
mkdir x || echo nonever ranecho no.-cscript or a script file ended at the first built-in that failed.That last behaviour stood in for
set -e, which was not implemented.set -eonly stored a variable, soset -e; false; echo afterprintedafter.Now every command is a boundary. A command that fails prints
vsh:and the reason, and gives status 1. Whatever ran it goes on.set -eis honoured. A simple command that fails ends the shell with its status, and the EXIT trap still runs. As in bash,set -edoes not apply in these places:if,elif,whileanduntil;&&or||list before its last command;! cmdis still not implemented.The owner chose this route after a measured prototype, on the condition that it lands together with a real
set -e. Without it, scripts would lose the accidental stop at the first failure and become less safe.Changes
impl/rust-cli/src/executable.rs:ExecutableCommand::executeis the boundary. It turns a failure into status 1 and appliesset -e. The old body is nowCommand::run.as_conditionopens and closes the contexts in whichset -edoes not apply.returnoutside a function: 2.evalof a line that cannot be read: 2.ifwith no branch taken: 0, as POSIX requires. Withset -e, the old 1 would end the shell.impl/rust-cli/src/state.rs: two new fields,errexitanderrexit_suppressed.impl/rust-cli/tests/command_boundary_tests.rs(new): 28 lines run on the built shell, in four groups. Each expected result was recorded from bash 5.2.21.function_and_script_tests.rs: five checks of a status instead of an error fromexecute.function_frame_tests.rs: the failure message readsvsh: …, notError: ….builtin_strictness_tests.rs: an EXIT trap statement that runs and fails is reported asvsh: ….builtin-05andrm-02now pass and come offknown_failing.txt. The count in the test goes from 61 to 59.CLAUDE.md,README.adoc,ROADMAP.adoc:set -eis off the "does not work" list;-cscript is gone.Measured, with bash 5.2.21 as reference
set -elinesThe three lines that still differ are older faults this does not touch:
shiftis not a built-in;lsgives 1 for a missing file wherelsgives 2.dash agrees with bash on every case except the status cases noted in the test file:
returnandlocaloutside a function, andeval 'if', which end dash.RSR Quality Checklist
Required
just testor equivalent):cargo test --no-fail-fastinimpl/rust-cli/: 889 passed, 0 failed, 15 ignored. That holds withOLDPWDandPWDset, with both unset, and with--no-default-features.just testitself was not run.just fmtor equivalent):cargo fmt --check.cargo clippy --all-targets -- -D warnings, and the same with--all-features.unsafeblocks without// SAFETY:comments (none added)..envfiles included.Testing
Limits
executekeeps itsResulttype but no longer returnsErrfor a command that fails. A program using the library sees a status instead, and the message goes to standard error.whileanduntilnow fails only the loop, with status 1, where before it ended the script.Generated by Claude Code