Repository: zeromicro/go-zero (go)

Issue:
import error if generate multiple proto
**Describe the bug**
当同时生成多个 proto 时，会有 import package 错误的问题，并且与执行的顺序有关

**To Reproduce**
Steps to reproduce the behavior, if applicable:

1. The code is
 - 第一次，先生成 protos/a/v1/a.proto，再生成 protos/b/v1/b.proto
   操作:
    <img width="498" alt="image" src="https://github.com/zeromicro/go-zero/assets/17561793/3a83799c-c59a-458e-b1ea-a7fe175b0d40">
   import 错误:
    <img width="931" alt="image" src="https://github.com/zeromicro/go-zero/assets/17561793/cd1ac0d9-c566-4b1f-9288-8b65ac2eac42">

- 第二次，先生成 protos/b/v1/b.proto，再生成 protos/a/v1/a.proto
   操作:
    <img width="272" alt="image" src="https://github.com/zeromicro/go-zero/assets/17561793/5dcff146-9d0c-496a-90f7-7f78a94d6fcd">
    
   import 正确:
    <img width="664" alt="image" src="https://github.com/zeromicro/go-zero/assets/17561793/3eaa0a27-d80f-4dd1-a987-cc0d22148749">


2. The error is


**Expected behavior**
无论生成 proto 的顺序如何，import package 都应该是正确的

**Screenshots**
参见上面的图片

**Environments (please complete the following information):**
 - OS: [e.g. macOS]
 - go-zero version [e.g. 1.6.0]
 - goctl version [e.g. 1.6.0]






Pull request:
fix import error if generate multiple proto
Close #3692.

Patch:
diff --git a/tools/goctl/rpc/generator/genpb.go b/tools/goctl/rpc/generator/genpb.go
index 797a1fc8..758bc2f6 100644
--- a/tools/goctl/rpc/generator/genpb.go
+++ b/tools/goctl/rpc/generator/genpb.go
@@ -31,14 +31,14 @@ func (g *Generator) genPbDirect(ctx DirContext, c *ZRpcContext) error {
 }
 
 func (g *Generator) setPbDir(ctx DirContext, c *ZRpcContext) error {
-	pbDir, err := findPbFile(c.GoOutput, false)
+	pbDir, err := findPbFile(c.GoOutput, c.Src, false)
 	if err != nil {
 		return err
 	}
 	if len(pbDir) == 0 {
 		return fmt.Errorf("pg.go is not found under %q", c.GoOutput)
 	}
-	grpcDir, err := findPbFile(c.GrpcOutput, true)
+	grpcDir, err := findPbFile(c.GrpcOutput, c.Src, true)
 	if err != nil {
 		return err
 	}
@@ -62,7 +62,11 @@ const (
 	grpcSuffix = "_grpc.pb.go"
 )
 
-func findPbFile(current string, grpc bool) (string, error) {
+func findPbFile(current string, src string, grpc bool) (string, error) {
+	protoName := strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
+	pbFile := protoName + "." + pbSuffix
+	grpcFile := protoName + grpcSuffix
+
 	fileSystem := os.DirFS(current)
 	var ret string
 	err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
@@ -71,11 +75,11 @@ func findPbFile(current string, grpc bool) (string, error) {
 		}
 		if strings.HasSuffix(path, pbSuffix) {
 			if grpc {
-				if strings.HasSuffix(path, grpcSuffix) {
+				if strings.HasSuffix(path, grpcFile) {
 					ret = path
 					return os.ErrExist
 				}
-			} else if !strings.HasSuffix(path, grpcSuffix) {
+			} else if strings.HasSuffix(path, pbFile) {
 				ret = path
 				return os.ErrExist
 			}


Test patch:
diff --git a/tools/goctl/rpc/generator/genpb_test.go b/tools/goctl/rpc/generator/genpb_test.go
index 9ed939a4..dbc6d5d9 100644
--- a/tools/goctl/rpc/generator/genpb_test.go
+++ b/tools/goctl/rpc/generator/genpb_test.go
@@ -46,12 +46,12 @@ service Greeter {
 			t.Log(err)
 			return
 		}
-		pbDir, err := findPbFile(output, false)
+		pbDir, err := findPbFile(output, protoFile, false)
 		assert.Nil(t, err)
 		pbGo := filepath.Join(pbDir, "greet.pb.go")
 		assert.True(t, pathx.FileExists(pbGo))
 
-		grpcDir, err := findPbFile(output, true)
+		grpcDir, err := findPbFile(output, protoFile, true)
 		assert.Nil(t, err)
 		grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
 		assert.True(t, pathx.FileExists(grpcGo))
@@ -76,12 +76,12 @@ service Greeter {
 			t.Log(err)
 			return
 		}
-		pbDir, err := findPbFile(output, false)
+		pbDir, err := findPbFile(output, protoFile, false)
 		assert.Nil(t, err)
 		pbGo := filepath.Join(pbDir, "greet.pb.go")
 		assert.True(t, pathx.FileExists(pbGo))
 
-		grpcDir, err := findPbFile(output, true)
+		grpcDir, err := findPbFile(output, protoFile, true)
 		assert.Nil(t, err)
 		grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
 		assert.True(t, pathx.FileExists(grpcGo))
@@ -108,12 +108,12 @@ service Greeter {
 			t.Log(err)
 			return
 		}
-		pbDir, err := findPbFile(output, false)
+		pbDir, err := findPbFile(output, protoFile, false)
 		assert.Nil(t, err)
 		pbGo := filepath.Join(pbDir, "greet.pb.go")
 		assert.True(t, pathx.FileExists(pbGo))
 
-		grpcDir, err := findPbFile(output, true)
+		grpcDir, err := findPbFile(output, protoFile, true)
 		assert.Nil(t, err)
 		grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
 		assert.True(t, pathx.FileExists(grpcGo))
@@ -140,12 +140,12 @@ service Greeter {
 			t.Log(err)
 			return
 		}
-		pbDir, err := findPbFile(output, false)
+		pbDir, err := findPbFile(output, protoFile, false)
 		assert.Nil(t, err)
 		pbGo := filepath.Join(pbDir, "greet.pb.go")
 		assert.True(t, pathx.FileExists(pbGo))
 
-		grpcDir, err := findPbFile(output, true)
+		grpcDir, err := findPbFile(output, protoFile, true)
 		assert.Nil(t, err)
 		grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
 		assert.True(t, pathx.FileExists(grpcGo))
@@ -183,12 +183,12 @@ service Greeter {
 			t.Log(err)
 			return
 		}
-		pbDir, err := findPbFile(output, false)
+		pbDir, err := findPbFile(output, protoFile, false)
 		assert.Nil(t, err)
 		pbGo := filepath.Join(pbDir, "greet.pb.go")
 		assert.True(t, pathx.FileExists(pbGo))
 
-		grpcDir, err := findPbFile(output, true)
+		grpcDir, err := findPbFile(output, protoFile, true)
 		assert.Nil(t, err)
 		grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
 		assert.True(t, pathx.FileExists(grpcGo))