bazel-contrib / bazel-contrib/rules_go

go_proto_library: Issue/New Feature for output path generation.

Open
#3,951 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
760
Avg merge
1d 11h
Merged PRs (30d)
12

Description

Hello, I am unsure whether this is an issue or a new feature request (details follow this introduction). The following describes an issue I encountered with your protobuf plugin when I tried to generate using ConnectRPC's plugin in Bazel 7/bzlmod. Working with the ConnectRPC team, we developed a patch-based solution. I have created a repository for my project with the patch and have donated it to the public domain at https://github.com/abitofhelp/connect-go-example. It would be great if we could add what the patch provides to rules_go. I hope that this information and example code is helpful.

### What version of rules_go are you using?
0.47.1

### What version of gazelle are you using?
0.36.0

### What version of Bazel are you using?
7.2.0

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

### What operating system and processor architecture are you using?
MacOS (Sonoma 14.5), AMD64

### Any other potentially useful information about your toolchain?
Golang 1.22.3
rules_buf 0.2.0

### What did you do?
I created a client/server Go solution that builds with Bazel 7/bzlmod and uses ConnectRPC.
I configured the Bazel build to generate the protobuf file without Connect's proto plugin.
I configured go_proto_compiler to use Connect's plugin to generate the client/server stubs.

### What did you expect to see?
Using Bazel, I generated the protobuf file and it was placed in the correct directory:
* bazel-bin/greet/v1/greetv1_go_proto_/github.com/abitofhelp/connect-go-example/gen/greet/v1/greet.go

Using Bazel, I tried to generate Connect's file in the following location, but nothing was created:
* bazel-bin/greet/v1/greetv1_go_proto_/github.com/abitofhelp/connect-go-example/gen/greet/v1/greetv1connect/greet.connect.go
Notice that Connect's code is in a separate module from the protobuf code, hence it needs to be in a subdirectory for Connect's module.

### What did you see instead?
Using Bazel, I tried to generate Connect's file and nothing was generated.
I contacted Connect's team for assistance debugging the issue.

#########################
## ADDITIONAL INFORMATION
#########################
Working with ConnectRPC's team, we determined rules_go assumes that generated files go into a single directory (i.e. '/gen/greet/v1'). However, if Connect's generated code is in a separate module from Protobuf's code, it must be placed in a subdirectory named as Connect's generated module (greetv1connect). The expected path for the Connect's generated file would be the following:
* bazel-bin/greet/v1/greetv1_go_proto_/github.com/abitofhelp/connect-go-example/gen/greet/v1/greetv1connect/greet.connect.go

In order to support this feature, we created a patch that added a new field to go_proto_library, and it is working properly. The BUILD.bazel file next to the related .proto file has the following go_proto_library blocks. Note the new field: 'overrideimportpath'.

```
# Generate the greet.connect.pb.go file, which imports greet.pb.go.
go_proto_library(
name = "greetv1_connect_go_proto",
compilers = [
":connect_go_proto_compiler",
],
importpath = "github.com/abitofhelp/connect-go-example/gen/greet/v1",
overrideimportpath = "github.com/abitofhelp/connect-go-example/gen/greet/v1/greetv1connect",
proto = ":greetv1_proto",
visibility = ["//visibility:public"],
deps = [":greetv1_go_proto"],
)

# Generate the greet.pb.go file.
go_proto_library(
name = "greetv1_go_proto",
importpath = "github.com/abitofhelp/connect-go-example/gen/greet/v1",
proto = ":greetv1_proto",
visibility = ["//visibility:public"],
)
```
FWIW: When I use protoc or 'buf generate proto' rather than bazel to build the solution, they generate the pb and connect files in the proper directory structure.

Contributor guide

Open the contributing guide

Research direction

Start with the donated connect-go-example repository and its BUILD.bazel go_proto_library declarations, then compare the overrideimportpath patch with rules_go's existing output-path handling. Reproduce the Bazel 7/bzlmod generation case; done means the Connect file is produced under the requested greetv1connect subdirectory alongside the protobuf output.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.