Skip to content

Commit 66e364d

Browse files
committed
Add junctions workaround.
1 parent c16fb19 commit 66e364d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

pkg/fsutils/fsutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func EvalSymlinks(path string) (string, error) {
6161
}
6262

6363
var er evalSymlinkRes
64-
er.path, er.err = filepath.EvalSymlinks(path)
64+
er.path, er.err = evalSymlinks(path)
6565
evalSymlinkCache.Store(path, er)
6666

6767
return er.path, er.err

pkg/fsutils/fsutils_unix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !windows
2+
3+
package fsutils
4+
5+
import "path/filepath"
6+
7+
func evalSymlinks(path string) (string, error) {
8+
return filepath.EvalSymlinks(path)
9+
}

pkg/fsutils/fsutils_windows.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build windows
2+
3+
package fsutils
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"syscall"
9+
)
10+
11+
func evalSymlinks(path string) (string, error) {
12+
resolved, err := filepath.EvalSymlinks(path)
13+
if err != nil {
14+
// This is a workaround for the behavior of filepath.EvalSymlinks, which fails with
15+
// syscall.ENOTDIR if the specified path contains a junction on Windows. Junctions
16+
// can occur, for example, when a volume is mounted as a subdirectory inside another
17+
// drive. This can usually happen when using the Dev Drives feature and replacing
18+
// existing directories. See: https://github.com/golang/go/issues/40180
19+
//
20+
// Since syscall.ENOTDIR is only returned when calling filepath.EvalSymlinks on
21+
// Windows if part of the presented path is a junction and nothing before was a
22+
// symlink, we simply treat this as NOT symlink, because a symlink over the junction
23+
// makes no sense at all.
24+
if err == syscall.ENOTDIR {
25+
if _, sErr := os.Stat(path); sErr == nil {
26+
return resolved, nil
27+
}
28+
}
29+
return "", err
30+
}
31+
return resolved, nil
32+
}

0 commit comments

Comments
 (0)