Skip to content

Commit d822dee

Browse files
committed
Make sure utils.GetCwd() does not return a symlink
Always resolve the symlinks before returning the current working directory name. Functions like filepath.Walk do not handle symlinks well. Found out the hard way by @kroepke - Thanks!
1 parent d4cee55 commit d822dee

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

utils/utils.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ func GetCwd() string {
1717
logger.Fatal("Unable to get current directory: %v", err)
1818
}
1919

20-
return currentDir
20+
// Make sure to resolve any symlinks and get the real directory.
21+
// Functions like filepath.Walk do not handle symlinks well...
22+
dir, err := filepath.EvalSymlinks(currentDir)
23+
if err != nil {
24+
logger.Fatal("Unable to eval symlink for %v: %v", currentDir, err)
25+
}
26+
27+
return dir
2128
}
2229

2330
func Chdir(path string) {
@@ -31,10 +38,7 @@ func GetRelativePath(path string) string {
3138
return path
3239
}
3340

34-
cwd, err := os.Getwd()
35-
if err != nil {
36-
logger.Fatal("Unable to get current working directory")
37-
}
41+
cwd := GetCwd()
3842
relPath, err := filepath.Rel(cwd, path)
3943
if err != nil {
4044
logger.Fatal("Unable to get relative path for %v", path)

0 commit comments

Comments
 (0)