我将以下 bazel BUILD 配置为
gazelle(name = "gazelle")
go_embed_data(
name = "static_files",
srcs = glob(["static/**/*"]),
package = "main",
var = "staticFS",
)
go_library(
name = "kmdr_lib",
srcs = ["main.go"],
importpath = "github.com/myorg/myrepo",
visibility = ["//visibility:private"],
deps = [
"//api",
"//cmd",
],
)
使用以下嵌入标签
package main
import (
"embed"
"github.com/myorg/myrepo/api"
"github.com/myorg/myrepo/cmd"
)
//go:embed static/*
var staticFS embed.FS
func main() {
api.StaticFS = staticFS
cmd.Execute()
}
但是,当运行时...
bazel run //:gazelle
bazel build //...
我收到以下错误,指出 go 主包中标记的静态文件无法匹配。
ERROR: GoCompilePkg kmdr_osx_amd64.a failed: (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_amd64 -src main.go -arc ... (remaining 17 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: baf0dff8fcdeaf74ff5ba5ff8921e77f/sandbox/linux-sandbox/425/execroot/__main__/main.go:22:12: could not embed static/*: no matching files found
INFO: Elapsed time: 1.249s, Critical Path: 0.15s
INFO: 5 processes: 5 internal.
FAILED: Build did NOT complete successfully
go_embed_data
文档没有提供有关如何使用该库的详细信息。我还尝试引用 go_library
src 中的 :static_files
但是,瞪羚重写了它。
如果我在库 src 中引用 go_emebed_data,bazel 将重写 go_library
go_embed_data generates a .go file that contains data from a file or a list of files. It should be consumed in the srcs list of one of the core go rules.
go_library(
name = "kmdr_lib",
srcs = ["main.go", ":static_files"],
importpath = "github.com/myorg/myrepo",
visibility = ["//visibility:private"],
deps = [
"//api",
"//cmd",
],
)
编辑:
go build
将解析标签并按预期嵌入数据
最佳答案
看起来 go_embed_data
不是正确的方法。
有一个 PR 可以解决此问题 https://github.com/bazelbuild/rules_go/pull/2806#issuecomment-784690934
将 embedsrcs
添加到您的 go_library
将遵循 go:embed
指令
关于go - 巴泽尔去嵌入数据 "could not embed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68459521/