Bazel:如何 import/build a go_binary for a go_test
Bazel: how to import/build a go_binary for a go_test
我正在使用 bazel 运行 在我的 CI 系统上进行 go 测试。 go 测试将需要使用从另一个 go 文件构建的辅助二进制文件。
目录结构如下所示:
some/of/my/path
├── BUILD.bazel
├── my_utils.go
├── my_test.go
└── my_tool
├── BUILD.bazel
└── my_tool.go
即my_test.go
中的代码将使用从目录 my_tool
.
构建的可执行二进制文件
在非 bazel 的情况下,我可以 go build ./mytool
获取 my_test.go
需要的可执行二进制文件。为了适应这个 bazel 世界,我在 bazel 文件中写了以下内容:
- 在
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"],
)
所以我的想法是当我点击 bazel run
时,它将构建 my_tool
go 二进制文件,go test my_test
可以使用它(参见 deps
字段围棋测试)。
在 CI 我遇到了这个错误:
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
但我的目标是使用二进制文件。我应该如何更改配置以实现该目标?
你想要 data
,而不是 deps
。 deps
用于链接到二进制文件的内容,data
用于二进制文件在 运行 时使用的内容。
一旦您的二进制文件在 data
中,请使用 runfiles.go 到 运行 它。将 @io_bazel_rules_go//go/tools/bazel:go_default_library
添加到 deps
(这是您要链接的库),然后使用 FindBinary
查找路径。
我正在使用 bazel 运行 在我的 CI 系统上进行 go 测试。 go 测试将需要使用从另一个 go 文件构建的辅助二进制文件。
目录结构如下所示:
some/of/my/path
├── BUILD.bazel
├── my_utils.go
├── my_test.go
└── my_tool
├── BUILD.bazel
└── my_tool.go
即my_test.go
中的代码将使用从目录 my_tool
.
在非 bazel 的情况下,我可以 go build ./mytool
获取 my_test.go
需要的可执行二进制文件。为了适应这个 bazel 世界,我在 bazel 文件中写了以下内容:
- 在
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"],
)
所以我的想法是当我点击 bazel run
时,它将构建 my_tool
go 二进制文件,go test my_test
可以使用它(参见 deps
字段围棋测试)。
在 CI 我遇到了这个错误:
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
但我的目标是使用二进制文件。我应该如何更改配置以实现该目标?
你想要 data
,而不是 deps
。 deps
用于链接到二进制文件的内容,data
用于二进制文件在 运行 时使用的内容。
一旦您的二进制文件在 data
中,请使用 runfiles.go 到 运行 它。将 @io_bazel_rules_go//go/tools/bazel:go_default_library
添加到 deps
(这是您要链接的库),然后使用 FindBinary
查找路径。