-
Notifications
You must be signed in to change notification settings - Fork 641
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
82d3e13
Add test
jakebailey e55a933
Copy Go implementation of EvalSymlinks
jakebailey 8998046
Treat junctions as symlinks
jakebailey f05c99d
Switch to using GetFinalPathNameByHandle
jakebailey 29498f0
fmt
jakebailey 5f91995
macOS only
jakebailey f2749e3
Add debugging
jakebailey 933f112
Revert "Switch to using GetFinalPathNameByHandle"
jakebailey 716bffb
Reapply "Switch to using GetFinalPathNameByHandle"
jakebailey ef4d9fb
test
jakebailey 99a8844
oops left a change
jakebailey 75f77b0
Merge branch 'main' into jabaile/fix-windows-junctions
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.