@@ -4,34 +4,40 @@ package fsutils
4
4
5
5
import (
6
6
"errors"
7
- "os"
8
7
"path/filepath"
9
8
"syscall"
10
9
)
11
10
11
+ // This is a workaround for the behavior of [filepath.EvalSymlinks],
12
+ // which fails with [syscall.ENOTDIR] if the specified path contains a junction on Windows.
13
+ // Junctions can occur, for example, when a volume is mounted as a subdirectory inside another drive.
14
+ // This can usually happen when using the Dev Drives feature and replacing existing directories.
15
+ // See: https://github.com/golang/go/issues/40180
16
+ //
17
+ // Since [syscall.ENOTDIR] is only returned when calling [filepath.EvalSymlinks] on Windows
18
+ // if part of the presented path is a junction and nothing before was a symlink,
19
+ // we simply treat this as NOT symlink,
20
+ // because a symlink over the junction makes no sense at all.
12
21
func evalSymlinks (path string ) (string , error ) {
13
22
resolved , err := filepath .EvalSymlinks (path )
14
23
if err == nil {
15
24
return resolved , nil
16
25
}
17
26
18
- // This is a workaround for the behavior of filepath.EvalSymlinks, which fails with
19
- // syscall.ENOTDIR if the specified path contains a junction on Windows. Junctions
20
- // can occur, for example, when a volume is mounted as a subdirectory inside another
21
- // drive. This can usually happen when using the Dev Drives feature and replacing
22
- // existing directories. See: https://github.com/golang/go/issues/40180
23
- //
24
- // Since syscall.ENOTDIR is only returned when calling filepath.EvalSymlinks on
25
- // Windows if part of the presented path is a junction and nothing before was a
26
- // symlink, we simply treat this as NOT symlink, because a symlink over the junction
27
- // makes no sense at all.
28
- if errors .Is (err , syscall .ENOTDIR ) {
29
- if _ , sErr := os .Stat (path ); sErr == nil {
30
- // If exists, we make the path absolute, to be sure...
31
- if abs , aErr := filepath .Abs (path ); aErr == nil {
32
- return abs , nil
33
- }
34
- }
27
+ if ! errors .Is (err , syscall .ENOTDIR ) {
28
+ return "" , err
35
29
}
36
- return "" , err
30
+
31
+ _ , err := os .Stat (path )
32
+ if err != nil {
33
+ return "" , err
34
+ }
35
+
36
+ // If exists, we make the path absolute, to be sure...
37
+ abs , err := filepath .Abs (path )
38
+ if err != nil {
39
+ return "" , err
40
+ }
41
+
42
+ return abs , nil
37
43
}
0 commit comments