From 9c4ed394a7dfc522797b6a9a5fd117fc876bded2 Mon Sep 17 00:00:00 2001 From: godLei6 <603785348@qq.com> Date: Thu, 19 Dec 2024 09:17:50 +0800 Subject: [PATCH] fix: go work duplicate prefix get err (#4487) --- tools/goctl/util/ctx/gomod.go | 6 ++-- tools/goctl/util/ctx/gomod_test.go | 54 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/tools/goctl/util/ctx/gomod.go b/tools/goctl/util/ctx/gomod.go index 15a30186..888d6889 100644 --- a/tools/goctl/util/ctx/gomod.go +++ b/tools/goctl/util/ctx/gomod.go @@ -82,13 +82,15 @@ func getRealModule(workDir string, execRun execx.RunFunc) (*Module, error) { if err != nil { return nil, err } - + if workDir[len(workDir)-1] != os.PathSeparator { + workDir = workDir + string(os.PathSeparator) + } for _, m := range modules { realDir, err := pathx.ReadLink(m.Dir) if err != nil { return nil, fmt.Errorf("failed to read go.mod, dir: %s, error: %w", m.Dir, err) } - + realDir += string(os.PathSeparator) if strings.HasPrefix(workDir, realDir) { return &m, nil } diff --git a/tools/goctl/util/ctx/gomod_test.go b/tools/goctl/util/ctx/gomod_test.go index 2859f4eb..e1bae88c 100644 --- a/tools/goctl/util/ctx/gomod_test.go +++ b/tools/goctl/util/ctx/gomod_test.go @@ -98,6 +98,60 @@ func Test_getRealModule(t *testing.T) { GoVersion: "go1.20", }, }, + { + name: "go work duplicate prefix", + args: args{ + workDir: "D:\\code\\company\\core-ee\\service", + execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) { + return ` + { + "Path": "gitee.com/unitedrhino/core", + "Dir": "D:\\code\\company\\core", + "GoMod": "D:\\code\\company\\core\\go.mod", + "GoVersion": "1.21.4" + } + { + "Path": "gitee.com/unitedrhino/core-ee", + "Dir": "D:\\code\\company\\core-ee", + "GoMod": "D:\\code\\company\\core-ee\\go.mod", + "GoVersion": "1.21.4" + }`, nil + }, + }, + want: &Module{ + Path: "gitee.com/unitedrhino/core-ee", + Dir: "D:\\code\\company\\core-ee", + GoMod: "D:\\code\\company\\core-ee\\go.mod", + GoVersion: "1.21.4", + }, + }, + { + name: "go work duplicate prefix2", + args: args{ + workDir: "D:\\code\\company\\core-ee", + execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) { + return ` + { + "Path": "gitee.com/unitedrhino/core", + "Dir": "D:\\code\\company\\core", + "GoMod": "D:\\code\\company\\core\\go.mod", + "GoVersion": "1.21.4" + } + { + "Path": "gitee.com/unitedrhino/core-ee", + "Dir": "D:\\code\\company\\core-ee", + "GoMod": "D:\\code\\company\\core-ee\\go.mod", + "GoVersion": "1.21.4" + }`, nil + }, + }, + want: &Module{ + Path: "gitee.com/unitedrhino/core-ee", + Dir: "D:\\code\\company\\core-ee", + GoMod: "D:\\code\\company\\core-ee\\go.mod", + GoVersion: "1.21.4", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {