rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

bzlmod: support reading toolchain version from toolchain directive

Open malt3 opened this issue 1 year ago • 2 comments

What version of rules_go are you using?

What version of gazelle are you using?

What version of Bazel are you using?

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

yes

What operating system and processor architecture are you using?

Any other potentially useful information about your toolchain?

What did you do?

Go 1.21 has a new toolchain directive in go.work / go.mod files. rules_go could interpret this directive to choose the version of the go sdk to use.

What did you expect to see?

A possible implementation might look like this:

MODULE.bazel:

bazel_dep(name = "rules_go", version = "0.39.1")
bazel_dep(name = "gazelle", version = "0.31.0")

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

go_sdk.from_file(go_mod = "//:go.mod")

go.mod:

module example.com/module

go 1.21.0

toolchain go1.21.0

In this case, rules_go would register the same toolchain version that is also specified in the go.mod file and there is no potential for a mismatch between using go build and bazel build.

What did you see instead?

rules_go does not yet work with go 1.21. I can only speculate what rules_go may do instead.

malt3 avatar Aug 09 '23 08:08 malt3

This would be a pretty good addition. I like the consistency with go_deps.from_file.

If toolchain isn't specified, should we fall back to using the version in the go line? How does Go behave in this case?

fmeum avatar Sep 25 '23 11:09 fmeum

If toolchain isn't specified, should we fall back to using the version in the go line?

We can do that. We could just make this defined behavior for rules_go.

How does Go behave in this case?

If the GOTOOLCHAIN env var allows downloading a newer SDK, Go also falls back to the go line:

When GOTOOLCHAIN is set to +auto or +path (or the shorthands auto or path), the go command selects and runs a newer Go version as needed. Specifically, it consults the toolchain and go lines in the current workspace’s go.work file or, when there is no workspace, the main module’s go.mod file. If the go.work or go.mod file has a toolchain line and is newer than the default Go toolchain, then the go command runs instead. If the file has a toolchain default line, then the go command runs the default Go toolchain, disabling any attempt at updating beyond . Otherwise, if the file has a go line and is newer than the default Go toolchain, then the go command runs go instead.

(source)

malt3 avatar Sep 25 '23 13:09 malt3