Repository navigation
WIP: fix(parser+executor): heredoc bodies stay with their own redirect node under compounds (#2305) - #2313
Conversation
…er compounds Refs #2305 GNU model: heredoc bodies are gathered in the order the << operators were read — parse.y:3120 gather_here_documents walks redir_stack[r++] and calls make_cmd.c:512 make_here_document on each REDIRECT, so the body is stored on its own redirect node; at execution, redir.c:423 here_document_to_fd materializes that node's body. A command's own redirect list is applied over the ambient descriptor state by the single ordered walk redir.c:237 do_redirections, and scoped lists restore through execute_cmd.c:526 undo_partial_redirects. Two rubash layers broke the correlation: 1. Parse-time pairing. fill_pending_heredoc_body_recursive claimed the first still-pending slot visiting a compound node's own heredoc entries BEFORE its members — but a compound's own << is written after its closing delimiter, i.e. last in operator order, so the outer entry stole the first body and the members' bodies shifted by one (nested groups, group siblings). The walk now fills nested commands first and the node's own entries last. collect_trailing_redirections no longer fills bodies at all: when it runs, the earliest pending operator can belong to an earlier sibling subtree (two brace groups on one line), so all pairing defers to the token loop's assign_heredoc_body. 2. In-process serve (read -u, mapfile -u). The serve consulted stores in a fixed per-kind order — own file first, the ambient fd table next, the command's own heredocs LAST — so an ambient same-fd binding shadowed the command's own heredoc/dup, and a mixed own list served an earlier entry of a luckier kind. A new own_input_binding resolver computes the list's effective binding (last input-side entry for the fd across Input/ReadWrite/HereDoc/HereString/DuplicateInput/ CloseInput, mirroring the ordered walk): heredoc and dup bindings serve before the ambient transports, dup resolution follows the source's binding (own list, then ambient, then coproc output), and an own close makes the fd invalid for read/mapfile even over an ambient entry (read.def:377-381 sh_validfd judges the redirected descriptor; redir.c:1128 makes a self-dup a no-op that keeps the ambient binding). Verified against GNU bash 5.3.20 with 43 script-file probes (41 byte-identical; one residual corner — position-blind dup-source resolution — recorded in the cluster REPORT) and 16 new pins in tests/issue2305_heredoc_body_correlation_regressions.rs. Controls under the scrubbed-fd runner: issue2292 18/18, issue2235 11/11, lib 985/985, regression 221 passed / 6 failed (exactly the 6 known local stock reds), adjacent fd pin targets green. Co-Authored-By: Muse Spark <[email protected]>
|
Collector adjudication of the local regression noise that held this PR: the full regression runs on the composed tree under machine load ~17.5 showed rotating failure sets (220+7, then 218+9, extras beyond the 6 known local stock reds = issue1951 count-exact + the issue1776/issue_1212 time-CPU pins). Verdict: load artifacts, not a regression of this change — (1) the suspect set rotated between runs; (2) the pin's exact shape ( |
GNU applies a command's redirect list as one ordered walk: do_redirections (redir.c:237) loops the list in order (redir.c:252 `for (temp = list; temp; temp = temp->next)`), so a dup copies its source descriptor as bound AT THE DUP'S OWN POSITION. Later entries that rebind the source cannot change what an earlier dup copied, and a cyclic pair (`3<&4 4<&3`) is not a loop — the second dup sees the first's result, the first saw the pre-list ambient state. The unixwin#2305 serve resolved a dup's source with a whole-list last-wins scan (position discarded) and served the terminal file/heredoc by rev-find, so on `read -u 3 a 3<&4 4<inner.txt` fd 3 read inner instead of fd 4's pre-list file, and the cyclic pair chased 3->4->3 until the fd was reported invalid. Thread positions through the serve instead: - own_input_binding_scan(cmd, fd, before) bounds the ordered walk to the entries before `before` and returns the winner's list index; own_input_binding keeps its signature as the position-blind wrapper for kind-only callers. - The dup chases (read/mapfile serve and the availability gate) start at the dup's own index and resolve each source against the entries before the current bound — positions strictly descend, seen-set keyed on (fd, bound). - List-sourced winners are served entry-exactly: read_redirected_fd_at / mapfile_redirected_fd_input_at bound the rev-find to the winning index; read_heredoc_fd_input_at / mapfile_heredoc_fd_input_at pick the heredoc entry by the ordinal correlation materialize_async_stdin_body uses (same-fd, same-stringness document entries before the winner). Entry cores extracted from the existing serves; the unpositioned serves are unchanged for the top-level fd and the ambient fallthrough. - redirect_in convention: it mirrors a list entry whose position is unrecoverable, so it is consulted only when the bounded scan yields no binding at all and reports position None; a dup sourced from it resolves its source against the full list, after which positions strictly descend. Probes (GNU bash 5.3.20, script files): headline, cyclic both directions, source rebound to file and to heredoc, heredoc source, two chained-dup shapes, close-after-dup all byte-match GNU; unrebound-dup and dup-of-closed-source controls unchanged. Pins: tests/issue2314_dup_source_position_regressions.rs (12). Controls green: issue2305 (16), issue2292 (18), issue2235 (11). Stacked on PR unixwin#2313 (the defect exists only on top of the unixwin#2305 serve); lands after it. Refs unixwin#2314
Refs #2305
What
Heredoc bodies now stay attached to their own redirect node under compound commands. Two layers broke the correlation:
1. Parse-time pairing (
src/parser/redirections.rs)fill_pending_heredoc_body_recursiveclaimed the first still-pending slot visiting a compound node's own heredoc entries before its members. But a compound's own<<is written after its closing delimiter — last in operator order — so the outer entry stole the first body and the members' bodies shifted by one (nested groups read OUTER for INNER; group siblings crossed). GNU gathers bodies strictly in operator order:parse.y:3120 gather_here_documentswalksredir_stack[r++]intomake_cmd.c:512 make_here_document, storing each body on its own REDIRECT node. The walk now fills nested commands first and the node's own entries last.collect_trailing_redirectionsno longer fills bodies mid-parse at all: when it ran, the earliest pending operator could belong to an earlier sibling subtree (two brace groups on one line), so its subtree-local fill stole bodies across siblings. All pairing now defers to the token loop's globalassign_heredoc_body, which sees bodies in stream order.2. In-process serve (
read -u,mapfile -u)The serve consulted stores in a fixed per-kind order — own file, ambient fd table, own heredocs last — so an ambient same-fd binding shadowed the command's own heredoc/dup, and a mixed own list served an earlier entry of a luckier kind (
3<file 3<<EOFserved the file;3<&4was unservable). GNU applies the own list over the ambient state in one ordered walk (redir.c:237 do_redirections; scoped restoreexecute_cmd.c:526 undo_partial_redirects; each body materialized byredir.c:423 here_document_to_fd). Newown_input_bindingresolver computes the list's effective binding — last input-side entry for the fd across Input/ReadWrite/HereDoc/HereString/DuplicateInput/CloseInput — and both builtins serve it first: heredocs and dups before the ambient transports, dup resolution following the source's binding (own list → ambient → coproc), an own close invalidating the fd even over an ambient entry (read.def:377-381 sh_validfdjudges the redirected descriptor;redir.c:1128self-dup is a no-op).Verification (GNU bash 5.3.20 oracle, script-file probes)
43 probes in the cluster workdir (
probes/): 41 byte-identical to GNU after the fix, incl. the issue's three shapes (nested group in-process + spawn, group-sibling crossing, pure in-processread -u). One known residual: a dup whose source is rebound later in the same list resolves position-blind (recorded with reproducer in the cluster REPORT; it was an invalid-fd error pre-fix, so no regression). One probe diffs only intime's millisecond accounting.New pins:
tests/issue2305_heredoc_body_correlation_regressions.rs— 16/16 under the scrubbed-fd runner. Controls: issue2292 18/18, issue2235 11/11, lib 985/985, regression 221 passed / 6 failed — the 6 failures exactly the known local stock reds — 8 adjacent fd pin targets green (74 tests).🤖 Generated with Claude Code