diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go index de9cb8b0..5cd2a807 100644 --- a/compiler/protogen/protogen.go +++ b/compiler/protogen/protogen.go @@ -191,6 +191,14 @@ func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) { } default: if param[0] == 'M' { + if i := strings.Index(value, ";"); i >= 0 { + pkgName := GoPackageName(value[i+1:]) + if otherName, ok := packageNames[param[1:]]; ok && pkgName != otherName { + return nil, fmt.Errorf("inconsistent package names for %q: %q != %q", value[:i], pkgName, otherName) + } + packageNames[param[1:]] = pkgName + value = value[:i] + } importPaths[param[1:]] = GoImportPath(value) mfiles[param[1:]] = true continue @@ -261,6 +269,8 @@ func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) { packageName, importPath := goPackageOption(fdesc) defaultPackageName := packageNameForImportPath[importPaths[filename]] switch { + case packageNames[filename] != "": + // A package name specified by the "M" command-line argument. case packageName != "": // TODO: For the "M" command-line argument, this means that the // package name can be derived from the go_package option. diff --git a/compiler/protogen/protogen_test.go b/compiler/protogen/protogen_test.go index 1f7b75ce..0967516e 100644 --- a/compiler/protogen/protogen_test.go +++ b/compiler/protogen/protogen_test.go @@ -141,6 +141,15 @@ func TestPackageNamesAndPaths(t *testing.T) { wantImportPath: "golang.org/x/bar", wantFilenamePrefix: "golang.org/x/foo/filename", }, + { + desc: "command line sets import path for a file with package name specified", + parameter: "Mdir/filename.proto=golang.org/x/bar;bar", + goPackageOption: "golang.org/x/foo", + generate: true, + wantPackageName: "bar", + wantImportPath: "golang.org/x/bar", + wantFilenamePrefix: "golang.org/x/foo/filename", + }, { desc: "import_path parameter sets import path of generated files", parameter: "import_path=golang.org/x/bar",