Skip to content

Commit

Permalink
fix aux_files relative paths for gomock source mocks (fix #3752) (#…
Browse files Browse the repository at this point in the history
…3753)

* fix aux_files relative paths for gomock source mocks (fix #3752)

* explicitly set GOROOT for the gomock run and disable modules

* remove f from the needed_files in the gomock implementation

* refer to the "gopath" string via the variable
  • Loading branch information
ikavalio authored Dec 12, 2023
1 parent d4667fe commit 8401b8c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
11 changes: 8 additions & 3 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def _gomock_source_impl(ctx):
go_ctx = go_context(ctx)

# create GOPATH and copy source into GOPATH
go_path_prefix = "gopath"
source_relative_path = paths.join("src", ctx.attr.library[GoLibrary].importmap, ctx.file.source.basename)
source = ctx.actions.declare_file(paths.join("gopath", source_relative_path))
source = ctx.actions.declare_file(paths.join(go_path_prefix, source_relative_path))

# trim the relative path of source to get GOPATH
gopath = source.path[:-len(source_relative_path)]
Expand All @@ -55,14 +56,14 @@ def _gomock_source_impl(ctx):
aux_files = []
for target, pkg in ctx.attr.aux_files.items():
f = target.files.to_list()[0]
aux = ctx.actions.declare_file(paths.join(gopath, "src", pkg, f.basename))
aux = ctx.actions.declare_file(paths.join(go_path_prefix, "src", pkg, f.basename))
ctx.actions.run_shell(
outputs = [aux],
inputs = [f],
command = "mkdir -p {0} && cp -L {1} {0}".format(aux.dirname, f.path),
)
aux_files.append("{0}={1}".format(pkg, aux.path))
needed_files.append(f)
needed_files.append(aux)
args += ["-aux_files", ",".join(aux_files)]

inputs = (
Expand All @@ -82,9 +83,11 @@ def _gomock_source_impl(ctx):
toolchain = GO_TOOLCHAIN_LABEL,
command = """
export GOPATH=$(pwd)/{gopath} &&
export GOROOT=$(pwd)/{goroot} &&
{cmd} {args} > {out}
""".format(
gopath = gopath,
goroot = go_ctx.sdk.root_file.dirname,
cmd = "$(pwd)/" + ctx.file.mockgen_tool.path,
args = " ".join(args),
out = ctx.outputs.out.path,
Expand All @@ -93,6 +96,8 @@ def _gomock_source_impl(ctx):
env = {
# GOCACHE is required starting in Go 1.12
"GOCACHE": "./.gocache",
# gomock runs in the special GOPATH environment
"GO111MODULE": "off",
},
)

Expand Down
17 changes: 16 additions & 1 deletion tests/extras/gomock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "client",
srcs = [
"client.go",
"client_wrapper.go",
],
importpath = "github.com/bazelbuild/rules_go/gomock/client",
visibility = ["//visibility:public"],
Expand All @@ -14,19 +15,33 @@ go_library(
)

gomock(
name = "mocks",
name = "client_mocks",
out = "client_mock.go",
library = ":client",
package = "client",
source = "client.go",
visibility = ["//visibility:public"],
)

gomock(
name = "wrapper_mocks",
out = "wrapper_mock.go",
aux_files = {
"client.go": "github.com/bazelbuild/rules_go/gomock/client",
},
library = ":client",
package = "client",
self_package = "github.com/bazelbuild/rules_go/gomock/client",
source = "client_wrapper.go",
visibility = ["//visibility:public"],
)

go_test(
name = "client_test",
srcs = [
"client_mock.go",
"client_test.go",
"wrapper_mock.go",
],
embed = [":client"],
deps = ["@com_github_golang_mock//gomock"],
Expand Down
1 change: 1 addition & 0 deletions tests/extras/gomock/client_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package client

var _ Client = (*MockClient)(nil)
var _ ClientWrapper = (*MockClientWrapper)(nil)
6 changes: 6 additions & 0 deletions tests/extras/gomock/client_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package client

type ClientWrapper interface {
Client
Close() error
}

0 comments on commit 8401b8c

Please sign in to comment.