Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

mockgen source mode does not work with go1.11 modules #230

Closed
arjantop opened this issue Nov 22, 2018 · 31 comments
Closed

mockgen source mode does not work with go1.11 modules #230

arjantop opened this issue Nov 22, 2018 · 31 comments
Assignees

Comments

@arjantop
Copy link

"self" import is generates as x ".". It works in gopath and with reflection based mockgen.
Using -self_package does not affect the output.

Sample program (foo.go):

package foo

type Arg struct {}

type Foo interface {
	Bar(a Arg) error
}

Used command:
$ mockgen -source=foo.go

Generated output:

// Code generated by MockGen. DO NOT EDIT.
// Source: foo.go

// Package mock_foo is a generated GoMock package.
package mock_foo

import (
	x "."
	gomock "github.com/golang/mock/gomock"
	reflect "reflect"
)

// MockFoo is a mock of Foo interface
type MockFoo struct {
	ctrl     *gomock.Controller
	recorder *MockFooMockRecorder
}

// MockFooMockRecorder is the mock recorder for MockFoo
type MockFooMockRecorder struct {
	mock *MockFoo
}

// NewMockFoo creates a new mock instance
func NewMockFoo(ctrl *gomock.Controller) *MockFoo {
	mock := &MockFoo{ctrl: ctrl}
	mock.recorder = &MockFooMockRecorder{mock}
	return mock
}

// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockFoo) EXPECT() *MockFooMockRecorder {
	return m.recorder
}

// Bar mocks base method
func (m *MockFoo) Bar(a x.Arg) error {
	ret := m.ctrl.Call(m, "Bar", a)
	ret0, _ := ret[0].(error)
	return ret0
}

// Bar indicates an expected call of Bar
func (mr *MockFooMockRecorder) Bar(a interface{}) *gomock.Call {
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), a)
}

Versions:
go version go1.11.2 darwin/amd64
mockgen latest version

@arjantop arjantop changed the title mockgen does not work with go1.11 modules mockgen source mode does not work with go1.11 modules Nov 22, 2018
@myles-mcdonnell
Copy link

Any news on this? Also a problem for me exactly as described. Thx

@poy
Copy link
Collaborator

poy commented Dec 11, 2018

@arjantop What SHA of mockgen are you using? I'm on v1.2.0 (51421b9) and I'm getting different results. Your title mentions Go modules. Do you have a go.mod file I could see?

@xdefrag
Copy link

xdefrag commented Dec 12, 2018

@poy I'm getting exactly same results as @arjantop

go/bin » shasum mockgen
ee87abd95d7ff07a7d43847b1fefb45b174ad814  mockgen
module github.com/xdefrag/module

require (
	github.com/BurntSushi/toml v0.3.1 // indirect
	github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
	github.com/bxcodec/faker v1.5.0
	github.com/gogo/protobuf v1.1.1 // indirect
	github.com/golang/mock v1.2.0
	github.com/golang/protobuf v1.2.0
	github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
	github.com/inconshreveable/mousetrap v1.0.0 // indirect
	github.com/jmoiron/sqlx v1.2.0
	github.com/julienschmidt/httprouter v1.2.0 // indirect
	github.com/lib/pq v1.0.0
	github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
	github.com/mitchellh/mapstructure v1.1.2 // indirect
	github.com/mwitkow/go-proto-validators v0.0.0-20180403085117-0950a7990007
	github.com/pkg/errors v0.8.0
	github.com/prometheus/client_golang v0.9.1
	github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
	github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 // indirect
	github.com/prometheus/procfs v0.0.0-20181129180645-aa55a523dc0a // indirect
	github.com/satori/go.uuid v1.2.0
	github.com/spf13/cobra v0.0.3
	github.com/spf13/pflag v1.0.3 // indirect
	github.com/spf13/viper v1.2.1
	go.uber.org/fx v1.7.1
	go.uber.org/goleak v0.10.0 // indirect
	go.uber.org/zap v1.9.1
	golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1
	golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba // indirect
	google.golang.org/genproto v0.0.0-20181004005441-af9cb2a35e7f // indirect
	google.golang.org/grpc v1.15.0
	gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
)

@davidkuridza
Copy link

The same problem here as well. I've bypassed it by running the following after creating the mocks:

find . -type f -name 'mock_*.go' | xargs sed -i 's/*x./*/g; s/ x./ /g; /x "."/d'

@arjantop
Copy link
Author

@poy https://github.com/arjantop/golang_gomock_bug

I see the same problem on v1.2.0

@poy
Copy link
Collaborator

poy commented Dec 12, 2018

Can I get go version and go env?

@poy poy self-assigned this Dec 12, 2018
@poy poy added the type: bug label Dec 12, 2018
@poy poy added this to the v1.3.x milestone Dec 12, 2018
@arjantop
Copy link
Author

Version:
go version go1.11.2 darwin/amd64

Env:

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/arjan/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/arjan/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/arjan/code/golang_gomock_bug/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8s/rrjdjdpn3rb6l508srshpx1c0000gn/T/go-build404061787=/tmp/go-build -gno-record-gcc-switches -fno-common"

@poy
Copy link
Collaborator

poy commented Dec 12, 2018

Perfect thanks. The difference was I was leaving the code inside the GOPATH. Once I moved it out of the GOPATH the problem presented itself.

@poy
Copy link
Collaborator

poy commented Dec 12, 2018

@arjantop (any anyone else) could you please try the branch go-modules? I believe to have fixed the issue but would love some feedback.

@davidkuridza
Copy link

@poy instead of x "." I now get api "service/api" which results in import cycle:

❯ go test -tags=mock -mod=vendor -v ./...
can't load package: import cycle not allowed
package service/api
	imports service/api
...

@poy
Copy link
Collaborator

poy commented Dec 12, 2018

@davidkuridza I believe this implies you need to use the -self_package flag.

@poy
Copy link
Collaborator

poy commented Dec 12, 2018

It also appears that this won't work with anything before go 1.10. Therefore we'll have to wait until 1.12 is released before we can release this.

@davidkuridza
Copy link

@poy it works correctly with -self_package flag, thank you!

@xdefrag
Copy link

xdefrag commented Dec 13, 2018

@poy everything works like a charm, thank you.

@Helvind
Copy link

Helvind commented Feb 21, 2019

@poy Can't seem to find the branch with the fix. Any other work being done on this?

@poy
Copy link
Collaborator

poy commented Feb 24, 2019

@Helvind I'm not sure how the branch disappeared. I've pushed it back up.

There is some an incompatibility with go1.9. As we support the last 3 versions of Go, we are waiting for 1.12 to come out.

@murilobsd
Copy link

@poy it works correctly with -self_package flag, thank you!

could you please put the example of using the flag?

@david-l-riley
Copy link

Go 1.12 is released today, any chance this fix will be merged soon? We'd love to get rid of the "replace" flag (which broke a few days ago when you rebased because the commit hash changed) in our project. Thanks for all your work!

@davidkuridza
Copy link

@murilobsd something like this

mockgen -package=user -self_package=github.com/example/user -destination=/path/to/mock_service.go github.com/example/user Interface

@poy poy mentioned this issue Feb 26, 2019
@poy
Copy link
Collaborator

poy commented Feb 26, 2019

@david-l-riley I think we need to get #268 merged first. Then hopefully our CI pipelines go green for #253

@david-l-riley
Copy link

The new test.src requirements seem to break go mod download now:

go: test.src/b@v0.0.0: unrecognized import path "test.src/b" (https fetch: Get https://test.src/b?go-get=1: dial tcp: lookup test.src: no such host)
go: test.src/a@v0.0.0: unrecognized import path "test.src/a" (https fetch: Get https://test.src/a?go-get=1: dial tcp: lookup test.src: no such host)
go: error loading module requirements

@poy
Copy link
Collaborator

poy commented Feb 27, 2019

@david-l-riley OK thanks! I'll try something different.

@poy
Copy link
Collaborator

poy commented Feb 27, 2019

@david-l-riley I pushed a fix.

@david-l-riley
Copy link

OK, I'm going to fork for now because every time changes are rebased onto the branch (instead of merge committed), the commit disappears, and since go.mod can't track branches, only tags and commit hashes, that breaks the build. Is there any other way to make this behave under modules without this fix?

@poy
Copy link
Collaborator

poy commented Feb 27, 2019

@david-l-riley Sorry about that. Its the problem with force pushing. I'm only doing so as I'm on a branch. Knowing you all are pulling from this branch, I'll stop doing this and ensure we squash before merging it into master.

Thanks for sticking with it!

@david-l-riley
Copy link

No, keep it up! I just forked, it's actually what I should have done in the first place. :-) Thanks again for all your work on it.

@jkellz-dev
Copy link

Any progress on this? I'm running into this specific issue when mocking GRPC clients.

@poy
Copy link
Collaborator

poy commented Mar 7, 2019

@losttrekker Sorry, #253 is still pending a review.

@forestsource
Copy link

forestsource commented Mar 11, 2019

It works correctly what converts x. to foo..

@poy poy closed this as completed in 837231f Mar 11, 2019
@david-l-riley
Copy link

Awesome, thanks so much for your work on this!

@dskoda1
Copy link

dskoda1 commented Jun 21, 2019

I still can't get this working with modules, I tried replicating what @davidkuridza commented here but now the generated code simply doesn't import the package under mock, resulting in undefined symbol errors (due to the symbols not being namespaced properly).

Is there an accepted solution / documentation anywhere for this problem?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests