Bazel module for linking Go generated sources (protobuf files) into your source directory. This tool solves IDE integration issues when using Bazel-generated Go protobuf files.
Note: This is a maintained fork updated for Bazel 8.4.2+ with bzlmod support. The original project was created by Nikunj Yadav.
Find more details on this post written here
Purpose: Copy golang generated proto files into your source directory so your IDE can find them and your project remains go build compliant.
Note: This is a workaround until better native solutions exist for Bazel/Go/Proto IDE integration.
- Bazel 8.4.2+ (recommended to use Bazelisk)
- Bzlmod enabled (default in Bazel 7+)
- Gazelle for auto-generating build rules
Add this to your MODULE.bazel file:
bazel_dep(name = "go_link", version = "2.0.0")
# If using from a git repository instead:
git_override(
module_name = "go_link",
remote = "https://github.com/zaccari/go_link.git",
commit = "<commit-hash>", # Use the latest commit
)For older Bazel versions using WORKSPACE:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "go_link",
urls = ["https://github.com/zaccari/go_link/archive/v2.0.0.tar.gz"],
sha256 = "<sha256>", # Update with actual SHA256
strip_prefix = "go_link-2.0.0",
)In your root BUILD or BUILD.bazel file:
load("@gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
# Create a custom gazelle binary that includes the go_link plugin
gazelle_binary(
name = "gazelle_binary",
languages = DEFAULT_LANGUAGES + ["@go_link//gazelle/go_link"],
visibility = ["//visibility:public"],
)
# Configure your gazelle target to use the custom binary
# gazelle:prefix github.com/example/your-project
gazelle(
name = "gazelle",
gazelle = ":gazelle_binary",
)Generate BUILD.bazel files and go_proto_link targets:
bazelisk run //:gazelleThis will:
- Generate
BUILD.bazelfiles for your Go and proto code - Automatically create
go_proto_linktargets for eachgo_proto_library - Name them as
<proto_name>_link
After running Gazelle, you'll have go_proto_link targets. Run them to copy generated .pb.go files:
# Copy all proto files in a specific package
bazelisk run //path/to/package:my_proto_link
# Or copy all proto files in your project
bazelisk run //...This copies the generated .pb.go files from Bazel's build directory into your source tree.
Given a proto file at api/v1/user.proto:
-
Run Gazelle:
bazelisk run //:gazelle
-
Gazelle generates in
api/v1/BUILD.bazel:proto_library( name = "user_proto", srcs = ["user.proto"], ) go_proto_library( name = "user_go_proto", proto = ":user_proto", ) # Auto-generated by go_link gazelle plugin go_proto_link( name = "user_go_proto_link", dep = ":user_go_proto", )
-
Copy generated files:
bazelisk run //api/v1:user_go_proto_link
-
Result:
api/v1/user.pb.gois now in your source tree and your IDE can see it!
You can also manually use the go_proto_link rule:
load("@go_link//proto:proto.bzl", "go_proto_link")
go_proto_link(
name = "my_proto_link",
dep = ":my_go_proto_library",
dir = "path/to/output/directory", # Optional, defaults to package directory
)- Bazel generates
.pb.gofiles inbazel-bin/duringgo_proto_librarybuilds go_proto_linkextracts these files from thego_generated_srcsoutput group- Creates an executable script that copies files to your source directory
- Running the target executes the copy operation
- Your IDE and
go buildcan now see the generated files
Q: My IDE still can't find the proto files
- Make sure you've run the
go_proto_linktarget:bazelisk run //package:target_link - Check that
.pb.gofiles exist in your source directory - Restart your IDE/language server
Q: Gazelle isn't generating go_proto_link targets
- Verify you're using the custom
gazelle_binarywith the go_link plugin - Check that you have
go_proto_librarytargets in your BUILD files - Re-run gazelle:
bazelisk run //:gazelle
Q: Build fails with "OutputGroupInfo not found"
- Ensure your
go_proto_libraryrules are up to date - Check that you're using compatible versions of rules_go and protobuf
- Add
MODULE.bazelto your project (if not already using bzlmod) - Update the target reference from
@go_link//gazelle/go_link:go_default_libraryto@go_link//gazelle/go_link - Update any custom uses of go_link rules to use new repository names (
@rules_goinstead of@io_bazel_rules_go) - Remove old WORKSPACE dependencies if fully migrated to bzlmod
Common commands are available via the Makefile:
make help # Show all available commands
make build # Build all targets
make test # Run all tests
make gazelle # Generate/update BUILD files
make update-repos # Update go dependencies
make format # Format bazel files
make clean # Clean bazel artifactsSee the original example commits for a complete setup:
See LICENSE file.