-
Notifications
You must be signed in to change notification settings - Fork 19
/
deps_pkg.go
191 lines (159 loc) · 4.85 KB
/
deps_pkg.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package depgraph
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"go.uber.org/zap"
"github.com/Helcaraxan/gomod/internal/graph"
"github.com/Helcaraxan/gomod/internal/logger"
"github.com/Helcaraxan/gomod/internal/modules"
"github.com/Helcaraxan/gomod/internal/util"
)
func (g *DepGraph) buildImportGraph(dl *logger.Builder) error {
log := dl.Domain(logger.PackageInfoDomain)
log.Debug("Building initial dependency graph based on the import graph.")
err := g.retrieveTransitiveImports(log, []string{fmt.Sprintf("%s/...", g.Main.Info.Path)})
if err != nil {
return err
}
pkgs := g.Graph.GetLevel(int(LevelPackages))
for _, node := range pkgs.List() {
pkg := node.(*Package)
imports := pkg.Info.Imports
if pkg.parent.Name() == g.Main.Name() {
imports = append(imports, pkg.Info.TestImports...)
imports = append(imports, pkg.Info.XTestImports...)
}
for _, imp := range imports {
if isStandardLib(imp) {
continue
}
targetNode, _ := pkgs.Get(packageHash(imp))
if targetNode == nil {
log.Error("Detected import of unknown package.", zap.String("package", imp))
continue
}
log.Debug(
"Adding package dependency.",
zap.String("source", pkg.Name()),
zap.String("source-module", pkg.Parent().Name()),
zap.String("target", targetNode.Name()),
zap.String("target-module", targetNode.Parent().Name()),
)
targetPkg := targetNode.(*Package)
if err = g.Graph.AddEdge(pkg, targetPkg); err != nil {
return err
}
}
}
if err = g.markNonTestDependencies(log); err != nil {
return err
}
return nil
}
func (g *DepGraph) markNonTestDependencies(log *logger.Logger) error {
log.Debug("Marking non-test dependencies.")
var todo []graph.Node
seen := map[string]bool{}
for _, mainPkg := range g.Main.packages.List() {
if strings.HasSuffix(mainPkg.(*Package).Info.Name, "_test") {
log.Debug("Skipping main module package as it is a test-only package.", zap.String("package", mainPkg.Name()))
continue
}
todo = append(todo, mainPkg)
seen[mainPkg.Name()] = true
}
for len(todo) > 0 {
next := todo[0]
todo = todo[1:]
log.Debug("Marking package as non-test dependency.", zap.String("package", next.Name()))
next.(*Package).isNonTestDependency = true
next.Parent().(*Module).isNonTestDependency = true
for _, imp := range next.(*Package).Info.Imports {
if isStandardLib(imp) {
continue
}
dep, err := g.Graph.GetNode(packageHash(imp))
if err != nil {
return err
}
if !seen[dep.Name()] {
todo = append(todo, dep)
seen[dep.Name()] = true
}
}
}
return nil
}
func (g *DepGraph) retrieveTransitiveImports(log *logger.Logger, pkgs []string) error {
const maxQueryLength = 950 // This is chosen conservatively to ensure we don't exceed maximum command lengths for 'go list' invocations.
queued := map[string]bool{}
for len(pkgs) > 0 {
queryLength := 0
cursor := 0
for {
if cursor == len(pkgs) || queryLength+len(pkgs[cursor]) > maxQueryLength {
break
}
queryLength += len(pkgs[cursor]) + 1
cursor++
}
query := pkgs[:cursor]
pkgs = pkgs[cursor:]
imports, err := g.retrievePackageInfo(log, query)
if err != nil {
return err
}
for _, pkg := range imports {
if !queued[pkg] {
queued[pkg] = true
pkgs = append(pkgs, pkg)
}
}
}
return nil
}
func (g *DepGraph) retrievePackageInfo(log *logger.Logger, pkgs []string) (imports []string, err error) {
stdout, _, err := util.RunCommand(log, g.Main.Info.Dir, "go", append([]string{"list", "-json"}, pkgs...)...)
if err != nil {
log.Error("Failed to list imports for packages.", zap.Strings("packages", pkgs), zap.Error(err))
return nil, err
}
dec := json.NewDecoder(bytes.NewReader(stdout))
for {
pkgInfo := &modules.PackageInfo{}
if err = dec.Decode(pkgInfo); err != nil {
if err == io.EOF {
break
} else {
log.Error("Failed to parse go list output.", zap.Error(err))
return nil, err
}
}
parentModule, ok := g.getModule(pkgInfo.Module.Path)
if !ok {
log.Error("Encountered package in unknown module.", zap.String("package", pkgInfo.ImportPath), zap.String("module", pkgInfo.Module.Path))
continue
}
pkg := NewPackage(pkgInfo, parentModule)
_ = g.Graph.AddNode(pkg)
log.Debug("Added import information for package", zap.String("package", pkg.Name()), zap.String("module", parentModule.Name()))
importCandidates := make([]string, len(pkgInfo.Imports))
copy(importCandidates, pkgInfo.Imports)
if parentModule.Name() == g.Main.Name() {
importCandidates = append(importCandidates, pkgInfo.TestImports...)
importCandidates = append(importCandidates, pkgInfo.XTestImports...)
}
for _, candidate := range importCandidates {
if !isStandardLib(candidate) {
imports = append(imports, candidate)
}
}
}
return imports, nil
}
func isStandardLib(pkg string) bool {
return !strings.Contains(strings.Split(pkg, "/")[0], ".")
}