Skip to content

fix(interpreter): check errexit_suppressed in execute_script_body#884

Merged
chaliy merged 3 commits intomainfrom
fix/issue-873-errexit-and-or-chain
Mar 30, 2026
Merged

fix(interpreter): check errexit_suppressed in execute_script_body#884
chaliy merged 3 commits intomainfrom
fix/issue-873-errexit-and-or-chain

Conversation

@chaliy
Copy link
Copy Markdown
Contributor

@chaliy chaliy commented Mar 27, 2026

Summary

  • set -e incorrectly exited when a for/while/until loop body ended with [[ test ]] && cmd returning non-zero
  • Check result.errexit_suppressed in execute_script_body for both errexit and ERR trap suppression
  • Add test set_e_and_chain_at_end_of_for_body reproducing the exact issue scenario

Test plan

  • New test set_e_and_chain_at_end_of_for_body passes
  • All 10 set -e tests pass (including negative tests)
  • Full cargo test --all-features passes
  • cargo fmt --check and cargo clippy clean

Closes #873

set -e incorrectly exited when a for/while/until loop body ended with
an AND-OR chain (e.g. [[ test ]] && cmd) that returned non-zero.

Check result.errexit_suppressed in execute_script_body to suppress
errexit for compound commands whose non-zero exit came from && or ||.

Closes #873
@chaliy chaliy force-pushed the fix/issue-873-errexit-and-or-chain branch from 4039d7a to 97078b6 Compare March 28, 2026 00:10
@chaliy chaliy requested a review from Copilot March 30, 2026 16:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds regression coverage for issue #873 (errexit incorrectly triggering when a loop body ends with an && chain failure) and updates interpreter inline documentation around errexit suppression behavior.

Changes:

  • Add a new set -e regression test covering an && chain failure at the end of a for loop body.
  • Update a comment in execute_script_body explaining errexit_suppressed propagation from compound commands.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/bashkit/tests/set_e_and_or_tests.rs Adds a regression test for issue #873 using a for loop whose body ends with an && chain.
crates/bashkit/src/interpreter/mod.rs Adjusts documentation comment describing errexit_suppressed propagation in execute_script_body.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1152 to 1158
// errexit (set -e): stop on non-zero exit for top-level simple commands.
// List commands handle errexit internally (with && / || chain awareness).
// Negated pipelines (! cmd) explicitly handle the exit code.
// Compound commands (for/while/until) propagate errexit_suppressed when
// their body ends with an AND-OR chain failure.
// Compound commands propagate errexit_suppressed from inner AND-OR chains.
if self.is_errexit_enabled() && exit_code != 0 {
let suppressed = matches!(command, Command::List(_))
|| matches!(command, Command::Pipeline(p) if p.negated)
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says execute_script_body was updated to check result.errexit_suppressed for both errexit and ERR-trap suppression, but this hunk only changes a comment. If additional logic changes are intended here, they may be missing from the diff (or the description may need updating).

Copilot uses AI. Check for mistakes.
result.stdout.contains("result: yes"),
"should print 'result: yes' but got: {:?}",
result.stdout
);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test verifies stdout, but it would also be useful to assert exit_code == 0 (since the last command is echo). That helps catch cases where errexit is suppressed correctly but the interpreter still reports the loop’s non-zero status as the script’s final exit code.

Suggested change
);
);
assert_eq!(
result.exit_code, 0,
"script should exit successfully but exit_code was {}",
result.exit_code
);

Copilot uses AI. Check for mistakes.
@chaliy chaliy merged commit a0588db into main Mar 30, 2026
27 checks passed
@chaliy chaliy deleted the fix/issue-873-errexit-and-or-chain branch March 30, 2026 17:36
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.

set -e incorrectly triggers on compound commands whose body ends with && chain failure

2 participants