Skip to content

Commit a9c080c

Browse files
committed
fix(gomod): support replace statement
1 parent f13d783 commit a9c080c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

module/go_mod/go.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,33 @@ func (i *Inspector) InspectProject(ctx context.Context) error {
4949
m.Name = f.Module.Mod.Path
5050
}
5151

52-
var depM = make(map[[2]string]struct{})
52+
var depReplace = make(map[[2]string][2]string)
53+
for _, it := range f.Replace {
54+
depReplace[[2]string{it.Old.Path, it.Old.Version}] = [2]string{it.New.Path, it.New.Version}
55+
depReplace[[2]string{it.Old.Path, ""}] = [2]string{it.New.Path, it.New.Version}
56+
}
57+
5358
for _, it := range f.Require {
5459
if it == nil {
5560
continue
5661
}
57-
depM[[2]string{it.Mod.Path, it.Mod.Version}] = struct{}{}
58-
}
59-
for _, it := range f.Replace {
60-
delete(depM, [2]string{it.Old.Path, it.Old.Version})
61-
depM[[2]string{it.New.Path, it.New.Version}] = struct{}{}
62-
}
63-
for it := range depM {
64-
m.Dependencies = append(m.Dependencies, model.Dependency{
65-
Name: it[0],
66-
Version: it[1],
67-
})
62+
if target, ok := depReplace[[2]string{it.Mod.Path, it.Mod.Version}]; ok {
63+
m.Dependencies = append(m.Dependencies, model.Dependency{
64+
Name: target[0],
65+
Version: target[1],
66+
})
67+
continue
68+
}
69+
if target, ok := depReplace[[2]string{it.Mod.Path, ""}]; ok {
70+
m.Dependencies = append(m.Dependencies, model.Dependency{
71+
Name: target[0],
72+
Version: target[1],
73+
})
74+
continue
75+
}
76+
m.Dependencies = append(m.Dependencies, model.Dependency{Name: it.Mod.Path, Version: it.Mod.Version})
6877
}
78+
6979
task.AddModule(m)
7080
return nil
7181
}

0 commit comments

Comments
 (0)