Skip to content
Merged
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
62 changes: 36 additions & 26 deletions core/util/fsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,29 +307,44 @@ func FileExtension(file string) string {
// If all attempts fail, and `ignoreMissing` is set to `true`, this function
// returns `filePath` as-is. Otherwise, it panics.
func TryGetFileAbsPath(filePath string, ignoreMissing bool) string {
if path.IsAbs(filePath) {
return filePath
}
// The paths that we tried
attempts := []string{}

/*
* Interpret it relative to process working directory
*/
absPathRelativeToWd, _ := filepath.Abs(filePath)
if FileExists(absPathRelativeToWd) {
return absPathRelativeToWd
}
if path.IsAbs(filePath) {
/*
* It is already an absolute path, no need to absolutize it. Check if it
* exists.
*/

/*
* Interpret it relative to the directory that contains this executable.
*/
absPathRelativeToExeDir := ""
exePath, err := os.Executable()
if err == nil {
exeDir := filepath.Dir(exePath)
absPathRelativeToExeDir = path.Join(exeDir, filePath)
if FileExists(filePath) {
return filePath
} else {
attempts = append(attempts, filePath)
}
} else {
/*
* Interpret it relative to process working directory
*/
absPathRelativeToWd, _ := filepath.Abs(filePath)
if FileExists(absPathRelativeToWd) {
return absPathRelativeToWd
} else {
attempts = append(attempts, absPathRelativeToWd)
}

if FileExists(absPathRelativeToExeDir) {
return absPathRelativeToExeDir
/*
* Interpret it relative to the directory that contains this executable.
*/
exePath, err := os.Executable()
if err == nil {
exeDir := filepath.Dir(exePath)
absPathRelativeToExeDir := path.Join(exeDir, filePath)

if FileExists(absPathRelativeToExeDir) {
return absPathRelativeToExeDir
} else {
attempts = append(attempts, absPathRelativeToExeDir)
}
}
}

Expand All @@ -338,12 +353,7 @@ func TryGetFileAbsPath(filePath string, ignoreMissing bool) string {
* return `filePath` as-is.
*/
if !ignoreMissing {
errorMsg := fmt.Sprintf("failed to absolutize path '%s', tried ['%s'", filePath, absPathRelativeToWd)
if absPathRelativeToExeDir != "" {
errorMsg += fmt.Sprintf(", '%s'", absPathRelativeToExeDir)
}
errorMsg += "], but they do not exist"

errorMsg := fmt.Sprintf("failed to absolutize path '%s', tried %v, but they do not exist", filePath, attempts)
panic(errors.New(errorMsg))
} else {
return filePath
Expand Down
Loading