Skip to content

WIP: fix #2486 — heredoc $ escape under set -u (nounset scan input) + bg-subshell assoc element assignment (respawn attribute transport) - #2491

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2486-setu-heredoc-assoc
Nov 2, 2026

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: two independent set -u faces of #2486. Fixes verified against WSL GNU bash 5.3.20 script files; not ready to merge until reviewed.

Refs #2486

Face A — unquoted heredoc \$ under set -u (commit 5f8e3b8)

GNU anchor

  • redir.c:348 heredoc_expand() — an unquoted heredoc body goes through expand_string_to_string (redirectee->word, Q_HERE_DOCUMENT) (redir.c:374).
  • subst.c:11229 expand_word_internal(), backslash branch subst.c:11614-11661: \x is consumed atomically BEFORE any $ dispatch. Under Q_HERE_DOCUMENT the escape class is CBSHDOC = \ $ (</code>syntax.h:27 slashify_in_here_document<code>, </code>syntax.h:59<code>; tflag selection </code>subst.c:11628-11629<code>), and the escaped $/`` `` become CTLESC-protected data (subst.c:11659-11661) — parameter expansion, and with it the nounset verdict, never runs on them.

Bisect conclusion

set +u was already byte-correct (literal ${0%/*} / $PREFIX printed), so the heredoc escape layer was NOT wrong; only the unbound check saw the escaped $. Root cause: parameter_expansion_error_in_heredoc_body scanned the RAW body, while the real expansion path (expand_heredoc_body_mut) scans prepare_unquoted_heredoc_expansion output where \$ is already DATA_DOLLAR (which the nounset scan skips). Verdict and expansion disagreed by construction.

Change

src/executor/parameter_errors.rs — run the heredoc error scan on the same prepared text (prepare_unquoted_heredoc_expansion, made pub(in crate::executor) in src/executor/command_input_scope.rs). One owner of the whole escape class (\$→DATA_DOLLAR, \``→DATA_BACKTICK, \→DATA_BACKSLASH): \$setstill expands (and can still be unbound),$` never opens an expansion. No per-symptom guard.

Face B — bg subshell assoc element assignment under set -u (commit 22d78a5)

GNU anchor

  • execute_cmd.c:1761-1763 execute_in_subshell — the async child is a fork; it inherits the variable-attribute table whole.
  • arrayfunc.c:392-408 assign_array_element_internal — for an ASSOC array the subscript is word-expanded (expand_subscript_string, arrayfunc.c:396 / subst.c:11063) and used literally as the key (bind_assoc_variable, arrayfunc.c:408); the expanded key is never re-read as a variable reference, so nounset can never fire on it.
  • Contrast: arrayfunc.c:425 array_expand_index — INDEXED subscripts are arithmetic; a bare unset name there IS fatal under set -u (expr.c). Both shells agree on that shape; it is pinned, not silenced.

Bisect conclusion

Top-level OK, plain ( ... ) OK, $( ... ) OK — only ( ... ) & children failed, and even a literal building[appx]=1 with no $ misfired. declare -p building inside the bg child printed declare -x building="()" (plain string!) while a plain subshell printed declare -A building=(). Root cause: the ( ... ) & body is a respawned rubash -c child whose env transport iterated the write-behind VarTable directly, so the __RUBASH_*_VARS attribute markers were never materialized; the child treated building as INDEXED, the subscript took the arithmetic path, and app raised nounset.

Change

All four respawn-env assembly sites now iterate env_vars_snapshot() — the sanctioned renderer that materializes every attribute key from the structured flags (public_accessors.rs:1437, "child-env bootstrapping reads them"):

  • src/executor/compound_exec.rs — cmd & wrapper + coproc respawn
  • src/executor/external_setup.rs — respawn transport
  • src/executor/trap_exec.rs — input procsub child

Post-fix the bg child prints declare -Ax building=(); the residual -x (every transported shell var rides as an env pair, so the child sees it as exported) is a pre-existing respawn-transport artifact affecting ALL unexported vars, out of #2486's scope.

Verification

Oracle: MSYS_NO_PATHCONV=1 wsl bash <script file> (GNU bash 5.3.20, /usr/local/bin/bash), stdout/stderr captured separately; base e6ccbf14.

  • Both issue repro scripts byte-identical to the WSL oracle (stdout AND stderr, rc).
  • Class matrices (A: \$/\${x}/\\\$/\\$set/plain $set/quoted-delimiter × set +u/set -u; B: top/plain-subshell/bg × fn-call/literal-subscript, indexed-arith contrast) byte-identical to WSL (path-prefix in stderr aside, which is environmental).
  • New pins: tests/issue2486_heredoc_escaped_dollar.rs (4), tests/issue2486_assoc_assign_subshell_nounset.rs (5) — all green.
  • Existing families re-run, no regressions: heredoc_regressions, issue1130/1149-50/1168-69/1177/1930/200 nounset families, assoc/array families (array_store_parity, 1297/1298/1311/1677/1710/1714/1556), bg/coproc/procsub families (1074/1408/1370/1021/1220/1223/1336/1460), issue2486 pins. Suites that fail on clean master fail identically here (environment-bound: issue1915 CRLF \r\n, issue1981, issue1153 cp resolution, issue2134, issue1219, issue1930 tempdir-path prefix) — verified by stash-and-run on the clean tree.
  • cargo test --lib: 1124 passed / 2 failed — identical counts to clean master (pre-existing, path-shape sensitive).

Disclosure: rustfmt / pre-commit hook

cargo fmt fails with os error 206 (filename too long) on this machine — including on the pristine main checkout, so the pre-commit hook (which shells out to cargo fmt --check) cannot pass machine-wide and commits were made with --no-verify. Substituted check: per-file rustfmt --edition 2021 --check over every tracked crate file — my edited regions are clean. Two pre-existing drift spots on lines I do not touch (compound_exec.rs:4065, trap_exec.rs:387) come from upstream e6ccbf14 and were left alone.

Process note

During verification, a sibling session's git stash pop in another worktree consumed this branch's uncommitted face-B stash (stashes are shared across worktrees in this repo). Face B was re-applied from known diffs, re-verified byte-for-byte against the WSL oracle, and committed; the foreign WIP that landed in this worktree was reverted without pushing anywhere.

Face A of rubash#2486: under set -u an unquoted heredoc body's $ was
reported as an unbound reference (\$PREFIX -> 'PREFIX: unbound
variable') and killed the script, while set +u printed the correct
literal text.

GNU: redir.c:348 heredoc_expand() expands an unquoted body with
expand_string_to_string(word, Q_HERE_DOCUMENT) (redir.c:374). In
expand_word_internal (subst.c:11229) the backslash branch
(subst.c:11614-11661) consumes the escape atomically BEFORE any $
dispatch: under Q_HERE_DOCUMENT the escape class is CBSHDOC =
backslash,backtick,dollar (syntax.h:27 slashify_in_here_document,
syntax.h:59; tflag at subst.c:11628-11629) and the escaped $/` become
CTLESC-protected data (subst.c:11659-11661) - parameter expansion, and
with it the nounset verdict, never runs on them.

Root cause: parameter_expansion_error_in_heredoc_body scanned the RAW
body, but the real expansion path (expand_heredoc_body_mut) scans
prepare_unquoted_heredoc_expansion output where $ is already
DATA_DOLLAR (which the nounset scan skips). The verdict and the
expansion disagreed by construction.

Fix: run the heredoc error scan on the same prepared text (one owner of
the escape class: $ -> DATA_DOLLAR, ` -> DATA_BACKTICK, \ ->
DATA_BACKSLASH), so a collapsed backslash pair before $set still
expands (and can still be unbound) while $ never opens an expansion.
No per-symptom guard.

Pins: tests/issue2486_heredoc_escaped_dollar.rs (installer shape,
escape class x set +u/-u, quoted delimiter, real-unbound still fires).
Verified against WSL GNU bash 5.3.20 script files byte-for-byte.

Note: committed with --no-verify because 'cargo fmt' fails with os
error 206 on this machine (fails in the pristine main checkout too);
the hook's check was substituted by per-file 'rustfmt --edition 2021
--check' over every tracked crate file - zero diffs.
Face B of rubash#2486: under set -u, an associative-array element
assignment inside a BACKGROUND subshell reported the subscript as an
unbound variable (building[$1]=1 with $1=app -> 'app: unbound
variable'), while the same call at top level and in a plain ( ... )
subshell worked. Even a literal building[appx]=1 with no $ misfired.

GNU: the async child is a fork (execute_cmd.c:1761-1763
execute_in_subshell) and inherits the attribute table whole. For an
assoc array the subscript of name[sub]=value is WORD-expanded
(arrayfunc.c:392-408 assign_array_element_internal ->
expand_subscript_string at arrayfunc.c:396 / subst.c:11063) and used
literally as the key (arrayfunc.c:408 bind_assoc_variable) - the
expanded key is never re-read as a variable reference, so nounset can
never fire on it. Only INDEXED arrays evaluate the subscript
arithmetically (arrayfunc.c:425 array_expand_index -> evalexp), where a
bare unset name under set -u IS fatal - that contrast behavior is kept
and pinned.

Root cause: the ( ... ) & body is a respawned 'rubash -c' child, and
its env transport iterated the write-behind VarTable directly
(#916), so the __RUBASH_*_VARS attribute markers were never
materialized: 'declare -A building=()' crossed as a plain string, the
child treated building as INDEXED, the subscript took the arithmetic
path, and 'app' raised nounset.

Fix: all four respawn-env assembly sites (cmd & wrapper, coproc,
external_setup respawn, input procsub child) now iterate
env_vars_snapshot(), the sanctioned renderer that materializes every
attribute key from the structured flags (public_accessors.rs:1437,
'child-env bootstrapping reads them').

Bisect evidence: top-level OK, plain subshell OK, only '&' children
failed; declare -p inside the bg child showed 'declare -x
building="()"' pre-fix and 'declare -Ax building=()' post-fix. The
residual -x (every transported shell var rides as an env pair) is a
pre-existing respawn-transport artifact for all unexported vars, out
of scope here.

Pins: tests/issue2486_assoc_assign_subshell_nounset.rs (worker shape x
top/plain-subshell/bg, literal subscript, bg declare -A, set +u
control, indexed-arith contrast). Repro + matrix verified against WSL
GNU bash 5.3.20 script files byte-for-byte.

Note: committed with --no-verify because 'cargo fmt' fails with os
error 206 on this machine (pristine main checkout too); substituted by
per-file 'rustfmt --edition 2021 --check' - my edited regions are
clean; two pre-existing drift spots on untouched lines
(compound_exec.rs:4065, trap_exec.rs:387) come from upstream e6ccbf1
and are left alone.
@caomengxuan666
caomengxuan666 force-pushed the fix/2486-setu-heredoc-assoc branch from 22d78a5 to 86c15c4 Compare November 2, 2026 00:41
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 00:41
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2491 PASS — both new pins green at --test-threads=4 (heredoc_escaped_dollar 4/4, assoc_assign_subshell_nounset 5/5 x2); both issue #2486 repro scripts (t8 evidence originals) byte-identical to WSL GNU Bash 5.3.20 — face A stdout/stderr 0 diff, rc 0; face B stdout/stderr/log1.txt side file 0 diff, rc 0.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 2506da1 (includes this merge, parent step d269fb0), GNU oracle = wsl bash 5.3.20 script-file comparison. Probes: D:/repo/win-accept/probes/p2486a.sh / p2486b.sh (copies of D:/repo/nbbuild/evidence/pre-1.5.0/t8/repro/ originals).

Family battery at -j4 (19 binaries): heredoc_regressions 3/3, nounset families 1130 4/4, 1149-50 2/2, 1168-69 2/2, 1177 3/3, 200 2/2, array_store_parity 2/2, assoc families 1297 1/1, 1298 1/1, 1311 2/2, 1677 12/12, bg/coproc/procsub families 1074 4/4, 1408 3/3, 1220 2/2, 1223 3/3, 1336 5/5, 1460 1/1, 1370 4/4 — all green. issue1930 has 1 red of 3 = the tempdir-path-prefix environment shape this PR's body already documented as pre-existing on clean master (/c/Users/.../case.sh: line 2: NOPE: unbound variable vs SH: placeholder), not a regression.

The bg-subshell respawn attribute transport (face B) is a Windows cfg-face fix and it is verified end-to-end here: the ( ... ) & child completes building[$1]=1 under set -u with GNU-identical output.

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