Skip to content

Use mockgen in Go modules mode to avoid errors

Mikhail Mazurskiy requested to merge ash2k/decouple-mockgen into master

By explicitly providing a version we decouple dependency resolution of mockgen that we use from go run from the agent module/dependencies. This resolves errors like seen in CI: https://gitlab.com/gitlab-org/cluster-integration/gitlab-agent/-/jobs/4434742165#L1424.

go run github.com/golang/mock/mockgen -destination mock_for_test.go -package agentkapp gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/cmd/agentk/agentkapp Runner,LeaderElector
go: downloading golang.org/x/mod v0.10.0
go: downloading golang.org/x/tools v0.8.0
/root/go/pkg/mod/github.com/golang/mock@v1.7.0-rc.1.0.20220812172401-5b455625bd2c/mockgen/mockgen.go:42:2: golang.org/x/mod@v0.10.0: missing go.sum entry for go.mod file; to add it:
	go mod download golang.org/x/mod
/root/go/pkg/mod/github.com/golang/mock@v1.7.0-rc.1.0.20220812172401-5b455625bd2c/mockgen/mockgen.go:43:2: golang.org/x/tools@v0.8.0: missing go.sum entry for go.mod file; to add it:
	go mod download golang.org/x/tools
cmd/agentk/agentkapp/doc.go:3: running "go": exit status 1
go help run
usage: go run [build flags] [-exec xprog] package [arguments...]

Run compiles and runs the named main Go package.
Typically the package is specified as a list of .go source files from a single
directory, but it may also be an import path, file system path, or pattern
matching a single known package, as in 'go run .' or 'go run my/cmd'.

If the package argument has a version suffix (like @latest or @v1.0.0),
"go run" builds the program in module-aware mode, ignoring the go.mod file in
the current directory or any parent directory, if there is one. This is useful
for running programs without affecting the dependencies of the main module.

If the package argument doesn't have a version suffix, "go run" may run in
module-aware mode or GOPATH mode, depending on the GO111MODULE environment
variable and the presence of a go.mod file. See 'go help modules' for details.
If module-aware mode is enabled, "go run" runs in the context of the main
module.

By default, 'go run' runs the compiled binary directly: 'a.out arguments...'.
If the -exec flag is given, 'go run' invokes the binary using xprog:
	'xprog a.out arguments...'.
If the -exec flag is not given, GOOS or GOARCH is different from the system
default, and a program named go_$GOOS_$GOARCH_exec can be found
on the current search path, 'go run' invokes the binary using that program,
for example 'go_js_wasm_exec a.out arguments...'. This allows execution of
cross-compiled programs when a simulator or other execution method is
available.

By default, 'go run' compiles the binary without generating the information
used by debuggers, to reduce build time. To include debugger information in
the binary, use 'go build'.

The exit status of Run is not the exit status of the compiled binary.

For more about build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.

See also: go build.

Merge request reports