Skip to content

WIP: fix streamed function bodies being alias-expanded a second time at the call (#2511) - #2513

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2511-modernish-readonly
Nov 1, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2511-modernish-readonly

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2511

Root cause

GNU expands aliases only while reading: parse.y read_token_word → alias_expand_token (parse.y:5758-5765). execute_cmd.c:5181 execute_function copies the stored body tree (copy_command(function_cell(var))) and executes it verbatim — a definition-time expansion is never re-applied at the call.

rubash instead gated executor-level alias expansion on the ambient __RUBASH_ALIAS_STREAMED marker, which describes the caller's input stream, not the body being executed:

  • The . driver always reads group-by-group and alias-expands each group, so functions defined in sourced files are registered streamed (body already expanded).
  • A main script whose text never mentions expand_aliases takes the whole-file fast path (src/main.rs dispatch via script_uses_aliases), whose top-level commands run with no marker.
  • execute_function only protected the opposite direction (UNSTREAMED bodies lift the marker). A streamed body called from an unmarked context was expanded a second time.

modernish sets alias let='let --' (bin/modernish:1617). Modules sourced after init bake let -- into stored bodies (declare -f which is byte-identical in both shells), and the call executed let -- -- 1 → which.mm:125: let: --: arithmetic syntax error: operand expected (error token is "-") → modernish die engaged → the die-child re-source produced the spurious readonly function cascade. Faces 1 and 2 of #2511 are that cascade; face 3 is the mechanism.

Fix

src/executor/function_calls.rs (execute_function): the body's own read-time state now governs its execution. UNSTREAMED bodies still lift the marker (lazy expansion, unchanged); streamed bodies pin the marker for the body's duration, and the caller's previous marker value is restored on return via the existing mark_alias_streamed/resume_alias_streamed pair. No modernish-specific handling.

Verification

  • Minimal divergence (pin shapes): GNU rc=0 silent / pre-fix exact let: --: error / post-fix rc=0 silent — all three shapes.
  • New pin tests/issue2511_modernish_readonly_regressions.rs: 3/3 (scrubbed cargo harness).
  • Full modernish chain (source init → use sys/base/which → which -s ls): post-fix output identical to GNU 5.3.20 — rc=0 REPLY=/usr/bin/ls, zero readonly function lines. Init-only repro clean.
  • No regressions (scrubbed): issue2212 15/15, issue2217 8/8, issue2374 1/1, issue2375 1/1, issue2441 6/6, issue2227 extraction 1/1, issue201 2/2, issue2033 3/3, issue1787 9/9. issue2227_bracket_quote_regressions 5p/1f — the 1 red is the known wrong pin already corrected on PR WIP: $[...] verdict names the double-quoted segment (rubash#2227 face 1) #2477's branch; identical on the unmodified base.

Adjacent finding (not fixed here)

The modernish --test suite under standalone rubash now dies earlier at an unrelated, pre-existing divergence (identical pre/post fix): command -v sed inside $(PATH=$DEFPATH; command -v sed) returns the bare name sed instead of GNU's resolved /usr/bin/sed, so sys/base/rev reports Can't find a functioning 'sed'. Minimal shape: rubash -c 'command -v sed' → sed; GNU → /usr/bin/sed (note command -v ls resolves correctly in both). Left for a separate filing.

GNU expands aliases only while reading (parse.y read_token_word ->
alias_expand_token); execute_cmd.c:5181 execute_function copies the
stored body tree and executes it verbatim, so a definition-time alias
expansion is never re-applied at the call.

rubash gated executor-level alias expansion on the ambient
__RUBASH_ALIAS_STREAMED marker, which describes the CALLER's input
stream, not the body being executed. A body registered as streamed
(already expanded by the reader that produced it, e.g. any sourced
file) called from an unmarked context -- a main script whose text
never mentions expand_aliases takes the whole-file fast path and runs
with no per-group marker -- was expanded a second time. modernish
sets alias let='let --'; modules sourced after init bake 'let --'
into their bodies at definition, and the call then ran 'let -- -- 1',
dying at which.mm:125 with 'let: --: arithmetic syntax error' and
engaging modernish's die path, whose re-source produced the spurious
'readonly function' cascade (#2511 faces 1-3).

execute_function now makes the body's own read-time state govern:
UNSTREAMED bodies still lift the marker for lazy expansion; streamed
bodies pin it for the body's duration, with the caller's previous
marker value restored on return.

Regression pins: tests/issue2511_modernish_readonly_regressions.rs
(3 shapes: batch caller, nested streamed calls, post-definition alias
redefinition). Verified against GNU bash 5.3.20; modernish init +
use sys/base/which + which -s ls now matches GNU byte-for-byte
(rc=0, REPLY=/usr/bin/ls, no readonly diagnostics).

Refs #2511
@caomengxuan666
caomengxuan666 force-pushed the fix/2511-modernish-readonly branch from 0e71531 to 0cff181 Compare November 1, 2026 20:06
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2513 PASS — pin issue2511_modernish_readonly_regressions is #![cfg(unix)] (0 tests on Windows by design); the mechanism is live-verified on Windows with a minimal clean-main probe: setup + fn both SOURCED (. ./setup2511.sh arms alias let='let --', . ./inner2511.sh defines fn_2511(){ let "$#"; }), main script text never mentions expand_aliases (whole-file fast path), then set -- -s; fn_2511 -s. Result: pre-fix tree (d529b35, built this session) errors ./inner2511.sh: line 1: let: --: arithmetic syntax error: operand expected (error token is "-") rc=1; master 9348b8d prints rc=0 silently — identical to WSL GNU Bash 5.3.20. Probes: D:/repo/win-accept/wave2/probes/p2511b.sh (+setup2511.sh/inner2511.sh).

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge). No BLOCKER.

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