resolving imported package collision for mockgen
Asked Answered
D

1

9

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.

Diesel answered 22/5, 2018 at 21:52 Comment(0)
B
2

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.

Berkin answered 25/9, 2018 at 16:1 Comment(2)
Hi, I'm having the same issue but your solution does not seem to work.Selah
See: github.com/golang/mock/pull/163 I googled a bit on this error in combination with protobuf and grpc. I guess it has to do with the context package. @mdittmer's suggestion to use reflect mode should solve your issue.Rianon

© 2022 - 2024 — McMap. All rights reserved.