Skip to content

Use os.Open for Windows realpath to handle long paths #862

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 7 commits into from
May 14, 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
72 changes: 51 additions & 21 deletions internal/vfs/osvfs/realpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,11 @@ import (
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))

mklink(t, target, link, true)
targetFile, linkFile := setupSymlinks(t)

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

fs := FS()

Expand All @@ -48,22 +35,65 @@ func TestSymlinkRealpath(t *testing.T) {
}
}

func mklink(t *testing.T, target, link string, isDir bool) {
t.Helper()
func setupSymlinks(tb testing.TB) (targetFile, linkFile string) {
tb.Helper()

tmp := tb.TempDir()

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

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

assert.NilError(tb, os.MkdirAll(target, 0o777))
assert.NilError(tb, os.WriteFile(targetFile, []byte("hello"), 0o666))

mklink(tb, target, link, true)

return targetFile, linkFile
}

func mklink(tb testing.TB, target, link string, isDir bool) {
tb.Helper()

if runtime.GOOS == "windows" && isDir {
// 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())
assert.NilError(tb, exec.Command("cmd", "/c", "mklink", "/J", link, target).Run())
} else {
err := os.Symlink(target, link)
if err != nil && !isDir && runtime.GOOS == "windows" && strings.Contains(err.Error(), "A required privilege is not held by the client") {
t.Log(err)
t.Skip("file symlink support is not enabled without elevation or developer mode")
tb.Log(err)
tb.Skip("file symlink support is not enabled without elevation or developer mode")
}
assert.NilError(t, err)
assert.NilError(tb, err)
}
}

func BenchmarkRealpath(b *testing.B) {
targetFile, linkFile := setupSymlinks(b)

fs := FS()
normalizedTargetFile := tspath.NormalizePath(targetFile)
normalizedLinkFile := tspath.NormalizePath(linkFile)

b.Run("target", func(b *testing.B) {
b.ReportAllocs()

for b.Loop() {
fs.Realpath(normalizedTargetFile)
}
})

b.Run("link", func(b *testing.B) {
b.ReportAllocs()

for b.Loop() {
fs.Realpath(normalizedLinkFile)
}
})
}

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

Expand Down
22 changes: 18 additions & 4 deletions internal/vfs/osvfs/realpath_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ import (
// 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
var h windows.Handle
if len(path) < 248 {
var err error
h, err = openMetadata(path)
if err != nil {
return "", err
}
defer windows.CloseHandle(h) //nolint:errcheck
} else {
// For long paths, defer to os.Open to run the path through fixLongPath.
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()

// Works on directories too since https://go.dev/cl/405275.
h = windows.Handle(f.Fd())
}
defer windows.CloseHandle(h) //nolint:errcheck

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

Expand Down