bazel-contrib / bazel-contrib/rules_go

Allow setting pure, static, etc. in go_cross_binary rule

Open
#4,131 5 comments 0 reactions 0 assignees View on GitHub
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?

0.50.1

### What version of gazelle are you using?

0.39.0

### What version of Bazel are you using?

7.3.2

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

Yes

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

linux/amd64

### Any other potentially useful information about your toolchain?

### What did you do?

We currently cross build docker image for linux/amd64 by passing in the follow args to the command line:

```bzl
build:docker --@rules_go//go/config:pure --platforms=//:amd64_v1
run:docker --@rules_go//go/config:pure --platforms=//:amd64_v1
```

Because of https://github.com/bazel-contrib/rules_oci/issues/706, and also just that passing in platform args to the command line is just brittle, we are moving away to use `go_cross_binary` to cross build.

Basically we now have `BUILD.bazel` like this:

```bzl
go_binary(
name = "foo_native",
embed = [":foo_lib"],
pure = "on",
visibility = ["//visibility:public"],
)

go_cross_binary(
name = "foo",
platform = "//:amd64_v1",
target = ":foo_native",
visibility = ["//visibility:public"],
)

pkg_tar(
name = "bin",
srcs = [
":foo",
],
mode = "0755",
package_dir = "/bin",
)

oci_image(
name = "docker",
base = "@distroless",
tars = [
":bin",
],
visibility = ["//visibility:public"],
)
```

Since we use distroless, pure mode is very important for us (without it we would have to use the version of distroless with libc instead), but it's not currently allowed on `go_cross_binary`, so we have to set it on `go_binary` instead. It would be great if we can set it on `go_cross_binary`.

Additionally (this could be its own issue), it would be great if we can have an alias/rename attribute allowed in `go_cross_binary` rule. Currently the binary name always come from the rule name, and since we are not allowed to have duplicate rule names, we cannot use it to generate same binary name with different platform cross binary rules (for example, to build multi-arch oci image). Basically ideally we could be able to do something like this instead:

```bzl
go_binary(
name = "foo",
embed = [":foo_lib"],
pure = "on",
visibility = ["//visibility:public"],
)

go_cross_binary(
name = "foo_linux_amd64",
platform = "//:linux_amd64",
target = ":foo",
rename = "foo",
visibility = ["//visibility:public"],
)

go_cross_binary(
name = "foo_linux_arm64",
platform = "//:linux_arm64",
target = ":foo",
rename = "foo",
visibility = ["//visibility:public"],
)

# then have pkg_tar and oci_image rules to build multi-arch image
```

### What did you expect to see?

### What did you see instead?

Contributor guide

Open the contributing guide

Research direction

Start with the go_cross_binary rule and the BUILD.bazel examples in the issue, then compare its available attributes with the pure setting currently applied to go_binary. Check how the rule produces the cross-built binary and define completion around supporting the requested build settings; treat the separate rename/alias request as an additional scope item.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system
Issue type
Feature
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.