I'm using bazel to run a go test on my CI system. The go test will need to use a helper binary built from another go file.
The dir structure looks like the following:
some/of/my/path
├── BUILD.bazel
├── my_utils.go
├── my_test.go
└── my_tool
├── BUILD.bazel
└── my_tool.go
I.e. the code in my_test.go
is going to use the executable binary built from dir my_tool
.
In a non-bazel case, I can just go build ./mytool
to get the executable binary that my_test.go
needs. To accommodate this the bazel world, I write the following in the bazel files:
some/of/my/path/BUILD.bazel
:
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "testutils",
srcs = ["my_utils.go"],
importpath = "some/of/my/path",
visibility = ["//visibility:public"],
deps = [
"@org_golang_x_net//context",
... (some other dependencies, not related)
],
)
go_test(
name = "my_test",
size = "enormous",
srcs = ["mytest.go"],
embed = [":testutils"],
deps = [
"//some/of/my/path/my_tool:my_tool",
],
)
some/of/my/path/my_tool/BUILD.bazel
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "my_tool_lib",
srcs = ["my_tool.go"],
importpath = "some/of/my/path/my_tool",
visibility = ["//visibility:private"],
deps = ["some dependency"],
)
go_binary(
name = "my_tool",
embed = [":my_tool_lib"],
visibility = ["//visibility:public"],
)
So the idea is when I hit bazel run
, it will build the my_tool
go binary, which the go test my_test
can use (see the deps
field of the go test).
While in the CI I hit this error:
ERROR: /home/agent/work/.go/src/some/of/my/path/BUILD.bazel:20:8: in go_test rule //some/of/my/path:my_test:
Traceback (most recent call last):
File "/home/agent/.cache/bazel/_bazel_agent/40f5e2a2e18a7cdb4cd075f919d8072b/external/io_bazel_rules_go/go/private/rules/test.bzl", line 66, column 43, in _go_test_impl
internal_source = go.library_to_source(go, ctx.attr, internal_library, ctx.coverage_instrumented())
File "/home/agent/.cache/bazel/_bazel_agent/40f5e2a2e18a7cdb4cd075f919d8072b/external/io_bazel_rules_go/go/private/context.bzl", line 247, column 26, in _library_to_source
_check_binary_dep(go, dep, "deps")
File "/home/agent/.cache/bazel/_bazel_agent/40f5e2a2e18a7cdb4cd075f919d8072b/external/io_bazel_rules_go/go/private/context.bzl", line 295, column 13, in _check_binary_dep
fail("rule {rule} depends on executable {dep} via {edge}. This is not safe for cross-compilation. Depend on go_library instead.".format(
Error in fail: rule //some/of/my/path:my_test depends on executable //some/of/my/path/my_tool:my_tool via deps. This is not safe for cross-compilation. Depend on go_library instead.
ERROR: Analysis of target '//some/of/my/path:my_test' failed; build aborted: Analysis of target '//some/of/my/path:my_test' failed
INFO: Elapsed time: 79.552s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (595 packages loaded, 12182 targets configured)
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully (595 packages loaded, 12182 targets configured)
Process exited with code 1
But my goal is to use the binary. How should I change the configs to achieve that?
You want data
, not deps
. deps
is for things linked into a binary, data
is for things the binary uses at runtime.
Once your binary is in data
, use runfiles.go to run it. Add @io_bazel_rules_go//go/tools/bazel:go_default_library
to deps
(it's a library you want linked in), and then use FindBinary
to find the path.