Skip to content

Commit

Permalink
Generate import paths in accordance with the standard Go package layout.
Browse files Browse the repository at this point in the history
If a .proto imports "a/b/c/foo.proto", we assume that will generate
"a/b/c/foo.pb.go". Previously we would try to import that as "a/b/c/foo.pb",
which makes no sense if this is a standard Go source layout, so we switch
to trying to import that as "a/b/c". The `M` parameter permits overridding this
anyway.

Fixes #8.
  • Loading branch information
dsymonds committed Apr 29, 2015
1 parent e228b1a commit dded913
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions protoc-gen-go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,26 +1161,25 @@ func (g *Generator) generateImports() {
continue
}
filename := goFileName(s)
// By default, import path is the dirname of the Go filename.
importPath := path.Dir(filename)

This comment has been minimized.

Copy link
@YijinLiu

YijinLiu Apr 30, 2015

What if filename is like "test.proto" (w/o a dir name)?

Try to run this to see whether it works:
// dep.proto
package proto;
message Dep {
required int i = 1;
};
// test.proto
import "dep.proto"
package proto;
message Test {
optional Dep dep = 1;
}

protoc --go_out=. dep.proto
protoc --go_out=. test.proto

This comment has been minimized.

Copy link
@dsymonds

dsymonds Apr 30, 2015

Author Contributor

Yes, that won't work (it'll generate import dep "."). This isn't designed for every case; you should either put different protos in their own directory (when they are in different packages), or compile them together (in which case the generated code won't have imports). The M flag can override import paths if there's truly a need for independent .proto files to be in the same dir.

if substitution, ok := g.ImportMap[s]; ok {
filename = substitution
}
filename = g.ImportPrefix + filename
if strings.HasSuffix(filename, ".go") {
filename = filename[0 : len(filename)-3]
importPath = substitution
}
importPath = g.ImportPrefix + importPath
// Skip weak imports.
if g.weak(int32(i)) {
g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(filename))
g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(importPath))
continue
}
if _, ok := g.usedPackages[fd.PackageName()]; ok {
g.P("import ", fd.PackageName(), " ", strconv.Quote(filename))
g.P("import ", fd.PackageName(), " ", strconv.Quote(importPath))
} else {
// TODO: Re-enable this when we are more feature-complete.
// For instance, some protos use foreign field extensions, which we don't support.
// Until then, this is just annoying spam.
//log.Printf("protoc-gen-go: discarding unused import from %v: %v", *g.file.Name, s)
g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(filename))
g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(importPath))
}
}
g.P()
Expand Down
6 changes: 3 additions & 3 deletions protoc-gen-go/plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
regenerate:
echo WARNING! THIS RULE IS PROBABLY NOT RIGHT FOR YOUR INSTALLATION
cd $(HOME)/src/protobuf/src && \
protoc --go_out=. ./google/protobuf/compiler/plugin.proto && \
cat ./google/protobuf/compiler/plugin.pb.go | \
sed '/^import/s;google/protobuf/descriptor.pb;github.com/golang/protobuf/protoc-gen-go/descriptor;' > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go
protoc --go_out=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. \
./google/protobuf/compiler/plugin.proto && \
cat ./google/protobuf/compiler/plugin.pb.go > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go

restore:
cp plugin.pb.golden plugin.pb.go
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/my_test/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/my_test/test.pb.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package my_test
import proto "github.com/golang/protobuf/proto"
import math "math"

// discarding unused import multitest2 "multi/multi1.pb"
// discarding unused import multitest2 "multi"

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
Expand Down

0 comments on commit dded913

Please sign in to comment.