Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted files generated when have external import and set all=true #100

Open
huangjunwen opened this issue Aug 1, 2018 · 4 comments
Open

Comments

@huangjunwen
Copy link

Here's an example .proto with "timestamp.proto" imported:

syntax = "proto3";

package timesvc;

import "google/protobuf/timestamp.proto";

message NowReq {}

message NowReply {
        google.protobuf.Timestamp now = 1;
}

service TimeSvc {
        rpc Now(NowReq) returns (NowReply) {}
}

And template looks like:

$ ls
{{.File.Package}}.xx.go.tmpl

After running

$  protoc --go_out=. --gotemplate_out=all=true,template_dir=.:. *.proto
$ ls
{{.File.Package}}.xx.go.tmpl  google.protobuf.xx.go  timesvc.pb.go  timesvc.proto  timesvc.xx.go

A 'google.protobuf.xx.go' is generated as well. And i don't know how to avoid generating it.

@jhayotte
Copy link
Collaborator

jhayotte commented Aug 1, 2018

Instead of {{.File.Package}} use {{.File.Name}} and you will get:
{{.File.Name}}.xx.go.tmpl timesvc.pb.go timesvc.proto timesvc.xx.go

@huangjunwen
Copy link
Author

huangjunwen commented Aug 1, 2018

Hi, but i get this:

$ tree
.
├── google
│   └── protobuf
│       └── timestamp.proto.xx.go
├── timesvc.pb.go
├── timesvc.proto
├── timesvc.proto.xx.go
└── {{.File.Name}}.xx.go.tmpl

A "google/protobuf/timestamp.proto.xx.go" is generated

@huangjunwen
Copy link
Author

I think the problem is here in the loop in main.go:

  // Generate the encoders
  log.Printf(">> files to genrate: %#v\n", g.Request.FileToGenerate)
  for _, file := range g.Request.GetProtoFile() {
    log.Printf(">> files and their depencies: %+q %+q\n", *file.Name, *file.Package)
    if all {
      if singlePackageMode {
      ...

Rerun the above commands will get:

protoc --go_out=. --gotemplate_out=all=true,template_dir=.:. *.proto
2018/08/02 10:21:09 >> files to genrate: []string{"timesvc.proto"}
2018/08/02 10:21:09 >> files and their depencies: "google/protobuf/timestamp.proto" "google.protobuf"
2018/08/02 10:21:09 >> files and their depencies: "timesvc.proto" "timesvc"

The loop should ignore files that's not in g.Request.FileToGenerate, as described in comment:

// The .proto files that were explicitly listed on the command-line. The
// code generator should generate code only for these files. Each file's
// descriptor will be included in proto_file, below.
FileToGenerate []string protobuf:"bytes,1,rep,name=file_to_generate,json=fileToGenerate" json:"file_to_generate,omitempty"

@huangjunwen
Copy link
Author

Hi, can anybody look into this ? Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants