golang / mock

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

Unable to generate mocks with mockgen in go mod based project #598

Closed Katiyman closed 2 years ago

Katiyman commented 2 years ago

Actual behavior A clear and concise description of what the bug is.

I am working on migrating a dep based go project to mod. After doing a bit of refactoring generating the go.mod, I run the go mod tidy and that is working fine. But to completely build the code, in need to include the compiled protos and the mocks, i am able to compile the protos using the protoc command, but when i try to generate the mocks using mockgen the in interfaces in the *.pb.go files, generated but protoc command, I get the below error. this was working previously when the project was dep based.


 mockgen ./protos/userManagement UserInfoServer > ./mock_Client/mock_umClient/smUmConnHandler.go
build command-line-arguments: cannot find module for path _/root/gomod_sm_master/microservice/sm_service_v2/pkg/gomock_reflect_437983013/protos/userManagement
build command-line-arguments: cannot find module for path _/root/gomod_sm_master/microservice/sm_service_v2/pkg/protos/userManagement/gomock_reflect_184361024/protos/userManagement
prog.go:14:2: cannot find package "." in:
        /tmp/gomock_reflect_429045919/protos/userManagement
2021/11/09 05:07:52 Loading input failed: exit status 1

I am working on migrating a dep based go project to mod. After doing a bit of refactoring generating the go.mod, I run the go mod tidy and that is working fine. But to completely build the code, in need to include the compiled protos and the mocks, i am able to compile the protos using the protoc command, but when i try to generate the mocks using mockgen the in interfaces in the *.pb.go files, generated but protoc command, I get the below error. this was working previously when the project was dep based.

mockgen ./protos/userManagement UserInfoServer > ./mock_Client/mock_umClient/smUmConnHandler.go
build command-line-arguments: cannot find module for path _/root/gomod_sm_master/microservice/sm_service_v2/pkg/gomock_reflect_437983013/protos/userManagement
build command-line-arguments: cannot find module for path _/root/gomod_sm_master/microservice/sm_service_v2/pkg/protos/userManagement/gomock_reflect_184361024/protos/userManagement
prog.go:14:2: cannot find package "." in:
        /tmp/gomock_reflect_429045919/protos/userManagement
2021/11/09 05:07:52 Loading input failed: exit status 1

the package structure is shown as below. In this the protos folder has all the compiled .protos.


root@xxxx:~/gomod_sm_master/microservice/sm_service_v2/pkg# ls -lrt
total 240
-rw-r--r--  1 root root   2267 Nov  5 04:42 README.md
-rw-r--r--  1 root root   1655 Nov  5 04:42 Gopkg.toml_bkp
-rw-r--r--  1 root root  12877 Nov  5 04:42 Gopkg.lock_bkp
-rw-r--r--  1 root root   6283 Nov  5 04:42 change.log
drwxr-xr-x  2 root root   4096 Nov  5 04:42 cassandraSanity
drwxr-xr-x  3 root root   4096 Nov  5 04:42 migrations
drwxr-xr-x  2 root root   4096 Nov  5 04:42 logger
drwxr-xr-x  4 root root   4096 Nov  5 04:42 constants
drwxr-xr-x  2 root root   4096 Nov  5 04:42 conf
drwxr-xr-x  2 root root   4096 Nov  5 04:42 mockServer
drwxr-xr-x  2 root root   4096 Nov  5 04:42 smDbCassandra
drwxr-xr-x  2 root root   4096 Nov  5 04:42 serviceimpl
drwxr-xr-x  2 root root   4096 Nov  5 04:42 s3Access
drwxr-xr-x  2 root root   4096 Nov  5 04:42 rpc
drwxr-xr-x  3 root root   4096 Nov  5 04:42 testutils
-rw-r--r--  1 root root   8358 Nov  5 04:42 smService.go
drwxr-xr-x  5 root root   4096 Nov  5 04:42 smInputValidation
drwxr-xr-x 10 root root   4096 Nov  5 04:42 smDialers
drwxr-xr-x  2 root root   4096 Nov  5 04:42 util
-rwxr-xr-x  1 root root   1722 Nov  5 04:57 generateMock.sh
drwxr-xr-x 12 root root   4096 Nov  5 04:57 mock_Client
drwxr-xr-x  2 root root   4096 Nov  5 05:57 services
drwxr-xr-x  2 root root   4096 Nov  5 05:57 commonFunctions
drwxr-xr-x 13 root root   4096 Nov  5 05:59 protos
drwxr-xr-x  8 root root   4096 Nov  5 05:59 vendor
-rw-r--r--  1 root root 112607 Nov  5 06:21 go.sum
-rw-r--r--  1 root root   1228 Nov  5 06:21 go.mod

the command to generate the protos is as below.

  protoc -I ./ -I ./subscriptionManagement ${PROTOS} ${OPTS} --go_out=plugins=grpc${PACKAGES}:${OUTDIR}
${PROTOS}= list of protos
${OPTS}=" --go_opt=paths=source_relative "
${PACKAGES}= path in which the protos have to be importer. eg.
PACKAGES=,Mcommon/resultCodes/result.proto=${PROTO_PACKAGE}/common/resultCodes
PACKAGES+=,Mxx/common/common.proto=${PROTO_PACKAGE}/dm/common
PACKAGES+=,Mcommon/token/token.proto=${PROTO_PACKAGE}/common/token
${PROTO_PACKAGE} = path in which proto has to be imported.

Also the go.mod content


 cat go.mod
module xxx.ext.net.xxx.com/company/microservice/sm_service_v2

go 1.16

require (
        github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
        github.com/containerd/containerd v1.5.7 // indirect
        github.com/docker/docker v20.10.10+incompatible // indirect
        github.com/docker/go-connections v0.4.0 // indirect
        github.com/go-ini/ini v1.63.2 // indirect
        github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1
        github.com/golang/mock v1.6.0
        github.com/golang/protobuf v1.5.2
        github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
        github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
        github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0
        github.com/mattes/migrate v3.0.1+incompatible
        github.com/minio/minio-go v6.0.14+incompatible
        github.com/morikuni/aec v1.0.0 // indirect
        github.com/prometheus/client_golang v1.11.0 // indirect
        github.com/sirupsen/logrus v1.8.1
        gitlabe2.ext.net.nokia.com/Nokia_DAaaS/daauditlogger v1.0.4
        golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420
        google.golang.org/grpc v1.40.0
        google.golang.org/protobuf v1.27.1
        gopkg.in/yaml.v2 v2.4.0
)

replace github.com/mattes/migrate v3.0.1+incompatible => github.com/mattes/migrate v3.0.2-0.20180508041

Kindly help. Also pls comment if any other info needed. TIA Expected behavior A clear and concise description of what you expected to happen.

To Reproduce Steps to reproduce the behavior

  1. mockgen ./protos/userManagement UserInfoServer > ./mock_Client/mock_umClient/smUmConnHandler.go

Additional Information

codyoss commented 2 years ago

Please see: https://github.com/golang/mock#reflect-vendoring-error , hope that helps.