go - 巴泽尔去嵌入数据 "could not embed"

标签 go bazel

我将以下 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/

相关文章:

bazel - 使用 git_repository-bazel 导入私有(private)存储库

Go CPU 配置文件缺少函数调用信息

go - 如何使用 Go 模块修复 Go build error "can' t load package"?

c++ - 如何强制Bazel使用gcc?

c++ - 将拷贝传播到Bazel中的所有依赖项

bazel - 多个 Bazel BUILD 文件出错 : "Target ' bar' is not visible from target 'foo' "

amazon-web-services - 如何使用 Go 跟踪分段上传到 s3 的进度?

go - Martini 渲染在页面上显示 {{ yield }}

vim - vim-go可以补全第三方框架吗

c++ - 使用Bazel为C++配置静态分析或linters