Skip to content

Commit f352bcd

Browse files
authored
Document Gazelle go_tools.bzl functionality (#4496)
**What type of PR is this?** > Documentation **What does this PR do? Why is it needed?** Documents the Gazelle `go_tools.bzl` feature added in bazel-contrib/bazel-gazelle#2215. **Which issues(s) does this PR fix?** buildbuddy-io/bazel_env.bzl#68
1 parent b3e12d7 commit f352bcd

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/go/core/bzlmod.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,32 @@ Limitations:
191191
- `go.work` is supported exclusively in the root module.
192192
- Dependencies that are indirect and depend on a go module specified in `go.work` will have that dependency diverge from the one in `go.work`. More details can be found here: https://github.com/bazelbuild/bazel-gazelle/issues/1797.
193193

194-
#### Depending on tools
194+
#### Depending on tools (Go 1.24+)
195+
196+
Go 1.24 introduced the [`tool` directive](https://tip.golang.org/doc/go1.24#tools), allowing you to specify tool dependencies directly in your `go.mod` like so:
197+
```sh
198+
bazel run @rules_go//go -- get -tool golang.org/x/tools/cmd/stringer
199+
```
200+
201+
This will add a `tool` section in your `go.mod`:
202+
```
203+
tool golang.org/x/tools/cmd/stringer
204+
```
205+
as well as adding that tool as a dependency.
206+
207+
If you are using Gazelle >=0.47.0, then the tools you have added are exported as a dictionary named `GO_TOOLS` from `@gazelle//:go_tools.bzl`. This dictionary is in a suitable format for use by [`bazel_env.bzl`](https://github.com/buildbuddy-io/bazel_env.bzl), so you should be able to do the following to get all your repository’s tools into a `bazel_env` target:
208+
```starlark
209+
load("@bazel_env.bzl", "bazel_env")
210+
load("@gazelle//:go_tools.bzl", "GO_TOOLS")
211+
bazel_env(
212+
name = "env",
213+
tools = {
214+
// […]
215+
} | GO_TOOLS,
216+
)
217+
```
218+
219+
#### Depending on tools (pre Go 1.24)
195220

196221
If you need to depend on Go modules that are only used as tools, you can use the [`tools.go` technique](https://github.com/golang/go/issues/25922#issuecomment-1038394599):
197222

0 commit comments

Comments
 (0)