Skip to content

Treat Windows junctions as symlinks in Realpath #186

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

Merged
merged 12 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.3
require (
github.com/go-json-experiment/json v0.0.0-20241127185351-9802db03f36a
github.com/google/go-cmp v0.6.0
golang.org/x/sys v0.27.0
golang.org/x/tools v0.27.0
gotest.tools/v3 v3.5.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
Expand Down
2 changes: 1 addition & 1 deletion internal/vfs/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (vfs *osFS) Realpath(path string) string {

orig := path
path = filepath.FromSlash(path)
path, err := filepath.EvalSymlinks(path)
path, err := realpath(path)
if err != nil {
return orig
}
Expand Down
11 changes: 11 additions & 0 deletions internal/vfs/realpath_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !windows

package vfs

import (
"path/filepath"
)

func realpath(path string) (string, error) {
return filepath.EvalSymlinks(path)
}
53 changes: 53 additions & 0 deletions internal/vfs/realpath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vfs

import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"

"github.com/microsoft/typescript-go/internal/tspath"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

func TestSymlinkRealpath(t *testing.T) {
t.Parallel()

tmp := t.TempDir()

target := filepath.Join(tmp, "target")
targetFile := filepath.Join(target, "file")

link := filepath.Join(tmp, "link")
linkFile := filepath.Join(link, "file")

const expectedContents = "hello"

assert.NilError(t, os.MkdirAll(target, 0o777))
assert.NilError(t, os.WriteFile(targetFile, []byte(expectedContents), 0o666))

if runtime.GOOS == "windows" {
// Don't use os.Symlink on Windows, as it creates a "real" symlink, not a junction.
assert.NilError(t, exec.Command("cmd", "/c", "mklink", "/J", link, target).Run())
} else {
assert.NilError(t, os.Symlink(target, link))
}

gotContents, err := os.ReadFile(linkFile)
assert.NilError(t, err)
assert.Equal(t, string(gotContents), expectedContents)

fs := FromOS()

targetRealpath := fs.Realpath(tspath.NormalizePath(targetFile))
linkRealpath := fs.Realpath(tspath.NormalizePath(linkFile))

if !assert.Check(t, cmp.Equal(targetRealpath, linkRealpath)) {
cmd := exec.Command("node", "-e", `console.log({ native: fs.realpathSync.native(process.argv[1]), node: fs.realpathSync(process.argv[1]) })`, linkFile)
out, err := cmd.CombinedOutput()
assert.NilError(t, err)
t.Logf("node: %s", out)
}
}
86 changes: 86 additions & 0 deletions internal/vfs/realpath_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package vfs

import (
"errors"
"os"
"syscall"

"golang.org/x/sys/windows"
)

// This implementation is based on what Node's fs.realpath.native does, via libuv: https://github.com/libuv/libuv/blob/ec5a4b54f7da7eeb01679005c615fee9633cdb3b/src/win/fs.c#L2937

func realpath(path string) (string, error) {
h, err := openMetadata(path)
if err != nil {
return "", err
}
defer windows.CloseHandle(h) //nolint:errcheck

// based on https://github.com/golang/go/blob/f4e3ec3dbe3b8e04a058d266adf8e048bab563f2/src/os/file_windows.go#L389

const _VOLUME_NAME_DOS = 0

buf := make([]uint16, 310) // https://github.com/microsoft/go-winio/blob/3c9576c9346a1892dee136329e7e15309e82fb4f/internal/stringbuffer/wstring.go#L13
for {
n, err := windows.GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), _VOLUME_NAME_DOS)
if err != nil {
return "", err
}
if n < uint32(len(buf)) {
break
}
buf = make([]uint16, n)
}

s := syscall.UTF16ToString(buf)
if len(s) > 4 && s[:4] == `\\?\` {
s = s[4:]
if len(s) > 3 && s[:3] == `UNC` {
// return path like \\server\share\...
return `\` + s[3:], nil
}
return s, nil
}

return "", errors.New("GetFinalPathNameByHandle returned unexpected path: " + s)
}

func openMetadata(path string) (windows.Handle, error) {
// based on https://github.com/microsoft/go-winio/blob/3c9576c9346a1892dee136329e7e15309e82fb4f/pkg/fs/resolve.go#L113

pathUTF16, err := windows.UTF16PtrFromString(path)
if err != nil {
return windows.InvalidHandle, err
}

const (
_FILE_ANY_ACCESS = 0

_FILE_SHARE_READ = 0x01
_FILE_SHARE_WRITE = 0x02
_FILE_SHARE_DELETE = 0x04

_OPEN_EXISTING = 0x03

_FILE_FLAG_BACKUP_SEMANTICS = 0x0200_0000
Comment on lines +58 to +66
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the naming convention used within the Go stdlib for Windows code that uses constants not defined elsewhere.

)

h, err := windows.CreateFile(
pathUTF16,
_FILE_ANY_ACCESS,
_FILE_SHARE_READ|_FILE_SHARE_WRITE|_FILE_SHARE_DELETE,
nil,
_OPEN_EXISTING,
_FILE_FLAG_BACKUP_SEMANTICS,
0,
)
if err != nil {
return 0, &os.PathError{
Op: "CreateFile",
Path: path,
Err: err,
}
}
return h, nil
}
Loading