我正在用bazel在我的CI系统上运行一个go测试。go测试将需要使用从另一个go文件构建的助手二进制文件。
dir结构如下所示:
some/of/my/path
├── BUILD.bazel
├── my_utils.go
├── my_test.go
└── my_tool
├── BUILD.bazel
└── my_tool.gomy_test.go my_tool**.**中的(即中的代码)将使用从dir 构建的可执行二进制文件。
在非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测试my_test可以使用这个二进制文件(参见go测试的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但我的目标是使用二进制。我该如何改变吐露来实现这一点呢?
发布于 2022-01-24 20:06:06
你想要的是data而不是deps。deps是链接到二进制中的东西,data是运行时二进制使用的东西。
一旦您的二进制文件在data中,使用runfiles.go运行它。将@io_bazel_rules_go//go/tools/bazel:go_default_library添加到deps (它是您希望链接的库),然后使用FindBinary查找路径。
https://stackoverflow.com/questions/70818261
复制相似问题