-
Notifications
You must be signed in to change notification settings - Fork 19
/
deps_mod.go
134 lines (113 loc) · 3.7 KB
/
deps_mod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package depgraph
import (
"bytes"
"io/ioutil"
"regexp"
"strings"
"go.uber.org/zap"
"github.com/Helcaraxan/gomod/internal/logger"
"github.com/Helcaraxan/gomod/internal/util"
)
func (g *DepGraph) overlayModuleDependencies(dl *logger.Builder) error {
log := dl.Domain(logger.ModuleDependencyDomain)
log.Debug("Overlaying module-based dependency information over the import dependency graph.")
raw, _, err := util.RunCommand(log, g.Main.Info.Dir, "go", "mod", "graph")
if err != nil {
return err
}
for _, depString := range strings.Split(strings.TrimSpace(string(raw)), "\n") {
log.Debug("Parsing dependency", zap.String("reference", depString))
modDep, ok := g.parseDependency(log, depString)
if !ok {
continue
}
log.Debug(
"Overlaying module dependency.",
zap.String("version", modDep.targetVersion),
zap.String("source", modDep.source.Name()),
zap.String("target", modDep.target.Name()),
)
err = g.Graph.AddEdge(modDep.source, modDep.target)
if err != nil {
return err
}
modDep.source.VersionConstraints[modDep.target.Hash()] = VersionConstraint{
Source: modDep.sourceVersion,
Target: modDep.targetVersion,
}
}
if err := g.markIndirects(log); err != nil {
return err
}
return nil
}
type moduleDependency struct {
source *Module
sourceVersion string
target *Module
targetVersion string
}
func (g *DepGraph) parseDependency(log *logger.Logger, depString string) (*moduleDependency, bool) {
depContent := depRE.FindStringSubmatch(depString)
if len(depContent) == 0 {
log.Warn("Skipping ill-formed line in 'go mod graph' output.", zap.String("line", depString))
return nil, false
}
sourceName, sourceVersion := depContent[1], depContent[2]
targetName, targetVersion := depContent[3], depContent[4]
source, ok := g.getModule(sourceName)
if !ok {
log.Warn("Encountered a dependency edge starting at an unknown module.", zap.String("source", sourceName), zap.String("target", targetName))
return nil, false
}
target, ok := g.getModule(targetName)
if !ok {
log.Warn("Encountered a dependency edge ending at an unknown module.", zap.String("source", sourceName), zap.String("target", targetName))
return nil, false
}
if sourceVersion != source.Info.Version {
log.Debug(
"Skipping edge as we are not using the specified source version.",
zap.String("source", sourceName),
zap.String("version", sourceVersion),
zap.String("target", targetName),
)
return nil, false
}
log.Debug(
"Recording module dependency.",
zap.String("source", sourceName),
zap.String("version", sourceVersion),
zap.String("target", targetName),
)
return &moduleDependency{
source: source,
sourceVersion: sourceVersion,
target: target,
targetVersion: targetVersion,
}, true
}
func (g *DepGraph) markIndirects(log *logger.Logger) error {
for _, node := range g.Graph.GetLevel(int(LevelModules)).List() {
module := node.(*Module)
log := log.With(zap.String("module", module.Name()))
log.Debug("Finding indirect dependencies for module.")
if module.Info.GoMod == "" {
// This occurs when we are under tests and can be skipped safely.
continue
}
modContent, err := ioutil.ReadFile(module.Info.GoMod)
if err != nil {
log.Error("Failed to read content of go.mod file.", zap.String("path", module.Info.GoMod), zap.Error(err))
return err
}
indirectDepRE := regexp.MustCompile(`^ ([^\s]+) [^\s]+ // indirect$`)
for _, line := range bytes.Split(modContent, []byte("\n")) {
if m := indirectDepRE.FindSubmatch(line); len(m) == 2 {
log.Debug("Found indirect dependency.", zap.String("consumer", module.Name()), zap.String("dependency", string(m[1])))
module.Indirects[string(m[1])] = true
}
}
}
return nil
}