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

Commit 64b0b80

Browse files
authored
add notes and error helper for vendor+reflect error (#567)
1 parent 953a5bb commit 64b0b80

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

README.md

+20-11
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ go install github.com/golang/mock/[email protected]
3030
If you use `mockgen` in your CI pipeline, it may be more appropriate to fixate
3131
on a specific mockgen version.
3232

33-
## Documentation
34-
35-
After installing, you can use `go doc` to get documentation:
36-
37-
```bash
38-
go doc github.com/golang/mock/gomock
39-
```
40-
41-
Alternatively, there is an online reference for the package hosted on GoPkgDoc
42-
[here][gomock-reference].
43-
4433
## Running mockgen
4534

4635
`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`.
265254
[ci-runs]: https://github.com/golang/mock/actions
266255
[reference-badge]: https://pkg.go.dev/badge/github.com/golang/mock.svg
267256
[reference]: https://pkg.go.dev/github.com/golang/mock
257+
258+
## Debugging Errors
259+
260+
### reflect vendoring error
261+
262+
```text
263+
cannot find package "."
264+
... github.com/golang/mock/mockgen/model
265+
```
266+
267+
If you come across this error while using reflect mode and vendoring
268+
dependencies there are three workarounds you can choose from:
269+
270+
1. Use source mode.
271+
2. Include an empty import `import _ "github.com/golang/mock/mockgen/model"`.
272+
3. Add `--build_flags=--mod=mod` to your mockgen command.
273+
274+
This error is do to changes in default behavior of the go command in more recent
275+
versions. More details can be found in
276+
[#494](https://github.com/golang/mock/issues/494).

mockgen/reflect.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"bytes"
2121
"encoding/gob"
2222
"flag"
23+
"fmt"
2324
"go/build"
25+
"io"
2426
"io/ioutil"
2527
"log"
2628
"os"
@@ -159,11 +161,18 @@ func runInDir(program []byte, dir string) (*model.Package, error) {
159161
cmdArgs = append(cmdArgs, "-o", progBinary, progSource)
160162

161163
// Build the program.
164+
buf := bytes.NewBuffer(nil)
162165
cmd := exec.Command("go", cmdArgs...)
163166
cmd.Dir = tmpDir
164167
cmd.Stdout = os.Stdout
165-
cmd.Stderr = os.Stderr
168+
cmd.Stderr = io.MultiWriter(os.Stderr, buf)
166169
if err := cmd.Run(); err != nil {
170+
sErr := buf.String()
171+
if strings.Contains(sErr, `cannot find package "."`) &&
172+
strings.Contains(sErr, "github.com/golang/mock/mockgen/model") {
173+
fmt.Fprint(os.Stderr, "Please reference the steps in the README to fix this error:\n\thttps://github.com/golang/mock#reflect-vendoring-error.")
174+
return nil, err
175+
}
167176
return nil, err
168177
}
169178

0 commit comments

Comments
 (0)