Skip to content

cp --parents with a backslash drive-absolute destination leaves a C: directory in the current directory #1980

Done

Description

@ShiomiHC
s=$(mktemp -d) || exit 1
mkdir -p "$s/w/src/a" "$s/d" && echo x > "$s/w/src/a/f" && echo y > "$s/w/g"
cd "$s/w" || exit 1
D=$(cygpath -w "$s/d")
cp --parents g "$D\\"; echo "1 flat file, D\\: rc=$? cwd: $(ls -A | tr '\n' ' ')"; rm -rf ./C*
cp --parents src/a/f "$D"; echo "2 nested, D: rc=$? cwd: $(ls -A | tr '\n' ' ')"; rm -rf ./C*
cp --parents g "$(cygpath -m "$s/d")/"; echo "3 forward slashes: rc=$? cwd: $(ls -A | tr '\n' ' ')"
cp g "$D\\"; echo "4 no --parents: rc=$? cwd: $(ls -A | tr '\n' ' ')"
echo "dest: $(cd "$s/d" && find . -type f | sort | tr '\n' ' ')"
cd / && rm -rf "$s"

Run in Git Bash, with WinuxCmd's cp.exe first on PATH.

WinuxCmd main:

1 flat file, D\: rc=0 cwd: C: g src 
2 nested, D: rc=0 cwd: C: g src 
3 forward slashes: rc=0 cwd: g src 
4 no --parents: rc=0 cwd: g src 
dest: ./g ./src/a/f 

GNU coreutils 8.32 (Git for Windows), and WinuxCmd 1.2.3 (WinuxCmd-1.2.3-win-x64.tar.gz):

1 flat file, D\: rc=0 cwd: g src 
2 nested, D: rc=0 cwd: g src 
3 forward slashes: rc=0 cwd: g src 
4 no --parents: rc=0 cwd: g src 
dest: ./g ./src/a/f 

When the destination is a drive-absolute path written with backslashes (C:\…\d or C:\…\d\), every cp --parents leaves an empty directory named C + U+F03A (shown as C:) in the current directory. This happens even when no intermediate directory has to be created (case 1). The copy itself is correct and the exit status is 0, so nothing reports it. A destination with forward slashes (case 3) and cp without --parents (case 4) are not affected. In niubash, cp --parents … "$TEMP/x/" hits this because $TEMP is C:\Users\…\Temp.

Possibly related: the intermediate-directory loop after parents_graft_path in src/commands/cp.cpp calls create_directory_recursive on each \-separated prefix of the whole destination path, so the first prefix is the bare C:. The output first changes at 7f576d1 (#1874): its parent 32406e3 prints the GNU output for cases 1 and 2, and 7f576d1 prints C:. That commit makes the path fold treat a bare C: as a relative colon name, so the create now lands in the current directory. GNU cp.c make_dir_parents_private only creates the directories below the destination.

WinuxCmd main at a5eed6f, 7f576d1 and 32406e3 (each built with scripts/build-with-vs.ps1, Release), WinuxCmd 1.2.3 release; Git Bash 5.3.15; Windows 11.

The issue was drafted with Claude and the reproductions were run on the versions above.

Activity

  1. caomengxuan666 commented on Nov 30, 2026

    @caomengxuan666
    Member

    Triage — confirmed, P2-medium regression on main

    Confirmed. This is real, and it is a regression on main: 1.2.3 is clean,
    main at a5eed6fb is not. Thank you for the version-pinned repro and the
    bisection — both check out.

    What I verified:

    • GNU side (pristine coreutils 9.4 oracle, cp (GNU coreutils) 9.4):
      cp --parents src/a/f d and cp --parents g d/ leave the cwd untouched
      and put the files under d/. With the backslash spelling
      cp --parents g 'C:\tmp\xyz\d\' GNU refuses (with --parents, the destination must be a directory, rc 1) and creates nothing in the cwd —
      it never splits on \ and never creates anything outside the destination
      subtree. make_dir_parents_private only creates below the destination.
    • Mechanism (source, at your tested ref a5eed6fb): the live cp on main
      is the C++ implementation (the Rust wire was rolled back by fix(cp): restore the C++ product path until the Rust wire passes symlink recreation (#1757) #1795, cp is
      not in WINUXCMD_RUST2_COMMANDS). The --parents loop at
      src/commands/cp.cpp:2262-2270 walks every \ prefix of the whole
      destination path and creates each one except the full dest itself — so for
      a backslash drive-absolute dest the first prefix created is the bare C:.
      And since 7f576d1d ([bug] applets fail on literal-colon operands: canon layer does not carrier-encode operand path components #1874) a bare C: is no longer drive syntax in the
      path fold: it folds to the relative name C + U+F03A and lands in the cwd.
      Your bisection is exactly right — the commit changed only the two fold
      twins, and before it a bare D: was explicitly exempted as "the drive
      itself, not a file name". This also explains your controls: forward-slash
      dests (case 3) have no \ for the loop to split on, and no --parents
      (case 4) never enters the loop.
    • Why it matters beyond the repro: any backslash drive-absolute
      --parents destination triggers it, including $TEMP-derived paths
      (C:\Users\…\Temp), and it fires even when no intermediate directory
      needs creating. The copy itself is correct and rc stays 0, so nothing
      reports it.

    Severity: P2-medium — silent cwd pollution on a common spelling, no data
    loss, no wrong copy, not yet shipped (1.2.3 clean). Suggested labels:
    bug + compat-verified.

    Fix direction for whoever picks this up: the loop should only create prefixes
    strictly below the destination (GNU creates only below the destination), not
    every \ prefix of the whole path — the partial != destPath guard is the
    wrong shape. The #1874 fold itself should not be reverted: the
    bare-D:-is-a-relative-name contract is deliberate (shell/MSYS side) and
    other applets depend on it. The Rust port's make_parents does not share
    this face (it only creates graft prefixes under the target), but it is not
    the live implementation right now.

    Dossier: lane-1980-triage-2026-11-30.md.

  2. added
    bug
    compat-verified
    on Nov 30, 2026
  3. added a commit that references this issue on Nov 30, 2026
  4. caomengxuan666 commented on Nov 30, 2026

    @caomengxuan666
    Member

    Fixed and merged — #1992

    PR #1992 merged (merge commit 646d4e8e). The fix is one change in the --parents intermediate-directory loop (src/commands/cp.cpp): the scan now starts at destPath.size() instead of 0, so it only creates prefixes strictly below the destination — GNU's make_dir_parents_private creates only below the destination, and the destination itself is required to exist (--parents, #1066), so none of its prefixes ever needs creating. The #1874 fold is untouched, as is the Rust make_parents (not the live implementation).

    Verification: CI full green on the PR head, CTest 4082/4082 (baseline 4081 + the new pin). The new pin cp_parents_backslash_drive_dest_leaves_no_drive_dir is your 4-case repro verbatim (backslash drive-absolute dest with/without trailing backslash, forward-slash control, no---parents control) plus a $TEMP-derived destination case; each asserts the copy, rc 0, and that the cwd listing is unchanged.

    One honest shortfall, recorded in the lane dossier: the fail-first re-run on a real Windows host could not be done this round — the devin-windows, devin-ubuntu and X299 tunnels were all down, and the lane does not build locally or under Wine. The evidence for the fix is the exact code path (old code called create_directory_recursive("C:") for your cases 1/2, matching your bisection to #1874) plus the CI pin passing on the fixed code. The re-run of your 4-case script on devin-windows is owed once the tunnel recovers; I will do it then and report back here if it disagrees.

    Closing as fixed — thank you again for the version-pinned repro and the bisection, both of which checked out exactly.

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

    bugcompat-verified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions