compiler/protogen: avoid suggesting faulty go_package option

In some situations, the full Go package path cannot be determined,
in which case, we should avoid suggesting a go_package option
that is incorrect.

As a heuristic, check whether the proposed path contains at least
a dot before a slash to determine whether it is a full path or not,
which is a simple way to determine whether the first segment is a
top-level domain.

This avoids printing unhelpful warnings like:
	WARNING: Missing 'go_package' option in "foo/bar.proto", please specify:
		option go_package = ".;foo_package";
	A future release of protoc-gen-go will require this be specified.
	See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
and instead prints a warning like:
	WARNING: Missing 'go_package' option in "foo/bar.proto",
	please specify it with the full Go package path as
	a future release of protoc-gen-go will require this be specified.
	See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

We rely on the documentation on the developer website to provide
better guidance.

Change-Id: I38fc4c676d0314ba6d7ad8d5f390fb9e237f2bb1
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/232338
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-05-05 12:01:13 -07:00
parent 0e19395744
commit ce5d8318a0

View File

@ -341,11 +341,21 @@ func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
"\n", fdesc.GetName(), goPkgOpt)
case packageName == "" && importPath == "":
// No Go package information provided.
warn("Missing 'go_package' option in %q, please specify:\n"+
"\toption go_package = %q;\n"+
"A future release of protoc-gen-go will require this be specified.\n"+
"See "+goPackageDocURL+" for more information.\n"+
"\n", fdesc.GetName(), goPkgOpt)
dotIdx := strings.Index(goPkgOpt, ".") // heuristic for top-level domain
slashIdx := strings.Index(goPkgOpt, "/") // heuristic for multi-segment path
if isFull := 0 <= dotIdx && dotIdx <= slashIdx; isFull {
warn("Missing 'go_package' option in %q, please specify:\n"+
"\toption go_package = %q;\n"+
"A future release of protoc-gen-go will require this be specified.\n"+
"See "+goPackageDocURL+" for more information.\n"+
"\n", fdesc.GetName(), goPkgOpt)
} else {
warn("Missing 'go_package' option in %q,\n"+
"please specify it with the full Go package path as\n"+
"a future release of protoc-gen-go will require this be specified.\n"+
"See "+goPackageDocURL+" for more information.\n"+
"\n", fdesc.GetName())
}
}
}