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

add notes and error helper for vendor+reflect error #567

Merged
merged 2 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions README.md
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
codyoss marked this conversation as resolved.
Show resolved Hide resolved
versions. More details can be found in
[#494](https://github.com/golang/mock/issues/494).
11 changes: 10 additions & 1 deletion mockgen/reflect.go
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