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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ go install github.com/golang/mock/mockgen@v1.5.0
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

Lines changed: 10 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy