fix: go work duplicate prefix get err (#4487)

This commit is contained in:
godLei6 2024-12-19 09:17:50 +08:00 committed by GitHub
parent fd07a9c6e4
commit 9c4ed394a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 2 deletions

View File

@ -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
}

View File

@ -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) {