Repository navigation
WIP: fix streamed function bodies being alias-expanded a second time at the call (#2511) - #2513
Conversation
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
0e71531 to
0cff181
Compare
|
[win-acceptance] #2513 PASS — pin Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge). No BLOCKER. |
Refs #2511
Root cause
GNU expands aliases only while reading:
parse.yread_token_word→alias_expand_token(parse.y:5758-5765).execute_cmd.c:5181execute_functioncopies 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_STREAMEDmarker, which describes the caller's input stream, not the body being executed:.driver always reads group-by-group and alias-expands each group, so functions defined in sourced files are registered streamed (body already expanded).expand_aliasestakes the whole-file fast path (src/main.rsdispatch viascript_uses_aliases), whose top-level commands run with no marker.execute_functiononly 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 bakelet --into stored bodies (declare -f whichis byte-identical in both shells), and the call executedlet -- -- 1→which.mm:125: let: --: arithmetic syntax error: operand expected (error token is "-")→ modernishdieengaged → the die-child re-source produced the spuriousreadonly functioncascade. 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 existingmark_alias_streamed/resume_alias_streamedpair. No modernish-specific handling.Verification
let: --:error / post-fix rc=0 silent — all three shapes.tests/issue2511_modernish_readonly_regressions.rs: 3/3 (scrubbed cargo harness).use sys/base/which→which -s ls): post-fix output identical to GNU 5.3.20 —rc=0 REPLY=/usr/bin/ls, zeroreadonly functionlines. Init-only repro clean.issue2227_bracket_quote_regressions5p/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
--testsuite under standalone rubash now dies earlier at an unrelated, pre-existing divergence (identical pre/post fix):command -v sedinside$(PATH=$DEFPATH; command -v sed)returns the bare namesedinstead of GNU's resolved/usr/bin/sed, sosys/base/revreportsCan't find a functioning 'sed'. Minimal shape:rubash -c 'command -v sed'→sed; GNU →/usr/bin/sed(notecommand -v lsresolves correctly in both). Left for a separate filing.