golang / mock

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

gomock generates mocks with import cycle #257

Closed mykytanikitenko closed 5 years ago

mykytanikitenko commented 5 years ago

In entire project I was generating mocks by putting go:generate comment above the interfaces declarations

//go:generate mockgen -source=$GOFILE -destination=../mock/mock_repo/$GOFILE

And it use to work perfectly till I updated the mockgen, which started to generate mocks with import declarations like import x "." what causes the import cycle what has broke all tests in my project.

poy commented 5 years ago

@mykytanikitenko is your project using modules and placed outside the GOPATH?

mykytanikitenko commented 5 years ago

My project does not use go modules, it placed in gopath, by all dependencies are vendored

poy commented 5 years ago

Any chance your project is hosted somewhere where I could see it?

mykytanikitenko commented 5 years ago

Sorry, it's a closed-source project. Diff between previous mocks and new one all looks like

 package mock_repo

 import (
+       x "."
        dbo "MY_PROJECT/cmd/datasets/internal/dbo"
-       repo "MY_PROJECT/cmd/datasets/internal/repo"
        db "MY_PROJECT/internal/db"
        uuidv1 "MY_PROJECT/internal/pkg/uuidv1"
        context "context"
@@ -38,9 +38,9 @@ func (m *MockDatasets) EXPECT() *MockDatasetsMockRecorder {
 }

 // Tx mocks base method
-func (m *MockDatasets) Tx(orm db.ORM) repo.Datasets {
+func (m *MockDatasets) Tx(orm db.ORM) x.Datasets {
        ret := m.ctrl.Call(m, "Tx", orm)
-       ret0, _ := ret[0].(repo.Datasets)
+       ret0, _ := ret[0].(x.Datasets)
        return ret0
 }

It use to have correct reference to interface, but started to substitute by "x" package

poy commented 5 years ago

Do you know version what you upgraded from?

mykytanikitenko commented 5 years ago

I can not be sure about mockgen binary, because I installed it using go install, but gomock vendored on revision = "c34cdb4725f4c3844d095133c6e40e448b86589b" version = "v1.1.1"

So I think it's 1.1.1 too

poy commented 5 years ago

Are you seeing this problem with 1.2.0 or with Master?

mykytanikitenko commented 5 years ago

If I took it from go get, it's from master, isn't it? I don't remember behaviour of go get

poy commented 5 years ago

go get does grab master. Can you tell me the SHA?

mykytanikitenko commented 5 years ago

Yes, I can. it's e698a2ea17fee71b248fc2bba82fc544c2017f9

mykytanikitenko commented 5 years ago

I've found what's was wrong. Sorry, it's my fault, a million apologies. Thank you for your time. I have go installation in WSL and native windows. By some circumstances, $GOPATH was overridden and project became out of GOPATH and I did not noticed it. I had tried to install 1.1.1 version by reseting the reposiotry to commit with tag 1.1.1 and still had the same wrong behaviour, so I understood that problem was on my side.

poy commented 5 years ago

Ah, I'm glad you figured it out!