Merge pull request #132 from jen20/default-gopath

Detect GOPATH if not specified in $GOPATH
This commit is contained in:
Vasily Romanov
2017-10-22 20:32:15 +03:00
committed by GitHub
3 changed files with 24 additions and 0 deletions
+6
View File
@@ -4,6 +4,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"os/exec"
"strings"
)
@@ -89,3 +90,8 @@ func (p *Parser) Parse(fname string, isDir bool) error {
}
return nil
}
func getDefaultGoPath() (string, error) {
output, err := exec.Command("go", "env", "GOPATH").Output()
return string(output), err
}
+9
View File
@@ -18,6 +18,15 @@ func getPkgPath(fname string, isDir bool) (string, error) {
fname = path.Join(pwd, fname)
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
var err error
gopath, err = getDefaultGoPath()
if err != nil {
return "", fmt.Errorf("cannot determine GOPATH: %s", err)
}
}
for _, p := range strings.Split(os.Getenv("GOPATH"), ":") {
prefix := path.Join(p, "src") + "/"
if rel := strings.TrimPrefix(fname, prefix); rel != fname {
+9
View File
@@ -25,6 +25,15 @@ func getPkgPath(fname string, isDir bool) (string, error) {
fname = normalizePath(fname)
gopath := os.Getenv("GOPATH")
if gopath == "" {
var err error
gopath, err = getDefaultGoPath()
if err != nil {
return "", fmt.Errorf("cannot determine GOPATH: %s", err)
}
}
for _, p := range strings.Split(os.Getenv("GOPATH"), ";") {
prefix := path.Join(normalizePath(p), "src") + "/"
if rel := strings.TrimPrefix(fname, prefix); rel != fname {