Copilot commented on code in PR #3052:
URL: https://github.com/apache/dubbo-go/pull/3052#discussion_r2438003644


##########
tools/protoc-gen-go-triple/main.go:
##########
@@ -75,17 +76,55 @@ func main() {
 }
 
 func genTriple(plugin *protogen.Plugin) error {
+       var errors []error
+
        for _, file := range plugin.Files {
                if !file.Generate {
                        continue
                }
-               tripleGo, err := generator.ProcessProtoFile(file.Proto)
-               if err != nil {
-                       return err
+
+               // 跳过无服务的proto文件
+               if len(file.Proto.GetService()) == 0 {
+                       continue
                }
+
                filename := file.GeneratedFilenamePrefix + ".triple.go"
-               g := plugin.NewGeneratedFile(filename, file.GoImportPath)
-               return generator.GenTripleFile(g, tripleGo)
+               // Use the same import path as the pb.go file to ensure they're 
in the same package
+               // Extract the package name from the go_package option
+               goPackage := file.Proto.Options.GetGoPackage()
+               var importPath protogen.GoImportPath
+               if goPackage != "" {
+                       parts := strings.Split(goPackage, ";")
+                       importPath = protogen.GoImportPath(parts[0])
+               } else {
+                       importPath = file.GoImportPath
+               }
+               g := plugin.NewGeneratedFile(filename, importPath)

Review Comment:
   protogen 已经在 file.GoImportPath 中解析了 go_package(若设置),这里再手动解析会引入边界情况(例如 
go_package 仅有路径无分号、包含额外空格等)。建议直接使用 file.GoImportPath:g := 
plugin.NewGeneratedFile(filename, file.GoImportPath)。
   ```suggestion
                // Use the import path as parsed by protogen to avoid edge 
cases.
                g := plugin.NewGeneratedFile(filename, file.GoImportPath)
   ```



##########
tools/protoc-gen-go-triple/gen/generator/tripleTpl.go:
##########
@@ -140,16 +140,8 @@ const ImportTpl = `
 
 import (
        "context"
-       {{if .IsStream}}"net/http"{{end}}
-)
-
-import (
        "dubbo.apache.org/dubbo-go/v3"

Review Comment:
   模板的 ImportTpl 移除了 client、common、constant、triple_protocol、server 等 dubbo 依赖的 
import,但生成代码仍会引用这些标识符。由于 GenTripleFile 直接将模板字符串写入文件(未使用 protogen 的自动 import 
机制),main.go 中对 g.QualifiedGoIdent 的调用不会把这些包注入到 import 块,最终编译将出现未定义标识符错误。请恢复这些固定 
import,或在模板中渲染一个基于收集到依赖的动态 import 列表。
   ```suggestion
        "dubbo.apache.org/dubbo-go/v3"
        "dubbo.apache.org/dubbo-go/v3/client"
        "dubbo.apache.org/dubbo-go/v3/common"
        "dubbo.apache.org/dubbo-go/v3/constant"
        "dubbo.apache.org/dubbo-go/v3/protocol/triple"
        "dubbo.apache.org/dubbo-go/v3/server"
   ```



##########
tools/protoc-gen-go-triple/main.go:
##########
@@ -75,17 +76,55 @@ func main() {
 }
 
 func genTriple(plugin *protogen.Plugin) error {
+       var errors []error
+
        for _, file := range plugin.Files {
                if !file.Generate {
                        continue
                }
-               tripleGo, err := generator.ProcessProtoFile(file.Proto)
-               if err != nil {
-                       return err
+
+               // 跳过无服务的proto文件
+               if len(file.Proto.GetService()) == 0 {
+                       continue
                }
+
                filename := file.GeneratedFilenamePrefix + ".triple.go"
-               g := plugin.NewGeneratedFile(filename, file.GoImportPath)
-               return generator.GenTripleFile(g, tripleGo)
+               // Use the same import path as the pb.go file to ensure they're 
in the same package
+               // Extract the package name from the go_package option
+               goPackage := file.Proto.Options.GetGoPackage()
+               var importPath protogen.GoImportPath
+               if goPackage != "" {
+                       parts := strings.Split(goPackage, ";")
+                       importPath = protogen.GoImportPath(parts[0])
+               } else {
+                       importPath = file.GoImportPath
+               }
+               g := plugin.NewGeneratedFile(filename, importPath)
+               // 导入dubbo基础库
+               
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/client").Ident("client"))
+               
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common").Ident("common"))
+               
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common/constant").Ident("constant"))
+               
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol").Ident("triple_protocol"))
+               
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/server").Ident("server"))

Review Comment:
   这些 QualifiedGoIdent 调用返回的引用字符串未被写入生成文件,且 GenTripleFile 直接写模板字符串,不会触发 
protogen 的自动 import 注入。因此这些调用对最终 import 块没有任何效果。建议:要么恢复模板中的固定 import,要么改为使用 g.P 
生成并实际输出这些引用,让 protogen 注入 import。
   ```suggestion
                
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/server").Ident("server"))
                // Output dummy references to trigger import injection
                g.P("// Reference: ", 
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/client").Ident("client")))
                g.P("// Reference: ", 
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common").Ident("common")))
                g.P("// Reference: ", 
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common/constant").Ident("constant")))
                g.P("// Reference: ", 
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol").Ident("triple_protocol")))
                g.P("// Reference: ", 
g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/server").Ident("server")))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to