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

Commit

Permalink
add notes and error helper for vendor+reflect error (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss authored Jun 4, 2021
1 parent 953a5bb commit 64b0b80
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
11 changes: 10 additions & 1 deletion mockgen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"bytes"
"encoding/gob"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 64b0b80

Please # to comment.