Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goexperiment.x build tag doesn't work #3555

Closed
lbcjbb opened this issue May 10, 2023 · 0 comments · Fixed by #3556
Closed

goexperiment.x build tag doesn't work #3555

lbcjbb opened this issue May 10, 2023 · 0 comments · Fixed by #3556

Comments

@lbcjbb
Copy link
Contributor

lbcjbb commented May 10, 2023

What version of rules_go are you using?

0.39.1

What version of gazelle are you using?

None

What version of Bazel are you using?

6.1.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?

No

What did you do?

Build a go binary using a Go SDK with a experimental feature and using a Go source file with the related goexperiment.x build flag.

Example with boringcrypto experimental feature:

WORKSPACE (but same issue with bzlmod):

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_register_toolchains", "go_rules_dependencies")

go_download_sdk(
    name = "go_sdk",
    experiments = ["boringcrypto"],
    version = "1.20.4",
)

go_rules_dependencies()

go_register_toolchains()

myapp/BUILD.bazel

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
    name = "myapp_lib",
    srcs = ["main.go"],
    importpath = "example.com/myapp",
    visibility = ["//visibility:private"],
)

go_binary(
    name = "myapp",
    embed = [":myapp_lib"],
    visibility = ["//visibility:public"],
)

myapp/main.go

//go:build goexperiment.boringcrypto

package main

func main() {}

Building myapp binary with this command below:

bazel build //myapp

What did you expect to see?

...
Target //myapp:myapp up-to-date:
  bazel-bin/myapp/myapp_/myapp
...
INFO: Build completed successfully, ...

What did you see instead?

...
external/go_sdk/pkg/tool/linux_amd64/link: bazel-out/k8-fastbuild/bin/myapp/myapp.a: not package main
link: error running subcommand external/go_sdk/pkg/tool/linux_amd64/link: exit status 2
Target //myapp:myapp failed to build
...

The main.go Go source file is ignored due to the build constraint inside the file.
The Go library archive file is empty, which causes the failure during the binary link command.

fmeum pushed a commit that referenced this issue May 17, 2023
The environment variable `GOEXPERIMENT` must be defined at the execution
of the builder. Without that, the default build context `build.Default`
used in the builder to filter files using build constraints will not be
able to filter on a `goexperiment.x` build tag.

Fixes #3555
tingilee pushed a commit to tingilee/rules_go that referenced this issue Jul 19, 2023
The environment variable `GOEXPERIMENT` must be defined at the execution
of the builder. Without that, the default build context `build.Default`
used in the builder to filter files using build constraints will not be
able to filter on a `goexperiment.x` build tag.

Fixes bazelbuild#3555
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant