bazel-contrib / bazel-contrib/rules_go

Can not use genrule with go:embed

Open
#3,040 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
760
Avg merge
1d 11h
Merged PRs (30d)
12

Description

### 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:

```bazel
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:

```go
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).

Contributor guide

Open the contributing guide

Research direction

Start with the minimal BUILD.bazel reproduction and hello.go, then run bazel build //... to confirm the failure. Investigate how the go_library embedsrcs attribute handles the genrule output; done means the generated hello.txt can be embedded without a system-specific path.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, shell
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.