Skip to content

Windows: MSYS/Cygwin children also lose apostrophes and glob metacharacters, not just double quotes (sequel to #3727) #4102

Open

Description

@KuSh

#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_argv is called with winshell = NOTSTATE (myself, PID_CYGPARENT) && allow_glob, which is true for a native parent like rtk (line 909).
  • With winshell true, build_argv tests if (*cmd != '"' && (!winshell || *cmd != '\'')) — so ' is a quote character too.
  • Every non-argv[0] word then goes through globify, which bails only when *word != '~' && strpbrk(word, "?*[\"\'(){}") == NULL, and otherwise runs glob(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.

Activity

  1. added
    bug
    help wanted
    on Sep 18, 2026
  2. rtk-wshm-sync-bot commented on Sep 18, 2026

    @rtk-wshm-sync-bot

    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.

  3. added 9 commits that reference this issue on Sep 18, 2026
    4e4a80a
    db20cd8
    b3ba0d5
    d7d7ec0
    e96b42c
    590c456
    24f2b08
    9722242
    e2470b6
  4. 30 remaining items

  5. added a commit that references this issue on Oct 29, 2026
    a3bcacb
  6. SNComrade commented on Nov 12, 2026

    @SNComrade
    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"].
    
  7. added 2 commits that reference this issue on Nov 17, 2026
    863d5ce
    ddcf77b
  8. KuSh commented on Nov 17, 2026

    @KuSh
    CollaboratorAuthor

    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.

  9. added a commit that references this issue on Nov 21, 2026
    de78669
  10. stevebeauge commented on Nov 23, 2026

    @stevebeauge

    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.

  11. KuSh commented on Nov 23, 2026

    @KuSh
    CollaboratorAuthor

    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.

  12. added 2 commits that reference this issue on Nov 25, 2026
    577c220
    e36369a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibughelp wantedplatform:windowspriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions