Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Reflect in Current Directory First
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Filip Nguyen committed Jan 22, 2020
1 parent 3dcdcb6 commit 2a0eb5d
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions mockgen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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
Expand All @@ -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, "")
}

Expand Down

0 comments on commit 2a0eb5d

Please sign in to comment.