From 01b33a907135850074483e8f3fb4b169cfb80e6c Mon Sep 17 00:00:00 2001 From: Lauri Peltonen Date: Mon, 14 May 2018 21:58:28 +0300 Subject: [PATCH] [protoc-gen-go-jsonpb] Only emit output for requested files As the documentation of "CodeGenerationRequest" message says, the code generator should generate code only for the files listed in the repeated FileToGenerate field. Generating code for other, imported protobuf files will result in multiple bazel targets trying to write the same files under bazel-genfiles, which is not good! (For example, if you have a.proto and b.proto both importing c.proto, and we want to generate .pb.jsonpb.go files for all of them, we don't want a.proto and b.proto generating c.pb.jsonpb.go file). --- protoc-gen-go-jsonpb/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/protoc-gen-go-jsonpb/main.go b/protoc-gen-go-jsonpb/main.go index efb5489..912e55a 100644 --- a/protoc-gen-go-jsonpb/main.go +++ b/protoc-gen-go-jsonpb/main.go @@ -85,7 +85,16 @@ func main() { func generate(req *plugin.CodeGeneratorRequest) ([]*plugin.CodeGeneratorResponse_File, error) { var files []*plugin.CodeGeneratorResponse_File + genFileNames := make(map[string]bool) + for _, n := range req.FileToGenerate { + genFileNames[n] = true + } for _, desc := range req.GetProtoFile() { + name := desc.GetName() + if _, ok := genFileNames[name]; !ok { + // Only emit output for files present in req.FileToGenerate. + continue + } code, err := genCode(desc) if err != nil { return nil, err @@ -96,7 +105,6 @@ func generate(req *plugin.CodeGeneratorRequest) ([]*plugin.CodeGeneratorResponse return nil, err } - name := desc.GetName() ext := filepath.Ext(name) base := strings.TrimSuffix(name, ext) output := fmt.Sprintf("%s.pb.jsonpb.go", base)