Skip to content

fix(builtins): source /dev/stdin reads the command's own stdin redirect (rubash#1642) - #1685

Closed
arookieofc wants to merge 1 commit into
masterfrom
fix-1642-source-dev-stdin-redir
Closed

arookieofc wants to merge 1 commit into
masterfrom
fix-1642-source-dev-stdin-redir

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #1642.

GNU redir.c do_redirections binds fd 0 before the builtin runs, so opening /dev/stdin reads the redirect. Rubash's /dev/stdin arm fell through to the pipeline buffer or the process stdin instead: with a here-string, heredoc, or < file redirect it blocked on a live stdin, or silently read EOF when stdin was redirected (the piped form worked only because the pipe IS the process stdin).

The arm now checks first whether the source command carries its own fd-0 stdin redirect, and if so sources the redirect's text via stdin_string_for_command_mut (the same effective-stdin path other builtins use). Bare commands in pipelines keep the existing FUNCTION_STDIN / process-stdin path untouched, and an explicit redirect still wins over a pipe. A failed redirect never reaches here: the simple-command dispatch reports it first (rc 1, source never runs).

Verification (local, master 499a5e2 + this fix, vs GNU):

  • source /dev/stdin <<< 'echo SRCHS' -> SRCHS, rc 0; < file -> FROMFILE2, rc 0; heredoc -> FROMHEREDOC, rc 0; pipe -> SRCSTDIN, rc 0; /dev/fd/0 and /proc/self/fd/0 aliases work; pipe + explicit redirect -> redirect wins; missing redirect -> diagnostic, rc 1.
  • A read inside the sourced script sees EOF, matching GNU (the redirect text was consumed as the script).
  • New regression file tests/issue1642_source_dev_stdin_redirect_regressions.rs: 7/7 passed (stdin nulled so a regression cannot hang the suite); related source suites (issue396, issue426, issue451, issue1563) still pass.

…ct (rubash#1642)

GNU redir.c do_redirections binds fd 0 before the builtin
runs, so opening /dev/stdin reads the redirect. Rubash's
/dev/stdin arm fell through to the pipeline buffer or the
process stdin instead: with a here-string, heredoc, or '< file'
redirect it blocked on a live stdin, or silently read EOF when
stdin was redirected (the piped form worked only because the
pipe IS the process stdin).

The arm now checks first whether the source command carries
its own fd-0 stdin redirect, and if so sources the redirect's
text via stdin_string_for_command_mut (the same effective-stdin
path other builtins use). Bare commands in pipelines keep the
existing FUNCTION_STDIN / process-stdin path untouched, and an
explicit redirect still wins over a pipe. A failed redirect
never reaches here: the simple-command dispatch reports it
first (rc 1, source never runs).

Regression tests:
tests/issue1642_source_dev_stdin_redirect_regressions.rs (7 tests:
here-string, file, heredoc, pipe control, redirect-wins-over-
pipe, /dev/fd/0 alias, missing-redirect fails fast).
caomengxuan666 added a commit that referenced this pull request Oct 25, 2026
…6-10-26)

Tests-only incorporation per the fd-source dual-route ruling
(~/workspace/collab-pr-review/REPORT.md §3.2): the slice takes the
route, and the two test shapes only the single-issue PR #1685 carried
are pinned in the slice's regression file before #1685 is closed as
incorporated:

- explicit fd-0 redirection wins over piped process stdin
  (GNU: redir.c binds fd 0 before the builtin runs)
- /dev/fd/0 alias spelling of the same binding

No production code change; the slice's read-only stdin ladder already
resolves both shapes (redirect_cmd before FUNCTION_STDIN; /dev/fd/0
shares the /dev/stdin matches arm).
caomengxuan666 added a commit that referenced this pull request Oct 26, 2026
…6-10-26)

Tests-only incorporation per the fd-source dual-route ruling
(~/workspace/collab-pr-review/REPORT.md §3.2): the slice takes the
route, and the two test shapes only the single-issue PR #1685 carried
are pinned in the slice's regression file before #1685 is closed as
incorporated:

- explicit fd-0 redirection wins over piped process stdin
  (GNU: redir.c binds fd 0 before the builtin runs)
- /dev/fd/0 alias spelling of the same binding

No production code change; the slice's read-only stdin ladder already
resolves both shapes (redirect_cmd before FUNCTION_STDIN; /dev/fd/0
shares the /dev/stdin matches arm).
@caomengxuan666

Copy link
Copy Markdown
Member

Closing as incorporated per the 2026-10-26 fd-source dual-route ruling (recorded in DECISIONS.md; full comparison in the collab PR review report §3.2/§4.3).

The slice PR #1689 was chosen as the single landing route for #1642 and has merged (merge commit 9355be310c9fbb164992ce028006deb982bb7861). The slice's read-only stdin ladder covers the wider surface (enclosing function/group fd-0 bindings, not only the command's own explicit redirect) and carries the extra guard that a command whose fd-0 redirect resolves to nothing never falls through to the process stdin, pinned by a dedicated safety test.

As the ruling prescribed, this PR's two unique test shapes were incorporated into the slice tests-only before the merge (commit b38e59b3 in the merged branch), into tests/issue1642_source_dev_stdin_regressions.rs:

  • issue1642_source_dev_stdin_explicit_redirect_wins_over_pipe — explicit fd-0 redirection wins over piped process stdin (your source_dev_stdin_explicit_redirect_wins_over_pipe)
  • issue1642_source_dev_fd0_alias_here_string — the /dev/fd/0 alias spelling (your source_dev_fd0_alias_herestring)

Both ran green on the landed tree as part of that file's 13/13 (full slice gates: targeted 30/0/2, lib failure set unchanged {ownership}, differential 305/306 with failure set {ms10}). The rest of this PR's test shapes duplicate the slice's, and its production route is superseded — nothing unique is lost by closing it unmerged. The two routes conflict on the same source.rs hunk, so they cannot both land.

Thanks for the contribution — including the two test shapes above, which the slice genuinely lacked.

caomengxuan666 added a commit that referenced this pull request Oct 27, 2026
Integrate PR #1783 onto current master for re-gating on the merged
tree. The three precondition commits (aa097de #1626, e6769a7 #1642,
5418fb8 #1643) are patch-equivalent to master (git cherry '-') and
drop out of the net diff; the delivery is the three per-issue commits
(755d151 #1687, 818da34 #1688, 9f0f347 #1686), unmodified.

Conflicts and resolutions (bilateral preservation throughout):

- src/executor/external_inner.rs (2 hunks):
  1. fd_is_anonymous_pipe: took master's u32-widened S_IFMT comparison
     (bionic compile fix) — same semantics, supersedes the branch's
     raw comparison from the #1626 commit.
  2. spawn_external_process pin computation: took the branch side —
     the table is computed by the extracted ambient_fd_pins_for helper
     (verbatim extraction of the loop master carries inline, plus the
     injected-group fall-through) and the #1686 command_fd_pins are
     appended after the ambient pins so a command binding wins a
     contested slot. The helper also serves the pipeline attach points,
     so spawn and pipeline paths share one computation; the
     command-redirect skip ruling is preserved inside the helper, and
     the #1686 collision ruling (re-open skipped only for injected
     group redirects) survived in the auto-merged region above.
- src/executor/pipeline_exec.rs (4 hunks): union at all four spawn
  sites — the branch's #1688 attach_ambient_fd_pins call AND master's
  ms-stopbleed G-02 process.current_dir(&self.shell_state.cwd) stamp
  both kept at every site (shell-writer consumer loop,
  spawn_wait_concurrent_external_stages,
  execute_external_prefix_streaming_tail,
  execute_external_pipeline_concurrently).
- tests (add/add, resolved to the superset side each):
  issue1626: branch side — #1688 de-ignored the pipeline known-gap
  pin and added two tests; the only master-only line was the removed
  #[ignore] itself.
  issue1642: master side — master carries two extra tests
  incorporated from PR #1685 under the 2026-10-26 fd-source ruling;
  the branch side had nothing master lacks.
  issue1643: branch side — identical to master except the #1687
  commit's de-ignore of the numbered-fd known-gap pin.

Auto-merged overlap verified: pipeline_stages.rs carries the #1688
attach beside the #438 attach; builtin_redirects.rs carries the
#1687 numbered-fd loop; merged-tree diff vs master is exactly the
8 delivery files. src/lexer/continuation.rs untouched.
@caomengxuan666
caomengxuan666 deleted the fix-1642-source-dev-stdin-redir branch October 30, 2026 01:41
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.

[bug] source /dev/stdin with file/herestring stdin redir reads nothing (GNU executes it)

2 participants