From d95e87fb1749c68645801901c17e67ebed214625 Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Tue, 27 Nov 2018 18:11:17 +0100 Subject: [PATCH] Import package by files if outside of GOPATH This is for example the case, if a mock is generated for an external interface while go modules are enabled. Fixes: #79 --- pkg/moq/moq.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index d2ad06e..4300b48 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -10,6 +10,7 @@ import ( "go/token" "go/types" "io" + "io/ioutil" "os" "path" "path/filepath" @@ -234,7 +235,19 @@ func pkgInfoFromPath(src string) (*loader.PackageInfo, error) { ParserMode: parser.SpuriousErrors, Cwd: src, } - conf.Import(pkgFull) + if strings.HasPrefix(pkgFull, string(filepath.Separator)) { + files, err := ioutil.ReadDir(pkgFull) + if err != nil { + return nil, err + } + for _, file := range files { + if !file.IsDir() && strings.HasSuffix(file.Name(), ".go") && !strings.HasSuffix(file.Name(), "_test.go") { + conf.CreateFromFilenames(abs, file.Name()) + } + } + } else { + conf.Import(pkgFull) + } lprog, err := conf.Load() if err != nil { return nil, err