-
Notifications
You must be signed in to change notification settings - Fork 16k
Description
What language does this apply to?
C++
Describe the problem you are trying to solve
Go 1.24 introduced Tool dependencies feature. This allows Go projects to refer to protoc-gen-go like this:
module github.com/example/project
go 1.24.0
tool (
google.golang.org/protobuf/cmd/protoc-gen-go
)
require (
google.golang.org/protobuf v1.36.4
)Why is this better?
- Automatic installation of
protoc-gen-go - Version control the version of required
protoc-gen-go - Utilize Go vendoring feature for the
protoc-gen-go
Describe the solution you'd like
TLDR; Before the fallback to Subprocess::SEARCH_PATH, also check if go tool protoc-gen-go is available.
Subprocess selection logic
flowchart TD
A[Looking for protoc-gen-go] --> B{Is protoc-gen-go defined with --plugin option?}
B -->|Yes| C[Use the plugin defined with --plugin option]
B -->|No| D[Run 'go tool' in current working directory]
D --> E[Check go tool output for protoc-gen-go]
E --> F{Is protoc-gen-go found in go tool output?}
F -->|Yes| G[Run: go tool protoc-gen-go]
%% Style the ADDED STAGE nodes
style D stroke:#ff9800,stroke-width:3px,stroke-dasharray: 5 5
style E stroke:#ff9800,stroke-width:3px,stroke-dasharray: 5 5
style F stroke:#ff9800,stroke-width:3px,stroke-dasharray: 5 5
style G stroke:#ff9800,stroke-width:3px,stroke-dasharray: 5 5
F -->|No| H[Look for protoc-gen-go in PATH]
C --> I[Complete]
G --> I
H --> I
Note that go tool will list google.golang.org/protobuf/cmd/protoc-gen-go if it's available:
> go tool
addr2line
asm
cgo
compile
...
google.golang.org/protobuf/cmd/protoc-gen-go
...
Currently devs of Go projects have to go install google.golang.org/protobuf/cmd/protoc-gen-go.
This has a few disadvantages:
- Manual installation for each developer and CI
- Manual version tracking of the
protoc-gen-go, instead of just defining it ingo.mod
Describe alternatives you've considered
There is a workaround.
- Create a
protoc-gen-gowrapper binary/script#!/bin/bash exec go tool protoc-gen-go "$@"
- Use
protoc --plugin=<path-to-wrapper>
Pros:
- No
protocmodifications
Cons:
- Not a cross-platform solution — Windows needs an actual Win32 binary rather than a
.batscript - Each user will have to re-implement this solution on their side. While I anticipate wide usage of
go toolsoon(ish).
Implementation
I can prepare a PR myself. But I would like to agree on the solution in advance.
I see that logic of looking for the language generator in CommandLineInterface::GeneratePluginOutput is very neat. I like it and don't want to pollute it with custom GoLang conditions.
The idea I have on my mind is to implement a common way to add custom way(s) of looking for the generator.
I'm not sure if it's useful for any other languages than Go, but at least it will not be a hack.