Repository navigation
Let MSYS programs write to files opened with >> - #5
Merged
Merged
Conversation
`>>` 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>
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.
Fixes #1.
What was wrong
>>opened the target file with Rust'sOpenOptions::append(true). On Windows that gives a handle withFILE_APPEND_DATAbut noFILE_WRITE_DATA. When an MSYS program starts, its runtime treats an inherited stdio handle as writable only if the handle hasFILE_WRITE_DATA. So every write an MSYS program made to a>>file failed with "Bad file descriptor", andsed, 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>>access0x120196and cmd gives0x12019f, both withFILE_WRITE_DATA; gash gave0x120194.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 newHandle::Append.src/io.rs:Handle::Appendmoves to the end of the file before each write the shell makes and before a child inherits it, like Cygwin'sO_APPENDemulation. 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.rsandsrc/main.rs: if gash's own stdout or stderr arrives append-only from its parent (Node's'a'flag, Rust'sOpenOptions::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 underexec >> 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_APPENDthrough 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 byregen_expected.ps1(run twice, no flakiness). It checks file contents for:>> f 2>&1and&>>with an MSYS program writing to both streamsgit -c alias.z='!sed …' z >> f)#!/usr/bin/awk -fscriptexec >> p 2>&1mainand on the first commit of this branch, and passes with the second.cargo test --releasepasses: unit tests, all compat scripts, andmsys_tmp.🤖 Generated with Claude Code