golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.3k stars 611 forks source link

mockgen errors when "io" package is included in my interface code #619

Closed mpetronic closed 2 years ago

mpetronic commented 2 years ago

The actual behavior I am expecting is normal mock generation from my interface files using either go generate or directly calling mockgen - neither is working correctly now - but used to work. I have not generated mocks in awhile so clearly something changed in my setup (I am assuming) that is causing these errors. I am stumped to understand what that is so I am reaching out and would greatly appreciate some help in understanding what is happening.

There are two issues that I am seeing:

  1. Failure to generate a mock when the "io" package is included. Originally, I got there errors when importing "net/http" but, while trying to debug, I further simplified my test case and reproduced the error with just the "io" package being imported
  2. Failure to regenerate the same mock when the previous mock file exists. I used to be able regenerate mocks and overwrite existing files but now, even that does not work

Here is the most simple test case I can contrive to consistently reproduce the error. The inline comments explain how to recreate:

package simple

//go:generate mockgen -source=simple.go -destination=mock_simple.go -package=simple

// With just this ISimple interface code included, and the IReader code commented out, you can
// successfully generate the mock file. However, if after generating it, you attempt to generate it
// again, it fails. You must delete the mock file first before you can generate is again. I have not
// seen that being a problem in the past as I am certain I have regenerated mock files after making
// changes to the mocked code and it just overwrites the existing. So, that sort of points to
// something really weird in my setup but the best that I can do right now is to start with this use
// case as the most simple setup that reproduces the issue and reach out for some help

type ISimple interface {
    Get() []byte
}

var _ ISimple = (*Simple)(nil)

type Simple struct{}

func (s *Simple) Get() []byte {
    return []byte{123}
}

// Adding in this IReader code (which pulls in the import to the  "io" package) and trying to run mockgen fails to
// generate the mock file. It appears that importing the "io" package is what is resulting in what
// appears to me to be compile errors based on the log messages

// type IReader interface {
//  Read(r io.ReadCloser) (int, error)
// }

// var _ IReader = (*Reader)(nil)

// type Reader struct{}

// func (s *Reader) Read(r io.ReadCloser) (int, error) {
//  return r.Read([]byte("123"))
// }

With the IReader interface code uncommented, I get the following errors (see attached for full error log). I also get errors when:

/usr/local/go/src/runtime/proc.go:5664:23: invalid operation: shift count (id % 32) (value of type int32) must be unsigned integer /usr/local/go/src/runtime/proc.go:5657:23: invalid operation: shift count (id % 32) (value of type int32) must be unsigned integer /usr/local/go/src/runtime/panic.go:826:20: invalid operation: shift count i (variable of type int) must be unsigned integer /usr/local/go/src/runtime/panic.go:848:34: invalid operation: shift count i (variable of type int) must be unsigned integer /usr/local/go/src/runtime/panic.go:796:26: invalid operation: shift count shift (variable of type int) must be unsigned integer /usr/local/go/src/runtime/panic.go:798:31: invalid operation: shift count shift (variable of type int) must be unsigned integer /usr/local/go/src/runtime/mpallocbits.go:172:9: invalid operation: shift count sys.TrailingZeros64(x) & 63 (value of type int) must be unsigned integer <--------- SNIP ---------->



* gomock mode: source
* gomock version or git ref: github.com/golang/mock v1.6.0
* golang version:  go version go1.17.6 linux/amd64 (but I get the error with other Go versions, too)

[error.log](https://github.com/golang/mock/files/8032890/error.log)
mpetronic commented 2 years ago

I am continuing to try to figure this out as I wait for some help. FWIW, I am running on Windows 10 in WSL2. I happen to also have a AlmaLinux 8 VM workstation at work and go 17.6. I tried this test there and everything works as expected for both #1 and #2 observed issues. There no differences between the two based on go env output and VSCode is out of the loop as I am trying everything from the command line.

mpetronic commented 2 years ago

Solved. I found that there was a version of mockgen installed in /usr/bin/mockgen with references to some 1.12 go libraries in the binary (strings /usr/bin/mockgen). I had removed mockgen from my normal GOPATH - not recall why - and like got distracted and did not "go install" it again. But, since the old version of mockgen was in my PATH, it got picked up and the mystery began until I just now realized - WAIT! Why is which mockgen showing /usr/bin/mockgen? I deleted that version and "go installed" the latest and all is well now. The joys of computers. :)