Skip to content

go_binary: Add support to set a default GODEBUG per binary #4168

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zecke
Copy link
Contributor

@zecke zecke commented Nov 9, 2024

Go supports setting default GODEBUG values in the go.mod file or per directive in the main package at the right location. This currently does not work with rules_go/gazelle.

Introduce an explicit godebug_default attribute on go_binary so that gazelle can easily manage it.

Supports bazel-contrib/bazel-gazelle!1945

What type of PR is this?

Uncomment one line below and remove others.

Bug fix
Feature
Documentation
Other

What does this PR do? Why is it needed?

Which issues(s) does this PR fix?

Fixes #

Other notes for review

Go supports setting default GODEBUG values in the go.mod file or per
directive in the main package at the right location. This currently does
not work with rules_go/gazelle.

Introduce an explicit godebug_default attribute on go_binary so that
gazelle can easily manage it.

Supports bazel-contrib/bazel-gazelle!1945
Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, but I think that an attribute on go_binary is the wrong location for this feature. Having potentially different semantics for the same line of code depending on the downstream consumer it is built for seems like too much of a foot gun.

Go language version and GODEBUG could be made configurable on a Go SDK and, with Bzlmod, directly via go.mod.

@zecke
Copy link
Contributor Author

zecke commented Nov 10, 2024

Thanks for working on this, but I think that an attribute on go_binary is the wrong location for this feature. Having potentially different semantics for the same line of code depending on the downstream consumer it is built for seems like too much of a foot gun.

My idea was to handle this like //go:embed. Have gazelle parse the .go files files and generate the go_binary based on the directives observed. Do you think something like rules_go's builder should scan the .go file to extract the directives from the main package?

//go:debug http2server=0
package main

const foo = 123

@abhishekmunie
Copy link

abhishekmunie commented Jun 24, 2025

It would be ideal if developers could define these settings using the mechanisms described in https://go.dev/doc/godebug, and have Gazelle automatically translate them into the appropriate Bazel configurations. Currently, this inconsistency leads to confusion, as behavior differs between using the Go toolchain directly and using Bazel.

This discrepancy is also hindering migration to Bazel—features that previously worked under CMake are now breaking. If //go:debug is not the correct or intended approach, that should be clarified and addressed in the Go community, ideally through discussions in the appropriate forums.

Ultimately, rules_go should aim for feature parity with the standard Go toolchain to ensure a smooth and predictable developer experience.

ref: bazel-contrib/bazel-gazelle#1945

@fmeum
Copy link
Member

fmeum commented Jun 24, 2025

Support for godebug directives could be added to Gazelle's go_deps extension. //go:debug lines would ideally be picked up by the Go compiler/linker (or rules_go's wrapper around them) without Gazelle even being involved.

Happy to support any contributions in this area. The problem with feature parity with native Go is that many of these features are spread across the internals of the Go compiler and the Go build system and these are not covered by backwards compatibility guarantees or even official documentation.

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 this pull request may close these issues.

3 participants