Skip to content

Windows: choose the child argument encoder per child — MSYS/Cygwin children lose one backslash of each pair inside quoted arguments #4326

Open

Description

@KuSh

What happens

On Windows rtk builds every child's command line with one encoder, the MSVCRT/libuv convention that native programs (git.exe, rg.exe, node, dotnet) parse with. MSYS2/Cygwin programs (Git for Windows' grep, find, ls, wc, sed, …) parse the command line with Cygwin's own rules (build_argv / quoted / globify in msys2-runtime winsup/cygwin/dcrt0.cc). The two disagree in one case: inside a quoted argument, Cygwin collapses every \\ to \, while MSVCRT keeps the backslashes that do not precede a quote.

So an argument that has to be quoted (it holds a space or a character MSYS would reinterpret) and contains a run of two or more backslashes not followed by a quote reaches an MSYS child with one backslash of each pair missing. No single encoding is exact for both kinds of child:

  • MSYS needs the run doubled.
  • MSVCRT needs it as written.
  • Moving the run outside the quotes depends on Cygwin's drive-letter heuristic, and fails for one parser or the other.

Evidence

Checked with a Linux harness built from msys2-runtime's real build_argv/quoted/globify and glob.cc (commit c770e1b9), next to an MSVCRT-rule parser. The harness was fed the exact command lines rtk's encoder produces:

argument sent MSYS child receives native child receives
\\( "\\(" \( \\(
C:\\Temp (x86) "C:\\Temp (x86)" C:\Temp (x86) C:\\Temp (x86)
a\\b( "a\\b(" a\b( a\\b(
\\srv\share\(x) "\\srv\share\(x)" \srv\share\(x) \\srv\share\(x)
C:\a\b "x" "C:\a\b \"x\"" C:\a\b "x" C:\a\b "x"

Single backslashes (ordinary Windows paths) are exact for both. Only runs of two or more are affected: a UNC path or a regex \\ in an argument that is quoted anyway. For example, rtk grep '\\(' f from PowerShell searches for \( in MSYS grep.

Switching everything to the Cygwin encoding would break native children instead: rtk git commit -m "fix: a\\b" would reach git.exe as a\\\b. That is worse, since git is the most common child.

Proposed

Choose the encoder per child. An MSYS/Cygwin program can be recognised from its executable: its PE import table names msys-2.0.dll or cygwin1.dll.

  • MSYS children get the Cygwin-exact encoding.
  • Native children keep today's encoding, which is exact for them.
  • The choice stays a pure function tested on every platform. Only the PE import scan is Windows-only I/O.
  • Cost: reading the import table of the resolved executable, about 1 ms per spawn uncached, less with a per-path cache.

Related

Activity

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

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 85%

    Summary

    Windows: single argument encoder mis-serializes backslash runs for MSYS/Cygwin children; needs per-child encoder selection via PE import table detection

    📁 Relevant files
    • src/core/child_command.rs
    • src/windows/argv_encode.rs

    Suggested Actions

    1. Coordinate with refactor(core): make MSYS-safe child argument encoding unforgettable #4103 (ChildCommand encoder refactor) since it documents this exact limitation
    2. Review linked Windows: arguments containing a double quote are corrupted when rtk spawns MSYS/Cygwin children (rtk grep, rtk proxy) #3727/Windows: MSYS/Cygwin children also lose apostrophes and glob metacharacters, not just double quotes (sequel to #3727) #4102 for prior MSYS reinterpretation reports this addresses
    3. Prototype PE import table scan (msys-2.0.dll/cygwin1.dll) with per-path caching as proposed
    4. Reuse author's Linux harness (msys2-runtime build_argv/quoted/globify) as a regression test fixture

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  3. KuSh commented on Oct 9, 2026

    @KuSh
    CollaboratorAuthor

    UNC paths, as of #4103's current head: globify reads \\ + a letter + a later \ as a DOS path, so #4103 sends such a path with the \\ and the host's first letter before the opening quote. Both parsers then read it as written, a share name holding a space included. The rest of the UNC forms are not DOS paths to globify, so they still go through the general quoting and keep the limit this issue describes:

    argument #4103 sends MSYS child receives native child receives
    \\srv\share\(x) \\s"rv\share\(x)" as written as written
    \\srv\my share\x \\s"rv\my share\x" as written as written
    \\srv\share\a\\b(1) \\s"rv\share\a\\b(1)" \\srv\share\a\b(1) as written
    \\192.168.1.5\share\x(1) "\\192.168.1.5\share\x(1)" \192.168.1.5\share\x(1) as written
    \\192.168.1.5\my share "\\192.168.1.5\my share" \192.168.1.5\my share as written
    \\?\C:\x(1) "\\?\C:\x(1)" \?\C:\x(1) as written
    \\.\pipe\x(1) "\\.\pipe\x(1)" \.\pipe\x(1) as written

    Same oracle as above (msys2-runtime's build_argv/quoted/globify next to an MSVCRT parser). So a per-child encoder still has UNC work left: an IP or digit-led host, the \\?\ and \\.\ device prefixes, and a backslash run inside the path. The Cygwin-exact encoding would double the leading \\ inside the quotes for those.

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