diff --git a/protoc-gen-twirp/go_naming.go b/protoc-gen-twirp/go_naming.go index 3716f6bd..5bacbe97 100644 --- a/protoc-gen-twirp/go_naming.go +++ b/protoc-gen-twirp/go_naming.go @@ -82,7 +82,7 @@ func (t *twirp) goFileName(f *descriptor.FileDescriptorProto) string { // otherwise, the directory is taken from the option go_package if impPath, _, ok := goPackageOption(f); ok && impPath != "" { if t.modulePrefix != "" { - impPath = strings.TrimPrefix(impPath, t.modulePrefix) + impPath = strings.TrimPrefix(strings.TrimPrefix(impPath, t.modulePrefix), "/") } // Replace the existing dirname with the import path from go_package diff --git a/protoc-gen-twirp/go_naming_test.go b/protoc-gen-twirp/go_naming_test.go index 6e288e04..c721860c 100644 --- a/protoc-gen-twirp/go_naming_test.go +++ b/protoc-gen-twirp/go_naming_test.go @@ -67,3 +67,40 @@ func TestGoPackageOption(t *testing.T) { testcase("github.com/example/foo;bar", "github.com/example/foo", "bar", true)) t.Run("non dotted import with package", testcase("foo;bar", "foo", "bar", true)) } + +func TestGoFileName(t *testing.T) { + testcase := func(srcrelpaths bool, modprefix, fname, gopkg, wantName string) func(t2 *testing.T) { + return func(t *testing.T) { + f := &descriptor.FileDescriptorProto{ + Name: &fname, + Options: &descriptor.FileOptions{ + GoPackage: &gopkg, + }, + } + + tw := &twirp{ + sourceRelativePaths: srcrelpaths, + modulePrefix: modprefix, + } + + if name := tw.goFileName(f); name != wantName { + t.Errorf("wrong goFileName, have=%q want=%q", name, wantName) + } + } + } + + t.Run("paths=source_relative", + testcase(true, "", + "rpc/v1/service.proto", "example.com/module/package/rpc/v1", + "rpc/v1/service.twirp.go")) + + t.Run("paths=import,module=example.com/module/package", + testcase(false, "example.com/module/package", + "rpc/v1/service.proto", "example.com/module/package/rpc/v1", + "rpc/v1/service.twirp.go")) + + t.Run("paths=import,module=example.com/module/package/", + testcase(false, "example.com/module/package/", + "rpc/v1/service.proto", "example.com/module/package/rpc/v1", + "rpc/v1/service.twirp.go")) +}