Skip to content

ln -s: "./"-prefixed relative targets stored verbatim are unresolvable by native Windows consumers (+ false-negative ENOENT on relative targets) #1101

Closed

Description

@caomengxuan666

Found while investigating niubash#109 (unixwin/niubash#109): a user creates a symlink with ln -s ./bds ./q as Administrator, the link is created, but Explorer/PowerShell then report 「文件、目录或卷标名称语法不正确」 (ERROR_INVALID_NAME) when accessing it.

Repro (winuxcmd v1.0.7, ln.cpp identical at HEAD)

D:\ln-t2> mkdir bds
D:\ln-t2> ln -s ./bds ./q     # rc=1 (!), but the link IS created
D:\ln-t2> fsutil reparsepoint query D:\ln-t2\q
    SubstituteName: "./bds" (5 chars, UTF-16), flags = SYMLINK_FLAG_RELATIVE

Native resolution (PowerShell / Explorer / cmd):

Get-ChildItem D:\ln-t2\q
-> 文件名、目录名或卷标语法不正确。: 'D:\ln-t2\q'.   (ERROR_INVALID_NAME)
(Get-Item D:\ln-t2\q).Target
-> ./bds

Root cause

create_symlink passes the user-supplied source string to CreateSymbolicLinkW verbatim, so NT stores it as the relative substitute name. The NT object manager does not accept a leading ./ component in a relative symlink substitute name — unlike the Linux kernel, which resolves ./x fine (this is why GNU ln on Unix gets away with it).

Resolution matrix verified on the same machine (all links created by winuxcmd ln, all resolved via native Win32 consumers):

stored substitute native resolution
bds (bare name) OK
./bds (dot-slash prefix) FAIL — ERROR_INVALID_NAME (123)
../bds (dot-dot prefix) OK
dir/file (contains a slash, no leading ./) OK
\??\C:\... absolute OK

Fix: normalize the target before CreateSymbolicLinkW — strip redundant leading ./ components (repeatedly, e.g. ././x → x), leave ../ components untouched (verified working), and consider absolutizing to \??\ form as a fallback. This matches what Git for Windows / MSYS winsymlinks:nativestrict effectively does.

Secondary bug: false-negative ENOENT on relative targets

Observed repeatably on this machine (same binary): for slash-less or ./-prefixed relative targets, CreateSymbolicLinkW returns FALSE with GetLastError() == ERROR_FILE_NOT_FOUND even though the reparse point ends up created, so ln prints

ln: failed to create symbolic link './q': No such file or directory   (rc=1)

while fsutil reparsepoint query shows the link exists. Targets with ../ or an embedded slash return rc=0 consistently under the same conditions. Suspect a filter-driver interaction or a double-create path in the hardlink-dispatch layer; worth instrumenting. GNU ln prints nothing and exits 0 here.

Expected (GNU) behavior

ln -s ./bds ./q → silent success, rc=0, link resolves. On Windows that additionally requires the stored target to be normalized so native consumers can resolve it.

Environment

winuxcmd v1.0.7 (ln.cpp identical at repo HEAD 13cd487), Windows 10 19044, Administrator (SeCreateSymbolicLink held), NTFS.

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-gap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions