2

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

a11hard
  • 1,904
  • 4
  • 19
  • 41
  • The patterns are interpreted relative to the package directory containing the source file. Is the static folder in the same path as your main.go? – Wishwa Perera Jul 20 '21 at 18:30
  • Yes, running `go build` or `go install .` builds normally I'm not sure why the go_library is being rewritten when referencing the embed data "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." Adding more details about this in the main question – a11hard Jul 20 '21 at 18:31
  • I think I understand what is happening better now... `go_embed_data` creates a map of byte[] containing the contents and doesn't actually use the embed directive. That map is in assigned to the named variable. – a11hard Jul 20 '21 at 18:54

1 Answers1

3

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

a11hard
  • 1,904
  • 4
  • 19
  • 41