Bazel:如何获取我在 bazel test 中构建的二进制文件的路径

huangapple go评论95阅读模式
英文:

Bazel: how to get the path of a binary that I built along a bazel test

问题

我正在使用bazel编写一个go测试。测试将首先构建一个二进制文件,然后将该二进制文件挂载到一个Docker容器中。因此,我需要知道我构建的二进制文件的路径。

文件系统结构如下:

目录结构如下所示:

    some/of/my/path
    ├── BUILD.bazel
    ├── my_test.go
    └── my_tool
        ├── BUILD.bazel
        └── my_tool.go

我想在my_tool目录下构建一个包含go文件的二进制文件。

根据我之前问题的答案,我知道可以使用some/of/my/path/BUILD.bazel中的go_testdata条目来构建二进制文件:

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
    ...
    ],
)

这确实构建了my_tool的go二进制文件。但现在的问题是,在my_test.go中,我如何知道my_tool的构建二进制文件在哪里?

my_test.go中,我需要写类似这样的代码:

package my_lucky_test

...

pathForMyTool := some_path
hostConfig := container.HostConfig{
    Binds := []string {"%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool}
}

我的问题是,我如何获取"my_tool-bin"的绝对路径?

英文:

I'm writing a go test with bazel. The test will build a binary first, and mount the binary to a docker container. So I need to know the path of the binary I built.

The file system structure is like this:

The dir structure looks like the following:

    some/of/my/path
    ├── BUILD.bazel
    ├── my_test.go
    └── my_tool
        ├── BUILD.bazel
        └── my_tool.go

where I'd like to build a go binary with go files at my_tool dir.

From my previous question's answer, I got to know that I can use the data entry of in go_test from some/of/my/path/BUILD.bazel to build the binary:

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
    ...
    ],
)

which did build the go binary for my_tool. But now the question is, how do I know where is the built binary of my_tool in my_test.go?

In my_test.go, I need to write something like this:

package my_lucky_test

...

pathForMyTool := some_path
hostConfig := container.HostConfig{
Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool"}
}

My question is how can I get the absolute path of "my_tool-bin"?

答案1

得分: 1

只返回翻译的部分:

只需运行:

bazel build //some/of/my/path:my_test

它应该将二进制文件的路径打印到标准输出。

作为对你当前方法的替代,我建议你使用bazelbuild/rules_docker#go_image,这是一个用于构建容器的Bazel感知方法。

编辑:OP已经澄清了问题,但我将保留上面的原始答案,因为其他人可能会发现它有用。

当你将二进制文件/测试与一些数据捆绑在一起时,数据属性下的文件将成为一组'runfiles'。每种语言都有各种库来解析runfiles。对于golang,你需要使用rules_go中的bazel pkg来解析你的runfiles。例如:

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
        # 新的依赖项
        "@rules_go//rules_go/go/tools/bazel",
    ...
    ],
)

你的my_test.go文件:

package my_lucky_test

# 新的导入
import(
    "github.com/bazelbuild/rules_go/go/tools/bazel"
)

#...

pathForMyTool, ok := bazel.FindBinary("some/of/my/path/my_tool", "my_tool")
if !ok {
    # 错误处理
}
hostConfig := container.HostConfig{
    Binds := []string {"%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool}
}
英文:

Simply run;

bazel build //some/of/my/path:my_test

It should print out the path of the binary to stdout.

As an alternative to your current approach might I suggest that you make use of bazelbuild/rules_docker#go_image which is a Bazel aware method for building containers.

EDIT: OP has clarified the question, though I am leaving the original answer above as others may find it useful.

When you bundle a binary/test with some data, the files under the data attribute become a set of 'runfiles'. There are various libraries in each language for resolving runfiles. For golang you'll want to use the bazel pkg in rules_go, to resolve your runfiles. e.g.

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
        # NEW dep
        "@rules_go//rules_go/go/tools/bazel",
    ...
    ],
)

Your my_test.go;

package my_lucky_test

# NEW
import(
    "github.com/bazelbuild/rules_go/go/tools/bazel"
)

#...

pathForMyTool, ok := bazel.FindBinary("some/of/my/path/my_tool", "my_tool")
if !ok {
    # Error handling
}
hostConfig := container.HostConfig{
    Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool}
}

huangapple
  • 本文由 发表于 2022年2月15日 10:20:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/71120426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定