Skip to content

FileManager.copyItems(atPath:toPath:) produces incorrect file names when dest doesn't have a trailing slash #1266

Open
@CraigSiemens

Description

@CraigSiemens

When calling FileManager.copyItems(atPath:toPath:) with a source folder and a destination folder without a trailing slash, the copied files in the source directory will be renamed to have the destination directory name prepended to the file/directory names.

This is occurring on macOS 15.4.1 (24E263) and it didn’t occur in macOS 15.3 and earlier.

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Filed feedback FB17323332


Script to reproduce the issue

mkdir -p a/b/c z
touch a/b/foo.txt
echo 'import Foundation; try FileManager.default.copyItem(atPath: "a/b/", toPath: "z/b")' | swift -

It will start with the following folder structure before preforming the copy

.
├── a
│   └── b
│       ├── c
│       └── foo.txt
└── z

After the copy, the resulting folder structure should be

.
├── a
│   └── b
│       ├── c
│       └── foo.txt
└── z
    └── b
        ├── c
        └── foo.txt

But it is actually the following, prepending the contained item names with the destination directory name.

.
├── a
│   └── b
│       ├── c
│       └── foo.txt
└── z
    ├── b
    ├── bc
    └── bfoo.txt

Adding a trailing slash to the toPath to become "z/b/" works around the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions