rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Can not use genrule with go:embed

Open wichert opened this issue 4 years ago • 7 comments

What version of rules_go are you using?

v0.29.0

What version of gazelle are you using?

v0.24.0

What version of Bazel are you using?

Build label: 4.2.2-homebrew
Build target: bazel-out/darwin_arm64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jan 1 00:00:00 1980 (315532800)
Build timestamp: 315532800
Build timestamp as int: 315532800

Does this issue reproduce with the latest releases of all the above?

Yes, I am using the latest releases.

What operating system and processor architecture are you using?

macOS 12.1 Monterey / M1 Max

Any other potentially useful information about your toolchain?

Go version 1.17

What did you do?

I am trying to embed files that are generated. A minimal case to reproduce it is this BUILD.bazel file:

genrule(
    name = "hellodata",
    srcs = ["hello.go"],
    outs = ["hello.txt"],
    cmd = "cat $(SRCS) | tr A-Za-z N-ZA-Mn-za-m > $@",
)

go_library(
    name = "hello",
    srcs = ["hello.go"],
    importpath = "wiggy.net/hello",
    visibility = ["//visibility:public"],
    embedsrcs = [":hellodata"],
)

with hello.go like this:

package hello

import (
    _ "embed"
    "io"
)

//go:embed hello.txt
var greeting []byte

func Hello(out io.Writer) error {
    _, err := out.Write(greeting)
    return err
}

What did you expect to see?

I expect to be able to generate hello.txt, and embed that in my Go source.

What did you see instead?

A build error:

❯ bazel build //...                              
INFO: Analyzed 5 targets (0 packages loaded, 0 targets configured).
INFO: Found 5 targets...
ERROR: /Users/wichert/Hack/bzl/BUILD.bazel:14:11: GoCompilePkg hello.a failed: (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_arm64 -src hello.go -embedsrc bazel-out/darwin_arm64-fastbuild/bin/hello.txt -importpath wiggy.net/hello ... (remaining 12 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: /private/var/tmp/_bazel_wichert/e7573342ee9452df4c3dfa671d399a16/sandbox/darwin-sandbox/76/execroot/__main__/hello.go:8:12: could not embed hello.txt: no matching files found
INFO: Elapsed time: 0,112s, Critical Path: 0,04s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

This happens because the output file is created in bazel-out/darwin_arm64-fastbuild/bin, which I can not reference in the go:embed directive (without making that very system-specific and unportable).

wichert avatar Jan 07 '22 08:01 wichert

I think you want embedsrcs = ["hello.txt"]

jacobbogdanov avatar Jan 27 '22 16:01 jacobbogdanov

hey @wichert did you find a solution to your problem? I am facing a, how i think, simular issue 3119. Mine is kind of building on top of yours. I am trying to embed files that are generated by a nodejs bazel rule.

Kruemelmann avatar May 20 '22 11:05 Kruemelmann

I think I did, but I'm afraid I don't remember how exactly. We ended up not using bazel for that project because the learning curve was too steep for us.

wichert avatar May 31 '22 18:05 wichert

I suspect the trick was to use go_embed_data, and include the result as a dep.

wichert avatar May 31 '22 19:05 wichert

Thank you so much i will try this :)

Kruemelmann avatar May 31 '22 19:05 Kruemelmann

We do have a passing test that demonstrates exactly this.

It embeds a genrule into a go_test (which under the covers behaves exactly like a go_binary or go_library for these purposes).

https://github.com/bazelbuild/rules_go/blob/3e49c5ec6c467b30e10ea7172ea3bfa9ef0e6efa/tests/core/go_library/BUILD.bazel#L114-L119

and then embeds it in the test

https://github.com/bazelbuild/rules_go/blob/3e49c5ec6c467b30e10ea7172ea3bfa9ef0e6efa/tests/core/go_library/embedsrcs_test.go#L25-L38

and makes assertions on it throughout the rest of the file. I don't believe this is a standing issue in the codebase. Can you show me what I'm missing here?

achew22 avatar May 31 '22 19:05 achew22

As I see it today the functionality is there, but some more tutorial-style documentation would be incredibly helpful.

wichert avatar May 31 '22 20:05 wichert