diff --git a/generator/loader.go b/generator/loader.go index 325463f..0adc201 100644 --- a/generator/loader.go +++ b/generator/loader.go @@ -2,8 +2,10 @@ package generator import ( "fmt" + "go/build" "go/types" "log" + "path/filepath" "reflect" "strings" @@ -19,11 +21,19 @@ func (f *Fake) loadPackages(c Cacher, workingDir string) error { log.Printf("loaded %v packages from cache\n", len(f.Packages)) return nil } + importPath := f.TargetPackage + if !filepath.IsAbs(importPath) { + bp, err := build.Import(f.TargetPackage, workingDir, build.FindOnly) + if err != nil { + return err + } + importPath = bp.ImportPath + } p, err := packages.Load(&packages.Config{ Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes, Dir: workingDir, Tests: true, - }, f.TargetPackage) + }, importPath) if err != nil { return err } diff --git a/integration/roundtrip_test.go b/integration/roundtrip_test.go index 847dbfe..b18afdd 100644 --- a/integration/roundtrip_test.go +++ b/integration/roundtrip_test.go @@ -2,6 +2,7 @@ package integration_test import ( "fmt" + "go/build" "io/ioutil" "log" "os" @@ -22,6 +23,7 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { baseDir string relativeDir string originalGopath string + originalBuildGopath string originalGo111module string testDir string copyDirFunc func() @@ -44,11 +46,14 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { os.Setenv("GO111MODULE", "on") } originalGopath = os.Getenv("GOPATH") + originalBuildGopath = build.Default.GOPATH var err error testDir, err = ioutil.TempDir("", "counterfeiter-integration") Expect(err).NotTo(HaveOccurred()) if useGopath { os.Setenv("GOPATH", testDir) + // build.Default only reads the GOPATH env variable once on init + build.Default.GOPATH = testDir } else { os.Unsetenv("GOPATH") } @@ -103,6 +108,7 @@ func runTests(useGopath bool, t *testing.T, when spec.G, it spec.S) { } else { os.Unsetenv("GOPATH") } + build.Default.GOPATH = originalBuildGopath if baseDir == "" { return }