如何在 go:embed 中使用 genrules

How to use genrules with go:embed

我需要在二进制文件中包含一些生成的文件(例如 go-swagger 的输出)。如果没有 bazel,您可以将 go:generatego:embed:

结合使用
package demo
//go:generate swagger generate spec -w . -o ./openapi.json

import "embed"

// Content contains openapi.json file generated via go-swagger from spec
//go:embed openapi.json
var Content embed.FS

我正在尝试用 bazel 做同样的事情。作为一个简单的测试,我有这个 BUILD.bazel 文件:

genrule(
    name = "hellodata",
    srcs = ["hello.go"],
    outs = ["hello.txt"],
    cmd = "cat $(SRCS) | tr A-Za-z N-ZA-Mn-za-m > $@",
)

go_library(
    name = "hello",
    srcs = ["hello.go"],
    importpath = "wiggy.net/hello",
    visibility = ["//visibility:public"],
    embedsrcs = [":hellodata"],
)

hello.go 看起来像这样:

package hello

import (
    _ "embed"
    "io"
)

//go:embed hello.txt
var greeting []byte

func Hello(out io.Writer) error {
    _, err := out.Write(greeting)
    return err
}

这里的目的是让Hello输出它自己来源的rot13。当我尝试编译它时,它成功生成了 hello.txt(位于 bazel-out/darwin_arm64-fastbuild/bin/hello.txt),但编译器找不到它:

❯ bazel build //...                              
INFO: Analyzed 5 targets (0 packages loaded, 0 targets configured).
INFO: Found 5 targets...
ERROR: /Users/wichert/Hack/bzl/BUILD.bazel:14:11: GoCompilePkg hello.a failed: (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_arm64 -src hello.go -embedsrc bazel-out/darwin_arm64-fastbuild/bin/hello.txt -importpath wiggy.net/hello ... (remaining 12 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: /private/var/tmp/_bazel_wichert/e7573342ee9452df4c3dfa671d399a16/sandbox/darwin-sandbox/76/execroot/__main__/hello.go:8:12: could not embed hello.txt: no matching files found
INFO: Elapsed time: 0,112s, Critical Path: 0,04s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

我注意到命令行中的 -embedsrc bazel-out/darwin_arm64-fastbuild/bin/hello.txt 是您问题中失败的操作,所以凭直觉我在 machine 上尝试了等效操作:

//go:embed bazel-out/k8-fastbuild/bin/hello.txt
var greeting []byte

这似乎奏效了。

这不是很好,因为配置信息嵌入在源文件中(实际上,在您的 mac machine 上它是 darwin_arm64-fastbuild 而在我的 linux machine 它是 k8-fastbuild),所以现在您的源代码人为地依赖于平台。

看起来这个功能比较新 (https://github.com/bazelbuild/rules_go/issues/2775, https://github.com/bazelbuild/rules_go/issues/2986)。我会就此向 rules_go 提出问题。

似乎还有 go_embed_data 可能表现不同:

https://github.com/bazelbuild/rules_go/blob/master/docs/go/extras/extras.md#go_embed_data

回答我自己的问题:诀窍是使用 genrule 生成要嵌入的文件,然后使用 go_embed_data 嵌入它们。作品BUILD.bazel是这样的:

genrule(
    name = "hellodata",
    srcs = ["hello.go"],
    outs = ["hello.txt"],
    cmd = "cat $(SRCS) | tr A-Za-z N-ZA-Mn-za-m > $@",
)

go_embed_data(
    name = "hello_embed",
    src = ":hellodata",
    package = "hello",
    var = "greeting",
)

go_library(
    name = "hello",
    srcs = [
        "hello.go",
        ":hello_embed",
    ],
    importpath = "wiggy.net/hello",
    visibility = ["//visibility:public"],
)