bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.19k stars 378 forks source link

runtime.godebugDefault handling doesn't work with gazelle/rules_go #1945

Open zecke opened 1 week ago

zecke commented 1 week ago

What version of gazelle are you using?

0.39.1

What version of rules_go are you using?

0.51.0

What version of Bazel are you using?

7.3.2

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

These should be the latest version

What operating system and processor architecture are you using?

AARCH64 and macos

What did you do?

I want to set the default value of a GODEBUG variable. According to the documentation there are three ways to do this:

  1. Use //go:debug somevalue=X before the package declaration
  2. Include it in the go.mod
  3. (Include it in the go.work)

What did you expect to see?

Trying first and second I expected the resulting binary to include the godebugDefault

What did you see instead?

Both the directive and the go.mod file have no impact on compiling the go binary.

More details and other observations

Reproducer set-up

$ cat MODULE.bazel
bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "gazelle", version = "0.39.1")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")

# Download an SDK for the host OS & architecture as well as common remote execution platforms.
go_sdk.download(version = "1.23.1")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

$ cat BUILD.bazel
load("@gazelle//:def.bzl", "gazelle")
load("@rules_go//go:def.bzl", "go_binary", "go_library")

gazelle(name = "gazelle")

go_library(
    name = "godebug_lib",
    srcs = ["main.go"],
    importpath = "github.com/zecke/godebug",
    visibility = ["//visibility:private"],
)

go_binary(
    name = "godebug",
    embed = [":godebug_lib"],
    visibility = ["//visibility:public"],
    #x_defs = {
    #    "runtime.godebugDefault": "tlsrsakex=1",
    #},
)

$ cat main.go
//go:debug tlsrsakex=2
package main

import (
    "fmt"
    "internal/godebug"
)

func main() {
    g := godebug.New("tlsrsakex")
    fmt.Printf("%v is %v\n", g.Name(), g.Value())
}

$ cat go.mod
module github.com/zecke/godebug

go 1.23.1

godebug tlsrsakex=3
zecke commented 1 week ago

Running go build -n -x ./main.go the first occurrence of my debug statement is inside the modinfo statement of the importcfg.link and then this is passed to the linker via the -X=runtime.godebugDefault=...