Skip to content

Commit

Permalink
compiler/protogen: relax rules for valid import paths
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dsnet committed May 24, 2021
1 parent acaef6a commit 4c193d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions compiler/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
"\t• a \"M\" argument on the command line.\n\n"+
"See %v for more information.\n",
fdesc.GetName(), goPackageDocURL)
case !strings.Contains(string(importPaths[filename]), "/"):
// Check that import paths contain at least one slash to avoid a
// common mistake where import path is confused with package name.
case !strings.Contains(string(importPaths[filename]), ".") &&
!strings.Contains(string(importPaths[filename]), "/"):
// Check that import paths contain at least a dot or slash to avoid
// a common mistake where import path is confused with package name.
return nil, fmt.Errorf(
"invalid Go import path %q for %q\n\n"+
"The import path must contain at least one forward slash ('/') character.\n\n"+
"The import path must contain at least one period ('.') or forward slash ('/') character.\n\n"+
"See %v for more information.\n",
string(importPaths[filename]), fdesc.GetName(), goPackageDocURL)
case packageNames[filename] == "":
Expand Down
8 changes: 8 additions & 0 deletions compiler/protogen/protogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ func TestPackageNamesAndPaths(t *testing.T) {
wantImportPath: "golang.org/x/foo",
wantFilename: "golang.org/x/foo/filename",
},
{
desc: "go_package option sets import path without slashes",
goPackageOption: "golang.org;foo",
generate: true,
wantPackageName: "foo",
wantImportPath: "golang.org",
wantFilename: "golang.org/filename",
},
{
desc: "go_package option sets import path and package",
goPackageOption: "golang.org/x/foo;bar",
Expand Down

0 comments on commit 4c193d1

Please sign in to comment.