mockgen errors when "io" package is included in my interface code #619
Description
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:
- 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
- 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:
- The IReader code is commented out
- I successfully generate the mock with just the ISimple code included
- Then I try to generate it again to overwrite the existing mock
mockgen -source=simple.go -destination=mock_simple.go -package=simple
/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)