Windows: MSYS/Cygwin children also lose apostrophes and glob metacharacters, not just double quotes (sequel to #3727)
#3727 / #3728 fixed the case where an argument contains a ". That is one of several characters Cygwin's command-line parser reinterprets when the parent is a native Win32 process such as rtk.exe.
From winsup/cygwin/dcrt0.cc:
build_argvis called withwinshell = NOTSTATE (myself, PID_CYGPARENT) && allow_glob, which is true for a native parent like rtk (line 909).- With
winshelltrue,build_argvtestsif (*cmd != '"' && (!winshell || *cmd != '\''))— so'is a quote character too. - Every non-
argv[0]word then goes throughglobify, which bails only when*word != '~' && strpbrk(word, "?*[\"\'(){}") == NULL, and otherwise runsglob(pattern, GLOB_TILDE|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE, …).
Verified against a port of Cygwin's own build_argv/quoted/globify:
rtk grep "it's" file.txt → MSYS grep receives: its
rtk grep "don't" file.txt → MSYS grep receives: dont
The apostrophe is silently dropped and grep searches for the wrong string — a wrong answer, not an error. This is identical before and after #3728, i.e. pre-existing; #3728 correctly scoped itself to the double-quote case.
Confirmed on a real MSYS box — see this comment (rtk 0.50.0, Windows 11, Git for Windows 2.55.0, passing each argument to MSYS printf and to a native python.exe as the control). GLOB_BRACE does split a single argument such as x{1,2} into two argv entries, destroying a bounded-repetition regex and adding a bogus file operand.
That run also shows a consequence missing from the list above: * and ? are glob-expanded against the child's working directory, so a*b arrives as the names of whatever files happen to match. A regex such as a.*b or v[0-9]?, or any sed/awk program containing *, is silently replaced by file names, and the command still exits 0. GLOB_QUOTE strips the backslashes from #\[test\] the same way, so grep -c '#\[test\]' counts 0 where a direct call counts 2. The native child receives every argument byte-for-byte, so this is specific to MSYS/Cygwin children — which is the case for encoding per child (#4326).
Why this is not a one-line widening. The obvious fix is to widen push_child_arg's trigger from s.contains('"') to also cover ', ?*[(){} and a leading ~, since globify backslash-escapes everything inside a quoted region before globbing. But wrapping an argument also suppresses child-side globbing. That is correct for search.rs patterns (the caller's shell already globbed) and a real behaviour change for ls/find path operands invoked from cmd.exe, where no parent shell globbed. So the trigger set and the glob question need deciding together, per call site — worth its own PR.
Note libuv has the same gap, so "matches libuv" is not the right bar for this one.
Follow-up from the review of #3728.
wshm · Automated triage by AI
🔍 Automated Triage
| 🐛 Category | bug |
| 🟠 Priority | high |
| 🎯 Confidence | 85% |
Summary
On Windows, MSYS/Cygwin children silently drop apostrophes and glob metacharacters from native-parent-passed args (not just double quotes), producing wrong results without error; fix requires per-call-site decisions about quoting vs. child-side globbing, and GLOB_BRACE arg-splitting risk needs verification on real MSYS.
Triaged automatically by wshm · This is an automated analysis, not a human review.
Another data point for the apostrophe case: with an unbalanced ' the failure is not a wrong answer but a hang.
Cygwin's build_argv treats ' as an opening quote (winshell = true for a native parent), so an unbalanced one swallows every following word, including -- and the file operand. grep then has no file and reads stdin. rtk passes its own stdin through, so when the parent never closes stdin (for example the Claude Code Bash tool, where the hook rewrites grep to rtk grep), grep blocks forever. Killing rtk.exe by timeout leaves grep.exe orphaned, still holding the pipe (we had pairs alive for 5+ hours).
Seen in the wild (Windows 11, Git for Windows grep, rtk 0.49.0), the child command line of a grep.exe that stayed alive for hours:
"C:\Program Files\Git\usr\bin\grep.exe" -n -H -I --null -n -e id=\|<h1\|Toast\|navigate\|Link\|aria-label\|type=\"submit\"\|onClick\|t(' -- presentation/pages/Perfil.tsx
Minimal repro (Git Bash, sleep stands in for a parent that keeps stdin open):
$ printf "t('x')\n" > q.txt
$ sleep 20 | timeout 8 rtk grep -c "t('" q.txt ; echo rc=${PIPESTATUS[1]}
rc=124 # hangs until stdin closes; grep.exe outlives the killed rtk.exe
$ timeout 8 rtk grep -c "t('" q.txt </dev/null ; echo rc=$?
0
rc=1 # no hang, but a false "no match"
$ grep -c "t('" q.txt
1
On 0.49.0 the same hang also happened with " (for example '"PreToolUse"\|"PostToolUse"'), which #3728 should cover as of 0.50.0. I could not test 0.50.0 here. The ' path is unchanged by #3728, per this issue.
Suggestion alongside the quoting fix: give the MSYS child Stdio::null() for stdin when rtk itself got file operands, so a mis-parsed argv degrades to a wrong result instead of a process that never exits.
Workaround in the meantime: [hooks] exclude_commands = ["grep"] in config.toml.
Confirmed on a real MSYS box. This includes the brace split, which the report lists as "suspected, not yet confirmed", and a glob expansion that the report does not list.
Environment: rtk 0.50.0, Windows 11 Pro 26200, Git Bash (Git for Windows 2.55.0.windows.5), MSYS printf from GNU coreutils 8.32, native python 3.14.5.
Method: every row passes the same argument, as one argv entry, three ways:
- directly to MSYS
printf, with no rtk involved; - through
rtk proxyto MSYSprintf; - through
rtk proxyto a native child,python.exe, as the control.
The harness is a shell script file, so the command text never passes through a hook or any other rewriting before bash runs it. The scratch directory holds two empty files, a1b and a2b, so any glob expansion is visible.
| argument | direct | rtk proxy → MSYS printf '%s|' |
rtk proxy → native python |
|---|---|---|---|
it's |
it's| |
its| |
it's| |
#\[test\] |
#\[test\]| |
#[test]| |
#\[test\]| |
x{1,2} |
x{1,2}| |
x1|x2|: split into two argv entries |
x{1,2}| |
a*b |
a*b| |
a1b|a2b|: expanded against the files in the child's working directory |
a*b| |
a?b |
a?b| |
a1b|a2b|: the same expansion |
a?b| |
(x) |
(x)| |
(x)| |
(x)| |
a b |
a b| |
a b| |
a b| |
The native child receives every argument exactly. The MSYS child receives five of the seven altered, and three of those become different argument counts. This matches your build_argv/globify reading: ' is treated as a quote, \[ is treated as GLOB_QUOTE escaping, GLOB_BRACE splits the braces, and * and ? glob against the current directory.
Consequence: the results are wrong and silent, with no error.
| command | direct | via rtk proxy |
|---|---|---|
grep -c '#\[test\]' on a file with two #[test] lines |
2 | 0 |
grep -c "it's" on a file containing it's |
1 | 0 |
The glob case is arguably the worst of these. A regex such as a.*b or v[0-9]?, or any sed/awk program containing *, can be replaced by the names of whatever files happen to match in the working directory. The command then runs on different operands and still exits 0.
Since rtk proxy is documented as "execute raw command without filtering", it is the path an agent is told to fall back to when filtering is suspect. That makes it a bad place for arguments to change.
The per-child encoder proposed in #4326 (detect msys-2.0.dll / cygwin1.dll in the child's import table) would cover all of these rows. An MSYS child could be sent each argument double-quoted, with ', *, ?, [, { and \ escaped the way Cygwin's quoted() expects.
Harness (bash)
set -u
d="$(mktemp -d)"; cd "$d" || exit 1
: > a1b; : > a2b
msys_direct() { printf '%s|' "$@"; }
msys_proxy() { rtk proxy printf '%s|' "$@"; }
native_proxy(){ rtk proxy python -c 'import sys; print("|".join(sys.argv[1:]) + "|", end="")' "$@"; }
for a in "it's" '#\[test\]' 'x{1,2}' 'a*b' 'a?b' '(x)' 'a b'; do
printf '%-12s direct=[%s] proxy->MSYS=[%s] proxy->native=[%s]\n' \
"$a" "$(msys_direct "$a")" "$(msys_proxy "$a")" "$(native_proxy "$a")"
done
printf '%s\n' '#[test]' 'fn a() {}' '#[test]' 'x' > f.rs
printf 'grep direct: %s rtk proxy grep: %s\n' "$(grep -c '#\[test\]' f.rs)" "$(rtk proxy grep -c '#\[test\]' f.rs)"
Thank you — this is exactly the measurement the issue was missing, and it settles more than the one open question.
Two things your table gives that the report did not have:
- The brace split is confirmed. It was written up here as read from
globifybut unverified, because my parser port does not modelGLOB_BRACE.x{1,2}arriving as two argv entries closes that. - Glob expansion against the child's working directory was not listed at all, and it is the worst of the set.
a*bcoming back asa1b a2bmeans a regex such asa.*b, or anysed/awkprogram containing*, can be silently replaced by whatever files happen to sit in the cwd, with a 0 exit status. I have added it to the issue body.
The native python.exe control column is what makes the table decisive rather than suggestive: it isolates the behaviour to children linked against the MSYS/Cygwin runtime, which is the argument for choosing the encoder per child (#4326). Running the harness from a script file so the text never passes a hook was the right call too — it rules out rewriting as an explanation.
On coverage: #4103 encodes all seven of your shapes on its literal path, which is the path rtk proxy uses, so each one reaches the child intact. I checked them individually against a port of msys2-runtime's own build_argv/globify rather than against the encoder's intent:
| argument | 0.50.0 | on the branch |
|---|---|---|
it's |
its |
it's |
#\[test\] |
#[test] |
#\[test\] |
x{1,2} |
x1 x2 |
x{1,2} |
a*b, a?b |
matching filenames | unchanged |
(x), a b |
unchanged | unchanged |
Your (x) row is a useful trap, incidentally. It looks like evidence that ( and ) need no encoding, and they do: globify bails only when strpbrk(word, "?*[\"\'(){}") is NULL, so a paren is what makes it call glob() at all — src\(x) unquoted loses its backslash to GLOB_QUOTE, while src\main.rs, with no metacharacter, passes through untouched.
The per-child encoder in #4326 remains the better long-term shape, for the reason your control column shows: a native child needs none of this, and an MSYS child would rather be handed quoted()-style escaping than a double-quoted wrapper.
Another data point: the backslash case is still present in 0.51.0, and it reaches agents through the Claude Code hook without them asking for rtk.
Environment: rtk 0.51.0, Windows 11, Git Bash (MSYS2 GNU grep), rtk init -g --auto-patch (native Claude Code hook).
Repro (tag name changed from our real one), 6-line file: two lines that start with [TAG], and four that don't, two of which happen to start with one of the letters T, A, G:
| Command | Result |
|---|---|
/usr/bin/grep -v '^\[TAG\]' f |
the 4 non-[TAG] lines (correct) |
rtk grep -v '^\[TAG\]' f |
the 2 [TAG] lines + the 2 other lines; the two lines starting with T/A/G are missing, rc=0 |
/usr/bin/grep -v '^[TAG]' f |
identical to the rtk output above |
/usr/bin/grep '^\[TAG\]' f |
the 2 [TAG] lines |
rtk grep '^\[TAG\]' f |
the wrong lines: the two starting with T/A/G instead of the [TAG] ones, rc=0 |
rtk grep '^\[T' f |
grep: Unmatched [, an error the pattern does not have |
rtk grep -c '^\[TAG\]' f |
same count as real grep, by coincidence |
So the pattern reaches grep with its backslashes removed whatever the flags, consistent with the GLOB_QUOTE explanation above.
Why it matters for the hook: simulating rtk hook claude on … 2>&1 | grep -v "^\[TAG\]" returns updatedInput: "… | rtk grep -v …", so a grep at the end of a pipe is rewritten too. The agent sees plausible, wrong output with exit code 0 and no sign that rtk was involved. We only caught it because the same script was re-run through PowerShell and two lines were missing.
Measured as faithful on the same box (no special characters in the arguments): rtk read (cat / head / tail), rtk wc -l, rtk diff. Not rewritten by the hook: sed, awk.
Workaround we are applying: exclude_commands with grep, rg and find, but since every passthrough child is exposed (ls, diff, wc with metacharacters in arguments), we are removing rtk from our Windows machines until a release contains #4103.
Another case of the same class, on rtk 1.2.0 (official windows-msvc build via
WinGet), Windows 11 Pro for Workstations (build 10.0.26300.9974), Git for
Windows 2.55.0.windows.5, run from the Claude Code Bash tool (Git Bash).
An argument that starts with "@" and names an existing file in the working
directory is replaced by that file's contents, split into separate arguments,
when an MSYS child is started through rtk:
$ cd ~/.claude # contains a file named RTK.md
$ echo '@RTK.md'
@RTK.md
$ rtk bypass echo '@RTK.md'
# Command output Command output here is condensed to save tokens, ...
(the words of RTK.md on one line)
$ rtk bypass grep -c '@RTK.md' CLAUDE.md
grep: <each word of RTK.md>: No such file or directory
Not triggered when "@" is in the middle of an argument ('test @RTK.md end'),
or when no file of that name exists in the working directory.
This looks like the MSYS/Cygwin runtime's @file handling for a child with a
native parent, alongside the quote and glob cases already listed here. I have
not tested the rewritten `rtk grep` path, only `rtk bypass`.
It is easy to hit when searching instruction files for "@" references
(e.g. '@AGENTS.md'), and `rtk bypass`, the documented way to get raw output,
is where it showed up.
Workaround in use: [hooks] exclude_commands = ["grep", "find", "sed", "awk"].
Thanks @SNComrade. #4103 covers this case: an argument that starts with @ is quoted before an MSYS/Cygwin child sees it, and the child only reads a response file for an unquoted @. rtk bypass and rtk grep both start their child that way, so '@RTK.md' reaches it as written.
Another instance of the GLOB_QUOTE case, still present on rtk 1.2.1 (WinGet rtk-ai.rtk), Windows 11, Git Bash (Git for Windows grep): a regex escape such as \s is dropped as soon as the same argument contains ( or [, so grep searches for a literal s.
printf 'a b\n' > t.txt
printf 'asb\n' > u.txt
rtk grep -qE 'a\s(b|c)' t.txt; echo $? # 1
rtk grep -qE 'a\s[b]' t.txt; echo $? # 1
rtk grep -qE 'a\sb' t.txt; echo $? # 0 (no globify metacharacter)
grep -qE 'a\s(b|c)' t.txt; echo $? # 0
MSYS=noglob rtk grep -qE 'a\s(b|c)' t.txt; echo $? # 0
rtk grep -E 'a\s(b|c)' u.txt # asb (grep received 'as(b|c)')
Same result without -E and with a tab instead of a space. Not tested on Linux; the mechanism is specific to MSYS/Cygwin children.
Thanks @stevebeauge. #4103 covers this one too: an argument that holds (, [ or another character globify reacts to now reaches an MSYS/Cygwin child wrapped in quotes, so globify no longer reads \s as an escape, and grep gets a\s(b|c) and a\s[b] as typed. It's the same mechanism as the ^\[TAG\] case above. One limit remains inside the quotes: a run of two or more backslashes (a\\s() is still halved by the MSYS child, tracked in #4326.