Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolving imported package collision for mockgen

Tags:

mocking

go

gomock

I have a package collision where I get the following error: mockgen -package=mocks -source=myproto.pb.go -destination=mocks/my_mocks.go imported package collision: "foo" imported twice

I see one import that is obvious: import foo "blah/blah/foo"

But I don't know where to start tracking down the duplicate import (nor where it is coming from). It seems strange to me that this is an issue as I am importing myproto.pb.go just fine, so I'm wondering if there is really an issue w/duplicate imports. Also, GoLand isn't showing any issues.

I'm hoping someone can point me in the direction of getting more information about where the duplicate import is coming form, checking if there is some issue and/or working around the issue.

like image 403
Bren Avatar asked May 22 '18 21:05

Bren


1 Answers

This seems to be an ongoing issue. I just ran into it with it claiming "rand" was imported twice (even though my code doesn't import "rand").

The following workaround worked for me: Write down the list of interfaces in myproto.pb.go you wish to mock, and use "reflect mode" instead of "source mode"

mockgen -package=mocks -destination=mocks/my_mocks.go path.to/package/containing/myproto/pb/go [space-separated interface names]

should be equivalent to your previous invocation:

mockgen -package=mocks -source=myproto.pb.go -destination=mocks/my_mocks.go

but for some reason is more robust and does not trigger the double-import error.

like image 166
mdittmer Avatar answered Nov 20 '22 19:11

mdittmer