Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling go_path rule in source mode #3121

Merged
merged 4 commits into from
Apr 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid calling go_path rule in source mode
  • Loading branch information
linzhp committed Apr 15, 2022
commit 979f5f1e9c116ff3010763efee5d22ff42ef7ff5
64 changes: 33 additions & 31 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,49 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_path")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary", "GoPath")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary")
load("@bazel_skylib//lib:paths.bzl", "paths")

_MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
_MOCKGEN_MODEL_LIB = "@com_github_golang_mock//mockgen/model:go_default_library"

def _gomock_source_impl(ctx):
go_ctx = go_context(ctx)
gopath = "$(pwd)/" + ctx.bin_dir.path + "/" + ctx.attr.gopath_dep[GoPath].gopath

# create GOPATH and copy source into 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))
# trim the relative path of source to get GOPATH
gopath = source.path[:-len(source_relative_path)]
ctx.actions.run_shell(
outputs=[source],
inputs=[ctx.file.source],
command = "mkdir -p {0} && cp -L {1} {0}".format(source.dirname, ctx.file.source.path),
)
# passed in source needs to be in gopath to not trigger module mode
args = ["-source", gopath + "/src/" + ctx.attr.library[GoLibrary].importmap + "/" + ctx.file.source.basename]
args = ["-source", source.path]

args, needed_files = _handle_shared_args(ctx, args)

if len(ctx.attr.aux_files) > 0:
aux_files = []
for pkg, files in ctx.attr.aux_files.items():
for f in files:
mapped_f = gopath + "/src/" + ctx.attr.library[GoLibrary].importmap + "/" + f
aux_files.append("{0}={1}".format(pkg, mapped_f))
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))
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)
args += ["-aux_files", ",".join(aux_files)]

inputs = (
ctx.attr.gopath_dep.files.to_list() + needed_files +
needed_files +
go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools
) + [ctx.file.source]
) + [source]

# We can use the go binary from the stdlib for most of the environment
# variables, but our GOPATH is specific to the library target we were given.
Expand All @@ -57,22 +73,18 @@ def _gomock_source_impl(ctx):
go_ctx.go,
],
command = """
source <($PWD/{godir}/go env) &&
export PATH=$GOROOT/bin:$PWD/{godir}:$PATH &&
export GOPATH={gopath} &&
mkdir -p .gocache &&
export GOCACHE=$PWD/.gocache &&
{cmd} {args} > {out}
export GOPATH=$(pwd)/{gopath} &&
{cmd} {args} > {out}
""".format(
godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)],
gopath = gopath,
cmd = "$(pwd)/" + ctx.file.mockgen_tool.path,
args = " ".join(args),
out = ctx.outputs.out.path,
mnemonic = "GoMockSourceGen",
),
env = {
"GO111MODULE": "off", # explicitly relying on passed in go_path to not download modules while doing codegen
# GOCACHE is required starting in Go 1.12
"GOCACHE": "./.gocache",
},
)

Expand All @@ -98,9 +110,10 @@ _gomock_source = rule(
doc = "Ignored. If `source` is not set, this would be the list of Go interfaces to generate mocks for.",
mandatory = True,
),
"aux_files": attr.string_list_dict(
"aux_files": attr.label_keyed_string_dict(
default = {},
doc = "A map from packages to auxilliary Go source files to load for those packages.",
doc = "A map from auxilliary Go source files to their packages.",
allow_files = True,
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
Expand All @@ -120,11 +133,6 @@ _gomock_source = rule(
allow_single_file = True,
mandatory = False,
),
"gopath_dep": attr.label(
doc = "The go_path label to use to create the GOPATH for the given library. Will be set correctly by the gomock macro, so you don't need to set it.",
providers = [GoPath],
mandatory = False,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
Expand All @@ -146,15 +154,9 @@ def gomock(name, library, out, **kwargs):
mockgen_tool = kwargs["mockgen_tool"]

if kwargs.get("source", None):
gopath_name = name + "_gomock_gopath"
go_path(
name = gopath_name,
deps = [library, mockgen_tool],
)
_gomock_source(
name = name,
library = library,
gopath_dep = gopath_name,
out = out,
**kwargs
)
Expand Down