Skip to content

Let MSYS programs write to files opened with >> - #5

Merged
hex2f merged 2 commits into
mainfrom
leah/gash-issue-investigation-52e53b
Nov 6, 2026
Merged

hex2f merged 2 commits into
mainfrom
leah/gash-issue-investigation-52e53b

Conversation

@hex2f

@hex2f hex2f commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1.

What was wrong

>> opened the target file with Rust's OpenOptions::append(true). On Windows that gives a handle with FILE_APPEND_DATA but no FILE_WRITE_DATA. When an MSYS program starts, its runtime treats an inherited stdio handle as writable only if the handle has FILE_WRITE_DATA. So every write an MSYS program made to a >> file failed with "Bad file descriptor", and sed, which ignores write errors, lost its output silently. Builtins and native programs write through the handle directly and were not affected. For comparison, Git Bash gives children of >> access 0x120196 and cmd gives 0x12019f, both with FILE_WRITE_DATA; gash gave 0x120194.

Fix

Open >> files the way Git Bash and cmd do: with ordinary write access, positioned at the end of the file.

  • src/exec.rs (open_path): >> on a disk file opens it writable, moves to the end, and returns a new Handle::Append.
  • src/io.rs: Handle::Append moves to the end of the file before each write the shell makes and before a child inherits it, like Cygwin's O_APPEND emulation. Every child, MSYS or native, inherits the same handle, so they share one file position and write one after another rather than over each other. That includes MSYS programs started by native ones, such as git hooks and aliases.
  • src/sys.rs and src/main.rs: if gash's own stdout or stderr arrives append-only from its parent (Node's 'a' flag, Rust's OpenOptions::append), gash replaces it once at startup with a writable handle to the same file. When stdout and stderr are the same file, they share one handle. gash moves to the end before its own writes to it.

The first commit on this branch took a different approach: it gave each MSYS child its own reopened handle. An independent review found that this loses output whenever two writers share a redirection (cmd >> log 2>&1, pipelines and background jobs under exec >> log), and that it missed MSYS programs started by native ones. The second commit replaces that approach.

For reviewers

One difference from Git Bash remains. Git Bash's MSYS children get real O_APPEND through Cygwin's own fd passing, which gash can't do. So if a process outside this shell appends to the same file at the same moment as one of gash's children, the two can overwrite each other. Writers inside one gash redirection share a position and don't. Git Bash's native children have the same limit.

Testing

  • tests/scripts/062_msys_append.sh, with expected output generated from Git Bash by regen_expected.ps1 (run twice, no flakiness). It checks file contents for:
    • the rows from the issue
    • >> f 2>&1 and &>> with an MSYS program writing to both streams
    • an MSYS program started by git (git -c alias.z='!sed …' z >> f)
    • an #!/usr/bin/awk -f script
    • mixed builtin and MSYS writes to an existing file
    • a pipeline and a background job under exec >> p 2>&1
  • The test fails on main and on the first commit of this branch, and passes with the second.
  • Checked by hand with a small launcher that starts gash with append-only stdout and stderr on one file: builtin, MSYS and error output all land in order after the existing content.
  • cargo test --release passes: unit tests, all compat scripts, and msys_tmp.

🤖 Generated with Claude Code

hex2f and others added 2 commits November 6, 2026 19:24
`>>` opens the file append-only (FILE_APPEND_DATA without FILE_WRITE_DATA).
The MSYS runtime treats an inherited stdio handle as writable only when it
has FILE_WRITE_DATA, so every write an MSYS program made to it failed with
EBADF, and tools that ignore write errors (sed) lost their output silently.

When starting an MSYS program, give it a second handle to such a file that
can also write, positioned at the end of the file. Builtins and native
programs keep the append-only handle.

Fixes #1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The previous fix gave each MSYS child its own reopened handle with its
own file position. Writers sharing one redirection then overwrote each
other: `cmd >> log 2>&1` lost stdout lines, as did pipeline stages and
background jobs under `exec >> log`. MSYS programs started by native ones
(git hooks and aliases) still got the append-only handle and lost their
output.

Open `>>` files the way Git Bash and cmd do: with ordinary write access,
positioned at the end. A new Handle::Append moves to the end before each
write the shell makes and before a child inherits it, like Cygwin's
O_APPEND. All children share the one handle and its position.

When gash's own stdout or stderr arrives append-only from its parent,
replace it once at startup with a writable handle to the same file,
shared by both when they are the same file.

Extend the 062 compat test with the cases above and check file contents,
not only sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Output is lost when MSYS programs write to a file with >>

1 participant