登录
首页 >  Golang >  Go问答

嵌入数据时的bazel go_embed_data问题

来源:stackoverflow

时间:2024-02-20 14:27:26 311浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《嵌入数据时的bazel go_embed_data问题》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我将以下 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 生成一个 .go 文件,其中包含文件或文件列表中的数据。它应该在核心 go 规则之一的 srcs 列表中使用。

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 指令

到这里,我们也就讲完了《嵌入数据时的bazel go_embed_data问题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>