Mailing list: bazel-go-discuss
Slack: #go on Bazel Slack, #bazel on Go Slack
These rules support:
They currently do not support or have limited support for:
The Go rules are tested and supported on the following host platforms:
Users have reported success on several other platforms, but the rules are only tested on those listed above.
Note: Since version v0.51.0, rules_go requires Bazel ≥ 6.5.0 to work.
The master
branch is only guaranteed to work with the latest version of Bazel.
To build Go code with Bazel, you will need:
patch
, cat
, and a handful of other Unix tools in PATH
.You normally won't need a Go toolchain installed. Bazel will download one.
See Using rules_go on Windows for Windows-specific setup instructions. Several additional tools need to be installed and configured.
If you're new to Bazel, read Bazel Tutorial: Build a Go Project, which introduces Bazel concepts and shows you how to set up a small Go workspace to be built with Bazel.
For a quicker "hello world" example, see examples/hello.
For an example that generates build files and retrieves external dependencies using Gazelle, see examples/basic_gazelle.
For more detailed Bzlmod documentation, see Go with Bzlmod.
For legacy WORKSPACE
instructions, see Go with WORKSPACE.
Go
Protocol buffers
Dependencies and testing
Can I still use the go command?Yes, but not directly.
rules_go invokes the Go compiler and linker directly, based on the targets described with go_binary and other rules. Bazel and rules_go together fill the same role as the go
command, so it's not necessary to use the go
command in a Bazel workspace.
That said, it's usually still a good idea to follow conventions required by the go
command (e.g., one package per directory, package paths match directory paths). Tools that aren't compatible with Bazel will still work, and your project can be depended on by non-Bazel projects.
If you need to use the go
command to perform tasks that Bazel doesn't cover (such as adding a new dependency to go.mod
), you can use the following Bazel invocation to run the go
binary of the Bazel-configured Go SDK:
bazel run @io_bazel_rules_go//go -- <args>
Prefer this to running go
directly since it ensures that the version of Go is identical to the one used by rules_go.
Yes, but not directly. Bazel ignores go.mod
files, and all package dependencies must be expressed through deps
attributes in targets described with go_library and other rules.
You can download a Go module at a specific version as an external repository using go_repository, a workspace rule provided by gazelle. This will also generate build files using gazelle.
You can import go_repository rules from a go.mod
file using gazelle update-repos.
This was used to keep import paths consistent in libraries that can be built with go build
before the importpath
attribute was available.
In order to compile and link correctly, rules_go must know the Go import path (the string by which a package can be imported) for each library. This is now set explicitly with the importpath
attribute. Before that attribute existed, the import path was inferred by concatenating a string from a special go_prefix
rule and the library's package and label name. For example, if go_prefix
was github.com/example/project
, for a library //foo/bar:bar
, rules_go would infer the import path as github.com/example/project/foo/bar/bar
. The stutter at the end is incompatible with go build
, so if the label name was go_default_library
, the import path would not include it. So for the library //foo/bar:go_default_library
, the import path would be github.com/example/project/foo/bar
.
Since go_prefix
was removed and the importpath
attribute became mandatory (see #721), the go_default_library
name no longer serves any purpose. We may decide to stop using it in the future (see #265).
You can cross-compile by setting the --platforms
flag on the command line. For example:
$ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd
By default, cgo is disabled when cross-compiling. To cross-compile with cgo, add a _cgo
suffix to the target platform. You must register a cross-compiling C/C++ toolchain with Bazel for this to work.
$ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo //cmd
Platform-specific sources with build tags or filename suffixes are filtered automatically at compile time. You can selectively include platform-specific dependencies with select
expressions (Gazelle does this automatically).
go_library( name = "foo", srcs = [ "foo_linux.go", "foo_windows.go", ], deps = select({ "@io_bazel_rules_go//go/platform:linux_amd64": [ "//bar_linux", ], "@io_bazel_rules_go//go/platform:windows_amd64": [ "//bar_windows", ], "//conditions:default": [], }), )
To build a specific go_binary target for a target platform or using a specific golang SDK version, use the go_cross_binary rule. This is useful for producing multiple binaries for different platforms in a single build.
To build a specific go_test target for a target platform, set the goos
and goarch
attributes on that rule.
You can equivalently depend on a go_binary or go_test rule through a Bazel configuration transition on //command_line_option:platforms
(there are problems with this approach prior to rules_go 0.23.0).
Bazel executes tests in a sandbox, which means tests don't automatically have access to files. You must include test files using the data
attribute. For example, if you want to include everything in the testdata
directory:
go_test( name = "foo_test", srcs = ["foo_test.go"], data = glob(["testdata/**"]), importpath = "github.com/example/project/foo", )
By default, tests are run in the directory of the build file that defined them. Note that this follows the Go testing convention, not the Bazel convention followed by other languages, which run in the repository root. This means that you can access test files using relative paths. You can change the test directory using the rundir
attribute. See go_test.
Gazelle will automatically add a data
attribute like the one above if you have a testdata
directory unless it contains buildable .go files or build files, in which case, testdata
is treated as a normal package.
Note that on Windows, data files are not directly available to tests, since test data files rely on symbolic links, and by default, Windows doesn't let unprivileged users create symbolic links. You can use the github.com/bazelbuild/rules_go/go/tools/bazel library to access data files.
How do I access go_binary executables from go_test?The location where go_binary
writes its executable file is not stable across rules_go versions and should not be depended upon. The parent directory includes some configuration data in its name. This prevents Bazel's cache from being poisoned when the same binary is built in different configurations. The binary basename may also be platform-dependent: on Windows, we add an .exe extension.
To depend on an executable in a go_test
rule, reference the executable in the data
attribute (to make it visible), then expand the location in args
. The real location will be passed to the test on the command line. For example:
go_binary( name = "cmd", srcs = ["cmd.go"], ) go_test( name = "cmd_test", srcs = ["cmd_test.go"], args = ["$(location :cmd)"], data = [":cmd"], )
See //tests/core/cross for a full example of a test that accesses a binary.
Alternatively, you can set the out
attribute of go_binary to a specific filename. Note that when out
is set, the binary won't be cached when changing configurations.
go_binary( name = "cmd", srcs = ["cmd.go"], out = "cmd", ) go_test( name = "cmd_test", srcs = ["cmd_test.go"], data = [":cmd"], )How do I avoid conflicts with protocol buffers?
See Avoiding conflicts in the proto documentation.
Can I use a vendored gRPC with go_proto_library?This is not supported. When using go_proto_library with the @io_bazel_rules_go//proto:go_grpc
compiler, an implicit dependency is added on @org_golang_google_grpc//:go_default_library
. If you link another copy of the same package from //vendor/google.golang.org/grpc:go_default_library
or anywhere else, you may experience conflicts at compile or run-time.
If you're using Gazelle with proto rule generation enabled, imports of google.golang.org/grpc
will be automatically resolved to @org_golang_google_grpc//:go_default_library
to avoid conflicts. The vendored gRPC should be ignored in this case.
If you specifically need to use a vendored gRPC package, it's best to avoid using go_proto_library
altogether. You can check in pre-generated .pb.go files and build them with go_library
rules. Gazelle will generate these rules when proto rule generation is disabled (add # gazelle:proto disable_global
to your root build file).
See Overriding dependencies for instructions on overriding repositories declared in go_rules_dependencies.
How do I test a beta version of the Go SDK?rules_go only supports official releases of the Go SDK. However, you can still test beta and RC versions by passing a version
like "1.16beta1"
to go_register_toolchains. See also go_download_sdk.
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") go_rules_dependencies() go_register_toolchains(version = "1.17beta1")
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4