From 1c38a191cebc54803eb4d3f01dd0ebc4595804f7 Mon Sep 17 00:00:00 2001 From: Oleg Strokachuk Date: Thu, 30 Jan 2020 16:12:59 +0300 Subject: [PATCH] Windows only issue: Fix case when file is located in gopath but they have different volume letter. For example: Error parsing fast.go: file 'E:\!project\go\src\coursera\hw3_bench\fast.go' is not in GOPATH 'e:\!project\go\' 1) filepath.Rel takes care about that; 2) add gopath variable to error --- parser/pkgpath.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/parser/pkgpath.go b/parser/pkgpath.go index 155d168..f977d29 100644 --- a/parser/pkgpath.go +++ b/parser/pkgpath.go @@ -154,7 +154,8 @@ func getPkgPathFromGOPATH(fname string, isDir bool) (string, error) { for _, p := range strings.Split(gopath, string(filepath.ListSeparator)) { prefix := filepath.Join(p, "src") + string(filepath.Separator) - if rel := strings.TrimPrefix(fname, prefix); rel != fname { + rel, err := filepath.Rel(prefix, fname) + if err == nil && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { if !isDir { return path.Dir(filePathToPackagePath(rel)), nil } else { @@ -163,7 +164,7 @@ func getPkgPathFromGOPATH(fname string, isDir bool) (string, error) { } } - return "", fmt.Errorf("file '%v' is not in GOPATH", fname) + return "", fmt.Errorf("file '%v' is not in GOPATH '%v'", fname, gopath) } func filePathToPackagePath(path string) string {