Skip to content

Commit

Permalink
interp: clarify error about GOPATH probably not set
Browse files Browse the repository at this point in the history
  • Loading branch information
mpl authored Feb 2, 2021
1 parent ccb8072 commit 6337f8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions interp/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (interp *Interpreter) importSrc(rPath, importPath string, skipTest bool) (s
rPath = "."
}
dir = filepath.Join(filepath.Dir(interp.name), rPath, importPath)
} else if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
} else if dir, rPath, err = interp.pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
// Try again, assuming a root dir at the source location.
if rPath, err = interp.rootFromSourceLocation(); err != nil {
return "", err
}
if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
if dir, rPath, err = interp.pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
return "", err
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (interp *Interpreter) rootFromSourceLocation() (string, error) {

// pkgDir returns the absolute path in filesystem for a package given its import path
// and the root of the subtree dependencies.
func pkgDir(goPath string, root, importPath string) (string, string, error) {
func (interp *Interpreter) pkgDir(goPath string, root, importPath string) (string, string, error) {
rPath := filepath.Join(root, "vendor")
dir := filepath.Join(goPath, "src", rPath, importPath)

Expand All @@ -196,6 +196,9 @@ func pkgDir(goPath string, root, importPath string) (string, string, error) {
}

if len(root) == 0 {
if interp.context.GOPATH == "" {
return "", "", fmt.Errorf("unable to find source related to: %q. Either the GOPATH environment variable, or the Interpreter.Options.GoPath needs to be set", importPath)
}
return "", "", fmt.Errorf("unable to find source related to: %q", importPath)
}

Expand All @@ -205,7 +208,7 @@ func pkgDir(goPath string, root, importPath string) (string, string, error) {
return "", "", err
}

return pkgDir(goPath, prevRoot, importPath)
return interp.pkgDir(goPath, prevRoot, importPath)
}

const vendor = "vendor"
Expand Down
4 changes: 3 additions & 1 deletion interp/src_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func Test_pkgDir(t *testing.T) {
},
}

interp := &Interpreter{}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
Expand All @@ -178,7 +180,7 @@ func Test_pkgDir(t *testing.T) {
}
}

dir, rPath, err := pkgDir(goPath, test.root, test.path)
dir, rPath, err := interp.pkgDir(goPath, test.root, test.path)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6337f8b

Please sign in to comment.