bazel-contrib / bazel-contrib/rules_go

Clearer error messages when build constraints exclude all sources

Open
#2,824 1 comment 0 reactions 0 assignees View on GitHub
enhancement go
Dominant language
Go
Stars
1.5k
Forks
762
Avg merge
1d 11h
Merged PRs (30d)
12

Description

### What version of rules_go are you using?

v0.25.1

### What version of gazelle are you using?

n/a

### What version of Bazel are you using?

Build label: 3.7.2

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

Yes

### What operating system and processor architecture are you using?

macOS 11.2.1, amd64

### What did you do?

Attempt to build a Go package where [build constraints](https://pkg.go.dev/go/build#hdr-Build_Constraints) exclude all sources by default. I've made a minimal reproduction in [github.com/jamie-digital/rules_go_tests](https://github.com/jamie-digital/rules_go_tests).

### What did you expect to see?

An error when building `//:hello_lib`, as the build constraints (`+build gotags`) excludes all sources listed in the target's `srcs`. This would make it easier to identify the issue.

### What did you see instead?

Building `//:hello_lib` succeeds with the following output:

```
% bazel build :hello_lib
INFO: SHA256 (https://golang.org/dl/?mode=json&include=all) = a6e405e31ea50626c4b0bcf821ba56fd4d7b90c0de5ec32a7c6323c73d7ba48d
INFO: Analyzed target //:hello_lib (36 packages loaded, 7073 targets configured).
INFO: Found 1 target...
Target //:hello_lib up-to-date:
bazel-bin/hello_lib.a
INFO: Elapsed time: 19.630s, Critical Path: 2.77s
INFO: 5 processes: 2 internal, 3 darwin-sandbox.
INFO: Build completed successfully, 5 total actions
```

This effectively produces an empty archive `hello_lib.a`. When that's then used to build `//:hello_broken`, the resulting error message (straight from the Go tooling) is quite confusing:

```
% bazel build :hello_broken
INFO: Analyzed target //:hello_broken (0 packages loaded, 1 target configured).
INFO: Found 1 target...
ERROR: /Users/user/dev/go/src/github.com/jamie-digital/rules_go_tests/BUILD:10:10: GoLink hello_broken_/hello_broken failed (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder link -sdk external/go_sdk -installsuffix darwin_amd64 -package_list bazel-out/host/bin/external/go_sdk/packages.txt -o ... (remaining 11 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder link -sdk external/go_sdk -installsuffix darwin_amd64 -package_list bazel-out/host/bin/external/go_sdk/packages.txt -o ... (remaining 11 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/go_sdk/pkg/tool/darwin_amd64/link: bazel-out/darwin-fastbuild/bin/hello_broken.a: not package main
link: error running subcommand external/go_sdk/pkg/tool/darwin_amd64/link: exit status 2
Target //:hello_broken failed to build
Use --verbose_failures to see the command lines of failed build steps.
```

It took me quite a while to debug "not package main", because it didn't occur to me that the build constraints would be honoured, given that `hello.go` is specified explicitly in `//:hello_lib`.

Applying the right build constraints, `//:hello_working` and `//:hello_working_test` both succeed as expected. `//:hello_broken_test` also fails, but with a slightly clearer error:

```
% bazel test :hello_broken_test
INFO: Analyzed target //:hello_broken_test (8 packages loaded, 361 targets configured).
INFO: Found 1 test target...
ERROR: /Users/user/dev/go/src/github.com/jamie-digital/rules_go_tests/BUILD:23:8: GoCompilePkg hello_broken_test.internal.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 hello.go -src hello_test.go -importpath github.com/jamie-digital/rules_go_tests -p ... (remaining 13 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_amd64 -src hello.go -src hello_test.go -importpath github.com/jamie-digital/rules_go_tests -p ... (remaining 13 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
hello_test.go:18:11: undefined: Hello
compilepkg: error running subcommand external/go_sdk/pkg/tool/darwin_amd64/compile: exit status 2
Target //:hello_broken_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
```

In summary, would it be possible for rules_go to emit an error when build constraints exclude all sources?

On a related point, is there a particular reason why the `gotags` argument is only available in go_binary and go_test, but not go_library? Intuitively I would have expected to apply build constraints at compile time rather than link time. It's something I can work around, but it feels a little odd.

Many thanks for all your hard work on rules_go!

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproduction linked in the issue, especially BUILD and hello.go, and run `bazel build :hello_lib` followed by `bazel build :hello_broken`. Trace how rules_go handles Go build constraints for go_library, go_binary, and go_test; done means a target whose constraints exclude every source reports a clear error instead of producing an empty archive.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.