From 64b0b80f458052cd8a56d8310258235f2237a214 Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Fri, 4 Jun 2021 15:06:01 -0600 Subject: [PATCH] add notes and error helper for vendor+reflect error (#567) --- README.md | 31 ++++++++++++++++++++----------- mockgen/reflect.go | 11 ++++++++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0a2be682..dad8724a 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,6 @@ go install github.com/golang/mock/mockgen@v1.5.0 If you use `mockgen` in your CI pipeline, it may be more appropriate to fixate on a specific mockgen version. -## Documentation - -After installing, you can use `go doc` to get documentation: - -```bash -go doc github.com/golang/mock/gomock -``` - -Alternatively, there is an online reference for the package hosted on GoPkgDoc -[here][gomock-reference]. - ## Running mockgen `mockgen` has two modes of operation: source and reflect. @@ -265,3 +254,23 @@ If the received value is `3`, then it will be printed as `03`. [ci-runs]: https://github.com/golang/mock/actions [reference-badge]: https://pkg.go.dev/badge/github.com/golang/mock.svg [reference]: https://pkg.go.dev/github.com/golang/mock + +## Debugging Errors + +### reflect vendoring error + +```text +cannot find package "." +... github.com/golang/mock/mockgen/model +``` + +If you come across this error while using reflect mode and vendoring +dependencies there are three workarounds you can choose from: + +1. Use source mode. +2. Include an empty import `import _ "github.com/golang/mock/mockgen/model"`. +3. Add `--build_flags=--mod=mod` to your mockgen command. + +This error is do to changes in default behavior of the go command in more recent +versions. More details can be found in +[#494](https://github.com/golang/mock/issues/494). diff --git a/mockgen/reflect.go b/mockgen/reflect.go index 620b5f15..e24efce0 100644 --- a/mockgen/reflect.go +++ b/mockgen/reflect.go @@ -20,7 +20,9 @@ import ( "bytes" "encoding/gob" "flag" + "fmt" "go/build" + "io" "io/ioutil" "log" "os" @@ -159,11 +161,18 @@ func runInDir(program []byte, dir string) (*model.Package, error) { cmdArgs = append(cmdArgs, "-o", progBinary, progSource) // Build the program. + buf := bytes.NewBuffer(nil) cmd := exec.Command("go", cmdArgs...) cmd.Dir = tmpDir cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stderr = io.MultiWriter(os.Stderr, buf) if err := cmd.Run(); err != nil { + sErr := buf.String() + if strings.Contains(sErr, `cannot find package "."`) && + strings.Contains(sErr, "github.com/golang/mock/mockgen/model") { + fmt.Fprint(os.Stderr, "Please reference the steps in the README to fix this error:\n\thttps://github.com/golang/mock#reflect-vendoring-error.") + return nil, err + } return nil, err }