Skip to content

fix(shell): a command that fails fails only itself; set -e works - #243

Merged
hyperpolymath merged 3 commits into
claude/vsh-product-gatefrom
claude/command-boundary-errexit
Nov 18, 2026
Merged

hyperpolymath merged 3 commits into
claude/vsh-product-gatefrom
claude/command-boundary-errexit

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

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 to main; the only new commit is 86ab3fd.

A built-in that failed was an error that unwound through every command around it, to the script or the prompt:

  • mkdir x || echo no never ran echo no.
  • A loop stopped at its first failure.
  • A function stopped, and its caller stopped with it.
  • A -c script 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 -e only stored a variable, so set -e; false; echo after printed after.

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 -e is honoured. A simple command that fails ends the shell with its status, and the EXIT trap still runs. As in bash, set -e does not apply in these places:

  • the condition of if, elif, while and until;
  • a command of an && or || list before its last command;
  • a function called from either of those.

! cmd is 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::execute is the boundary. It turns a failure into status 1 and applies set -e. The old body is now Command::run.
    • as_condition opens and closes the contexts in which set -e does not apply.
    • Three statuses change to what bash gives:
      • return outside a function: 2.
      • eval of a line that cannot be read: 2.
      • an if with no branch taken: 0, as POSIX requires. With set -e, the old 1 would end the shell.
  • impl/rust-cli/src/state.rs: two new fields, errexit and errexit_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.
  • Changed tests, each because the behaviour changed:
    • function_and_script_tests.rs: five checks of a status instead of an error from execute.
    • function_frame_tests.rs: the failure message reads vsh: …, not Error: ….
    • builtin_strictness_tests.rs: an EXIT trap statement that runs and fails is reported as vsh: ….
  • The battery list: builtin-05 and rm-02 now pass and come off known_failing.txt. The count in the test goes from 61 to 59.
  • CLAUDE.md, README.adoc, ROADMAP.adoc:
    • the battery count is now 69;
    • set -e is off the "does not work" list;
    • the line saying a failed built-in ends a -c script is gone.

Measured, with bash 5.2.21 as reference

before (#233 + #240) this branch
56 failure and set -e lines 13 match bash 53 match bash
conformance battery, 128 lines 67 match 69 match; no case gets worse

The three lines that still differ are older faults this does not touch:

  • a redirection from a missing file gives 127;
  • shift is not a built-in;
  • the built-in ls gives 1 for a missing file where ls gives 2.

dash agrees with bash on every case except the status cases noted in the test file: return and local outside a function, and eval 'if', which end dash.

RSR Quality Checklist

Required

  • Tests pass (just test or equivalent): cargo test --no-fail-fast in impl/rust-cli/: 889 passed, 0 failed, 15 ignored. That holds with OLDPWD and PWD set, with both unset, and with --no-default-features. just test itself was not run.
  • Code is formatted (just fmt or equivalent): cargo fmt --check.
  • Linter is clean (no new warnings or errors): cargo clippy --all-targets -- -D warnings, and the same with --all-features.
  • No banned language patterns.
  • No unsafe blocks without // SAFETY: comments (none added).
  • No banned functions.
  • SPDX license headers present on all new/modified source files.
  • No secrets, credentials, or .env files included.

Testing

  • The four new tests fail without the change. Against the code before this commit, the same test file fails all four. 21 of its 28 lines differ from bash there.
  • The prompt tests, which drive the shell through a pseudo-terminal, were run 400 times against each of the two prompts (the line editor and the plain prompt), with no failure.
  • Not verified: macOS; the stress suite; CI on this branch.

Limits

  • execute keeps its Result type but no longer returns Err for a command that fails. A program using the library sees a status instead, and the message goes to standard error.
  • The 100,000-iteration safety limit on while and until now fails only the loop, with status 1, where before it ended the script.
  • No proof covers this. The evidence is tests only.

Generated by Claude Code

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
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
@sonarqubecloud

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review November 18, 2026 01:44
Repository owner deleted a comment from coderabbitai Bot Nov 18, 2026
@hyperpolymath
hyperpolymath merged commit ce3af2f into claude/vsh-product-gate Nov 18, 2026
13 checks passed
@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: a5a6c6cb-d3dc-4d5f-a85f-782caf82f632

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@hyperpolymath
hyperpolymath deleted the claude/command-boundary-errexit branch November 18, 2026 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants