Why could `cp` in Cygwin could fail with the error «cp: cannot create symbolic link '<...>': No such file or directory» in the `winsymlinks:nativestrict` mode?

This problem is due to the way Cygwin handles symbolic links during recursive copying.

The problem sequence

  1. During recursive directory copying, cp traverses the directory structure in a specific order
  2. When it finds a symbolic link, it attempts to recreate that link in the destination
  3. If the link points to a relative path (like ../some-package/bin.js), Cygwin checks if that target exists in the destination directory structure
  4. In winsymlinks:nativestrict mode, Cygwin performs strict security checks on the target path
  5. If the target file/directory hasn't been copied yet (because of the traversal order), these checks fail
  6. The error "cannot create symbolic link: No such file or directory" is generated

Symlink modes in Cygwin

Cygwin's behavior with symbolic links is controlled by the CYGWIN environment variable:

Mode Description Behavior with symbolic links
Default (unset) Uses Cygwin's emulated symlinks Creates special files that only Cygwin recognizes as symlinks
winsymlinks:native Uses native Windows NTFS symbolic links Creates proper Windows symlinks, less strict about target existence
winsymlinks:nativestrict Uses native Windows NTFS symlinks with strict security checks Creates Windows symlinks but strictly verifies target paths

A comparison of symbolic link types in Windows and Cygwin

Solution

Why this works

The winsymlinks:native mode still creates proper Windows symbolic links but skips some of the strict security checks that would otherwise fail when targets don't yet exist in the destination directory. This allows the copy operation to complete successfully even with complex directory structures.

Native Windows symlinks will be properly recognized by both Windows and Cygwin tools, preserving the integrity of your directory structure.