Skip to content

Commit 476a1fe

Browse files
linzhpfmeum
andcommitted
Revert "Fail when expected files are not produced by protoc (#4287)" (#4324)
**What type of PR is this?** Bug fix **What does this PR do? Why is it needed?** It's common for a `go_proto_library` to include proto files with and without service definitions at the same time. In this case, the gRPC plugins are needed, but some gRPC plugins don't generate the grpc.pb.go files if there is no service definition. Partially reverting #4287 to restore the previous behavior of creating an empty file **Which issues(s) does this PR fix?** Fixes #4317 **Other notes for review** --------- Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
1 parent e8bc0cb commit 476a1fe

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

go/tools/builders/protoc.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"errors"
2121
"flag"
2222
"fmt"
23-
"go/parser"
24-
"go/token"
2523
"io/ioutil"
2624
"log"
2725
"os"
@@ -185,9 +183,15 @@ func run(args []string) error {
185183
for _, f := range files {
186184
switch {
187185
case f.expected && !f.created:
188-
return fmt.Errorf("file %q expected from plugin %q but not created", f.path, *plugin)
186+
// Some plugins only create output files if the proto source files
187+
// have relevant definitions (e.g., services for grpc_gateway). Create
188+
// trivial files that the compiler will ignore for missing outputs.
189+
data := []byte("// +build ignore\n\npackage ignore")
190+
if err := ioutil.WriteFile(abs(f.path), data, 0644); err != nil {
191+
return err
192+
}
189193
case f.expected && f.ambiguous:
190-
fmt.Fprintf(buf, "Ambiguous output %v.\n", f.path)
194+
fmt.Fprintf(buf, "ambiguous output %v.\n", f.path)
191195
case f.from != nil:
192196
data, err := ioutil.ReadFile(f.from.path)
193197
if err != nil {
@@ -203,15 +207,6 @@ func run(args []string) error {
203207
fmt.Fprintf(buf, "Check that the go_package option is %q.", *importpath)
204208
return errors.New(buf.String())
205209
}
206-
207-
if filepath.Ext(f.path) != ".go" {
208-
continue
209-
}
210-
// Additionally check that created files are valid, because invalid Go file causes cache poisoning.
211-
_, err := parser.ParseFile(token.NewFileSet(), f.path, nil, parser.PackageClauseOnly)
212-
if err != nil {
213-
return fmt.Errorf("plugin %q created an invalid Go file %q: %v", *plugin, f.path, err)
214-
}
215210
}
216211

217212
return nil

0 commit comments

Comments
 (0)