Skip to content

fix(interpreter): ERR/RETURN trap scoping and N>&word redirects like bash - #2631

Merged
chaliy merged 5 commits into
mainfrom
claude/project-thread-eafupr-bb-builtins
Oct 8, 2026
Merged

chaliy merged 5 commits into
mainfrom
claude/project-thread-eafupr-bb-builtins

Conversation

@chaliy

@chaliy chaliy commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Requested by Михайло · project thread

What changed

  • ERR trap scoping follows bash. ERR fires right after the failing command (simple commands, function calls, eval, multi-command pipelines, ( ), [[ ]], (( ))); lists and compound commands no longer fire it a second time. Functions, subshells, $( ) and pipeline stages don't inherit ERR unless set -E is on; a trap set inside a function stays set; a function that sets ERR doesn't fire it for its own call. $LINENO in the handler is the failing line.
  • RETURN trap runs. It fires when a function or sourced file finishes, keeps $?, and is inherited by functions only under set -T.
  • N>&word redirects. 2>&- closes stderr for that command (it used to act like 2>&1), >&file / 1>&file mean &>file, 2>&file is an ambiguous redirect, and 2>&$fd duplicates the fd instead of writing a file named after it. A failed &> open no longer prints the command's output.
  • for ((...)) re-runs $(...) in its init, condition and step.

Why

BashBox corpus batch: all 17 skipped cases in bashbox-interpreter-builtins.test.sh and all 10 in bashbox-err-trap.test.sh now pass (10 of the 27 were already fixed by earlier batches and only lost their skip lines).

Before / After

trap "echo err" ERR; f(){ false; echo in; }; f; x=$(false; echo sub); echo "[$x]"; ls /nope 2>&-; { echo z; echo e >&2; } >& both.txt; cat both.txt
Output
Before err in / [err sub] / ls: cannot access '/nope': ... / z (stderr lost)
After (= bash 5.2) in / [sub] / err / z / e

set -T; trap 'echo r' RETURN; g(){ :; }; f(){ g; }; f; echo end printed only end; it now prints r r end, like bash.

Risk

  • Medium: ERR firing moved from list/script level into execute_command; covered by 9 new trap-scope cases plus the corpus, all recorded from bash 5.2. New handling sits in boxed helpers so the recursion stack budget (function_recursion_is_bounded_on_a_two_mib_stack) is unchanged.
  • Known gaps (in limitations.md): a write error after a command's own >&- doesn't name the command; the DEBUG trap still runs in subshells without set -T.

Checklist

  • Tests added or updated
  • Backward compatibility considered

Generated by Claude Code

chaliy added 3 commits October 8, 2026 03:25
ERR fires after the failing simple command, multi-command pipeline,
( ), [[ ]] or (( )) itself, only when a handler was set before the
command ran. Functions inherit ERR only under set -E and RETURN only
under set -T; subshells, command substitutions and pipeline stages keep
ERR listed but dormant without set -E. RETURN now runs when a function
or sourced file finishes, and trap handlers count $LINENO from the
triggering line.
N>&- closes the descriptor for the command (stderr vanishes, stdout
writes fail), >&file and 1>&file send both streams to the file like
&>file, 2>&file is an ambiguous redirect, and 2>&$fd duplicates the
expanded descriptor instead of writing a file named after it. A failed
&>/>& open drops the command's output. for (( )) headers run their
$(...) each time an expression is evaluated.
@chaliy chaliy self-assigned this Oct 8, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 924a4b0 Commit Preview URL

Branch Preview URL
Oct 08 2026, 04:01 AM

chaliy added 2 commits October 8, 2026 03:50
…-eafupr-bb-builtins

# Conflicts:
#	crates/bashkit/src/interpreter/mod.rs
#	knowledge/operations/limitations.md
…-eafupr-bb-builtins

# Conflicts:
#	crates/bashkit/src/interpreter/mod.rs
@chaliy
chaliy merged commit ba993c1 into main Oct 8, 2026
46 checks passed
@chaliy
chaliy deleted the claude/project-thread-eafupr-bb-builtins branch October 8, 2026 04:22
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.

1 participant