From 2a0eb5dae8b38f5c1eca409580dbf0e6726516b1 Mon Sep 17 00:00:00 2001 From: Filip Nguyen Date: Wed, 22 Jan 2020 13:55:47 +0100 Subject: [PATCH] Reflect in Current Directory First Some users have intention to run mockgen in a module aware directory with version pinned to those in `go.mod`. Always try to run reflect program in current working directory first. --- mockgen/reflect.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mockgen/reflect.go b/mockgen/reflect.go index 47f95ef2..5604baf3 100644 --- a/mockgen/reflect.go +++ b/mockgen/reflect.go @@ -129,6 +129,7 @@ func runInDir(program []byte, dir string) (*model.Package, error) { if err := cmd.Run(); err != nil { return nil, err } + return run(filepath.Join(tmpDir, progBinary)) } @@ -152,6 +153,11 @@ func reflectMode(importPath string, symbols []string) (*model.Package, error) { wd, _ := os.Getwd() + // Try to run the reflection program in the current working directory. + if p, err := runInDir(program, wd); err == nil { + return p, nil + } + // Try to run the program in the same directory as the input package. if p, err := build.Import(importPath, wd, build.FindOnly); err == nil { dir := p.Dir @@ -160,11 +166,7 @@ func reflectMode(importPath string, symbols []string) (*model.Package, error) { } } - // Since that didn't work, try to run it in the current working directory. - if p, err := runInDir(program, wd); err == nil { - return p, nil - } - // Since that didn't work, try to run it in a standard temp directory. + // Try to run it in a standard temp directory. return runInDir(program, "") }