Skip to content

Commit 4c193d1

Browse files
committed
compiler/protogen: relax rules for valid import paths
The path "sub.example.com" is a valid Go import path and should not be rejected. Relax the check to require at least one dot or slash. Either way, it still prevents the situation where a user erroneously treats this option as just the package name. Change-Id: I3df811e1eae61c9d2a0b81c001a27cc7c08c3838 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/316949 Trust: Joe Tsai <[email protected]> Trust: Joe Tsai <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent acaef6a commit 4c193d1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

compiler/protogen/protogen.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,13 @@ func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
251251
"\t• a \"M\" argument on the command line.\n\n"+
252252
"See %v for more information.\n",
253253
fdesc.GetName(), goPackageDocURL)
254-
case !strings.Contains(string(importPaths[filename]), "/"):
255-
// Check that import paths contain at least one slash to avoid a
256-
// common mistake where import path is confused with package name.
254+
case !strings.Contains(string(importPaths[filename]), ".") &&
255+
!strings.Contains(string(importPaths[filename]), "/"):
256+
// Check that import paths contain at least a dot or slash to avoid
257+
// a common mistake where import path is confused with package name.
257258
return nil, fmt.Errorf(
258259
"invalid Go import path %q for %q\n\n"+
259-
"The import path must contain at least one forward slash ('/') character.\n\n"+
260+
"The import path must contain at least one period ('.') or forward slash ('/') character.\n\n"+
260261
"See %v for more information.\n",
261262
string(importPaths[filename]), fdesc.GetName(), goPackageDocURL)
262263
case packageNames[filename] == "":

compiler/protogen/protogen_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ func TestPackageNamesAndPaths(t *testing.T) {
108108
wantImportPath: "golang.org/x/foo",
109109
wantFilename: "golang.org/x/foo/filename",
110110
},
111+
{
112+
desc: "go_package option sets import path without slashes",
113+
goPackageOption: "golang.org;foo",
114+
generate: true,
115+
wantPackageName: "foo",
116+
wantImportPath: "golang.org",
117+
wantFilename: "golang.org/filename",
118+
},
111119
{
112120
desc: "go_package option sets import path and package",
113121
goPackageOption: "golang.org/x/foo;bar",

0 commit comments

Comments
 (0)