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

Commit

Permalink
Fix source mode to not trigger go modules (#23)
Browse files Browse the repository at this point in the history
Fixes #17. Investigating a bit, golang's module mode seems to trigger when using source mode with a fake  module file, resulting in downloading modules  while doing codegen. This can be avoided by prefixing the path passed  to `-source` with `$GOPATH`, so `go/packages` recognizes the source file as being in the GOPATH.

This also removes the `_go_tool_run_shell_stdout`, which  was only  called from one place. Seemed better to remove as compared having to pass the gopath around to prefix the source flag.
  • Loading branch information
robbertvanginkel authored and jmhodges committed Aug 5, 2019
1 parent 1e4c2d6 commit 0863892
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,46 @@ _MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
_MOCKGEN_MODEL_LIB = "@com_github_golang_mock//mockgen/model:go_default_library"

def _gomock_source_impl(ctx):
args = ["-source", ctx.file.source.path]
go_ctx = go_context(ctx)
gopath = "$(pwd)/" + ctx.var["BINDIR"] + "/" + ctx.attr.gopath_dep[GoPath].gopath

# passed in source needs to be in gopath to not trigger module mode
args = ["-source", gopath + "/" + ctx.file.source.path]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
args += [",".join(ctx.attr.interfaces)]

_go_tool_run_shell_stdout(
ctx = ctx,
cmd = ctx.file.mockgen_tool,
args = args,
extra_inputs = [ctx.file.source],
out = ctx.outputs.out,
inputs = (
ctx.attr.gopath_dep.files.to_list() +
go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools
) + [ctx.file.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.
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = inputs,
tools = [
ctx.file.mockgen_tool,
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}
""".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,
),
env = {
"GO111MODULE": "off", # explicitly relying on passed in go_path to not download modules while doing codegen
},
)

_gomock_source = go_rule(
Expand Down Expand Up @@ -241,34 +270,3 @@ _gomock_prog_exec = go_rule(
},
)

def _go_tool_run_shell_stdout(ctx, cmd, args, extra_inputs, out):
go_ctx = go_context(ctx)
gopath = "$(pwd)/" + ctx.var["BINDIR"] + "/" + ctx.attr.gopath_dep[GoPath].gopath

inputs = (
ctx.attr.gopath_dep.files.to_list() +
go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools
) + extra_inputs

# 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.
ctx.actions.run_shell(
outputs = [out],
inputs = inputs,
tools = [
cmd,
go_ctx.go,
],
command = """
source <($PWD/{godir}/go env) &&
export PATH=$GOROOT/bin:$PWD/{godir}:$PATH &&
export GOPATH={gopath} &&
{cmd} {args} > {out}
""".format(
godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)],
gopath = gopath,
cmd = "$(pwd)/" + cmd.path,
args = " ".join(args),
out = out.path,
),
)

0 comments on commit 0863892

Please sign in to comment.