Skip to content

Keep here-doc input that an external program leaves unread - #8

Closed
ExpectedArgument wants to merge 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-heredoc-unread-input
Closed

ExpectedArgument wants to merge 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-heredoc-unread-input

Conversation

@ExpectedArgument

Copy link
Copy Markdown

Fixes #2.

A here-doc or here-string given to an external program was copied whole into a new pipe for that child alone (Handle::to_stdio for Handle::Mem), so a child that never read stdin used it up and the next reader got nothing.

Now the unread rest moves into one pipe the first time an external program is given it (mem_pipe in io.rs), and every later reader, builtin or program, shares that pipe, as with bash's pipe-backed here-documents. While only builtins read it, it stays in memory as before. The writer thread isn't joined: the data waits in the pipe until it is read, and the thread ends once everything is written or the last reader is gone.

Readers that read ahead now behave like Git Bash as well: { head -n1; cat; } <<EOF prints only the first line in both, and { /usr/bin/head -n1; read -r c; } <<< $'x\ny' leaves c empty in both.

Tests

  • New compat script 063_heredoc_unread_input.sh, with expected output from Git Bash 5.3.15 via regen_expected.ps1. It covers the repros from the issue (a script and a function, here-string and here-doc, x=$(/usr/bin/true) and < file), a launcher that runs cygpath and then exec cat, builtins and programs taking turns at reading, and a 100 KB here-string. It fails with v0.1.0 and passes with this branch.
  • cargo test passes: unit tests, 62/62 compat scripts and msys_tmp.

A here-doc or here-string handed to an external program was copied
whole into a pipe for that child alone, so a child that never read
stdin used it up and the next reader got nothing. Its unread rest now
moves into one pipe the first time an external program is given it,
and every later reader, builtin or program, shares that pipe, as with
bash's pipe-backed here-documents. While only builtins read it, it
stays in memory as before.

Fixes hex2f#2.
@ExpectedArgument

Copy link
Copy Markdown
Author

Closing this in favour of #14, which covers the same cases: this PR's 063_heredoc_unread_input.sh passes on main. Thanks!

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.

Heredoc and here-string stdin is lost after an external command runs

1 participant