bazel go_embed_data “could not embed” 的中文翻译是:bazel go_embed_data “无法嵌入”。

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

bazel go_embed_data "could not embed"

问题

我已经配置了以下的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",
    ],
)

使用以下的embed标签:

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_librarysrcs中引用:static_files,但是gazelle会对其进行重写。

如果我在库的srcs中引用go_embed_data,bazel会重写go_library

go_library(
    name = "kmdr_lib",
    srcs = ["main.go", ":static_files"],
    importpath = "github.com/myorg/myrepo",
    visibility = ["//visibility:private"],
    deps = [
        "//api",
        "//cmd",
    ],
)

编辑:
go build将按预期解析标签并嵌入数据。

英文:

I have the following bazel BUILD configured to

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",
    ],
)

With the following embed tag

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()
}

However, when running...

bazel run //:gazelle
bazel build //...    

I receive the following error that the static files tagged in the go main package could not be matched.

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

The go_embed_data documentation doesn't give much details on how to use the library. I've also tried referencing the :static_files in the go_library srcs however, gazelle rewrites that.

bazel will rewrite the go_library if I reference go_emebed_data in the library srcs

> 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",
    ],
)

EDIT:
go build will parse the tag and embed the data as expected

答案1

得分: 3

看起来go_embed_data不是正确的方法。

有一个PR来解决这个问题https://github.com/bazelbuild/rules_go/pull/2806#issuecomment-784690934

在你的go_library中添加embedsrcs将会遵循go:embed指令。

英文:

It looks like go_embed_data is not the right method.

There was a PR made to address this issue https://github.com/bazelbuild/rules_go/pull/2806#issuecomment-784690934

Adding embedsrcs to your go_library will respect the go:embed directive

huangapple
  • 本文由 发表于 2021年7月21日 02:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/68459521.html
匿名

发表评论

匿名网友

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

确定