Skip to content

goexperiment.x build tag doesn't work #3555

@ghost

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions