Repository navigation
WIP: fix(parser): heredoc io_number requires adjacency — spaced 3 <<X keeps its argument (Refs #2467) - #2468
Merged
Merged
Conversation
The HereDoc arm of handle_token derived the heredoc fd through take_heredoc_fd_prefix, which popped the command's last word whenever it was all digits — with no adjacency requirement. Every other redirection arm (and the second HereDoc arm in redirections.rs) goes through take_adjacent_redirect_fd_prefix, which requires the digit word to END exactly at the operator token. GNU parse.y read_token_word() (got_token): an all-digit token is a NUMBER only when the character that ended it is `<` or `>`; a whitespace-terminated digit token is a plain WORD, and the grammar's NUMBER LESS_LESS WORD (parse.y:629) engages only for the fused spelling. So `echo 3 <<X` keeps `3` as an argument and the heredoc lands on fd 0, while fused `3<<X` keeps working through redirect_operator_fd. Route the arm through take_adjacent_redirect_fd_prefix and drop the now-unused unchecked wrapper. The fused forms are byte-unchanged (negative controls in tests/issue2467_heredoc_fd_spacing.rs).
caomengxuan666
marked this pull request as ready for review
November 1, 2026 13:29
caomengxuan666
added a commit
that referenced
this pull request
Nov 8, 2026
`tests/cat_redirect_stream_regressions.rs::shell_bin()` trusted `CARGO_BIN_EXE_bash` unconditionally, but the `bash` bin is a Windows-only shim (required-features windows-shim; Cargo.toml: "Unix builds never produce it") — cargo still sets the env var at compile time on Linux, so both tests fail with spawn NotFound on the never-built `target/debug/bash`. Found while running the full `cargo test` for rubash #2885 (the failure is pre-existing and unrelated to this lane: the test file is untouched since #2468, and a fresh single-target run fails identically). Fix, test-only: take the bash path only when the file exists, else fall through to rubash (the existing branch 2). Behavior-preserving on Windows (the shim exists there, so it is still taken) and makes the full suite green on Linux. CI never runs the full integration suite (ci.yml invokes only --lib and --test regression), which is why this survived on master.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2467
根因
Parser 侧,不是词法侧。
src/parser/token_actions.rs的TokenKind::HereDoc臂是全仓唯一不查相邻性的重定向臂:take_heredoc_fd_prefix→take_redirect_fd_prefix(src/parser/redirections.rs)无条件弹出命令的最后一个词、只要它全是数字。于是echo 3 <<X在处理<<时把参数3弹走当作 fd 3,head -n 3 <<EOF则丢了-n的参数直接报错。其余每个重定向臂(同文件的 RedirectIn 臂、redirections.rs 的整条 assign 路径含它自己的 HereDoc 臂)都走带 column 相邻性检查的take_adjacent_redirect_fd_prefix。词法侧经核查均无问题:
number_redirect.rs与scanner.rs::finish_prefixed_input_redirect只在数字后紧贴</>时融合;alias_stream.rs的数字词规则要求词尾下一字节即</>。GNU 依据
parse.yread_token_word()(got_token 裁决):全数字 token 仅当终止它的字符是</>时才是 NUMBER;空格终止即 WORD。parse.y:629语法NUMBER LESS_LESS WORD:io_number 形态只在融合拼写3<<X时成立。<终止」⇔ 数字词 column 终点 == 操作符 column,正是相邻性检查所测。修复
take_adjacent_redirect_fd_prefix(与全部兄弟臂一致),附 GNU 裁决注释。take_heredoc_fd_prefixwrapper。redirect_operator_fd在or_else之前命中,逐字节不变。src/lexer/continuation.rs未触及。探针(脚本文件;GNU = 5.3.20 oracle)
head -n 3 <<EOF(a/b/c/d)a b crc=0head: option requires an argument -- 'n'rc=1echo 3 <<X33被吞)echo 42 <<X/echo 3 <<-X/echo 3 <<'X'/ 管道形42/3/3/3echo 3<<Y、head -n 3 < /dev/null、echo 3 > out.txt、head -n 3 <<< ignored、cat <&3 3<<X、exec 3<<X/exec 3<<-X电池(scrubbed fd 环境,
scrubbed_cargo.py)tests/issue2467_heredoc_fd_spacing.rs(13 钉:6 正例 + 7 阴性):修复前 7 passed / 6 failed → 修复后 13/13。regressionheredoc 过滤:1/1;cli_testsheredoc 过滤:13/13。引擎侧修复同时覆盖 niubash #791(同一 bug 的 niu 跟踪单)。