Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path/filepath: EvalSymlinks ignores link type on Windows #71165

Open
neild opened this issue Jan 7, 2025 · 3 comments
Open

path/filepath: EvalSymlinks ignores link type on Windows #71165

neild opened this issue Jan 7, 2025 · 3 comments
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. OS-Windows
Milestone

Comments

@neild
Copy link
Contributor

neild commented Jan 7, 2025

Windows makes a distinction between symlinks to a file and to a directory. A file link pointing to a directory cannot be traversed, nor can a directory link pointing to a file.

filepath.EvalSymlinks doesn't pay any attention to the type of links, however, and will resolve links that Windows will not. It probably should behave consistently with the rest of the OS.

Possible testcases (currently failing):

func TestWindowsEvalSymlinksDirectoryLinkToFile(t *testing.T) {
        dir := tempDirCanonical(t)
        if err := os.WriteFile(dir+"/target", nil, 0666); err != nil {
                t.Fatal(err)
        }
        mustExec(t, "cmd", "/c", "mklink", "/D", dir+`\symlink`, dir+`\target`)
        if _, err := filepath.EvalSymlinks(dir + `\symlink`); err == nil {
                t.Errorf("EvalSymlinks(symlink) succeeded; want error (directory link to file target)")
        }
        if _, err := os.ReadFile(dir + `\symlink`); err == nil {
                t.Errorf("ReadFile(symlink) succeeded; want error (directory link to file target)")
        }
}

func TestWindowsEvalSymlinksFileLinkToDirectory(t *testing.T) {
        dir := tempDirCanonical(t)
        if err := os.Mkdir(dir+"/target", 0777); err != nil {
                t.Fatal(err)
        }
        mustExec(t, "cmd", "/c", "mklink", dir+`\symlink`, dir+`\target`)
        if _, err := filepath.EvalSymlinks(dir + `\symlink`); err == nil {
                t.Errorf("EvalSymlinks(filelink) succeeded; want error (file link to directory target)")
        }
        if _, err := os.ReadDir(dir + `\symlink`); err == nil {
                t.Errorf("ReadDir(symlink) succeeded; want error (file link to directory target)")
        }
}

func mustExec(t *testing.T, cmd string, args ...string) {
        output, err := exec.Command(cmd, args...).CombinedOutput()
        if err != nil {
                t.Fatalf("command failed: %q\n%v", cmd, string(output))
        }
}
@neild
Copy link
Contributor Author

neild commented Jan 7, 2025

/cc @qmuntal

@prattmic
Copy link
Member

prattmic commented Jan 8, 2025

cc @robpike @rsc for path/filepath

@prattmic prattmic added OS-Windows NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jan 8, 2025
@prattmic prattmic added this to the Backlog milestone Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. OS-Windows
Projects
None yet
Development

No branches or pull requests

3 participants