@@ -43,7 +43,6 @@ func (p *Pkg) Resolve() {
43
43
if name == "" {
44
44
return
45
45
}
46
- p .Depth = p .depth ()
47
46
48
47
// Stop resolving imports if we've reached a loop.
49
48
var importMode build.ImportMode
@@ -108,14 +107,26 @@ func (p *Pkg) setDeps(imports []string, parentDir string) {
108
107
109
108
// addDep creates a Pkg and it's dependencies from an imported package name.
110
109
func (p * Pkg ) addDep (name string , parentDir string ) {
111
- dep := Pkg {
112
- Name : name ,
113
- Tree : p .Tree ,
114
- //TODO: maybe better pass ParentDir as a param to Resolve() instead
115
- ParentDir : parentDir ,
116
- Parent : p ,
110
+ var dep Pkg
111
+ cached := p .Tree .getCachedPkg (name )
112
+ if cached != nil {
113
+ dep = * cached
114
+ dep .ParentDir = parentDir
115
+ dep .Parent = p
116
+ } else {
117
+ dep = Pkg {
118
+ Name : name ,
119
+ Tree : p .Tree ,
120
+ //TODO: maybe better pass ParentDir as a param to Resolve() instead
121
+ ParentDir : parentDir ,
122
+ Parent : p ,
123
+ }
124
+ dep .Resolve ()
125
+
126
+ p .Tree .cacheResolvedPackage (& dep )
117
127
}
118
- dep .Resolve ()
128
+
129
+ p .Depth = p .depth ()
119
130
120
131
if dep .IsBuiltin || dep .Name == "C" {
121
132
return
@@ -200,6 +211,8 @@ type Tree struct {
200
211
201
212
UnresolvedPkgs map [string ]struct {}
202
213
214
+ PkgCache map [string ]* Pkg
215
+
203
216
importCache map [string ]struct {}
204
217
}
205
218
@@ -221,6 +234,7 @@ func (t *Tree) Resolve(name string) error {
221
234
// reuse the same cache.
222
235
t .importCache = map [string ]struct {}{}
223
236
t .UnresolvedPkgs = map [string ]struct {}{}
237
+ t .PkgCache = map [string ]* Pkg {}
224
238
225
239
t .Root .Resolve ()
226
240
if ! t .Root .IsResolved {
@@ -244,6 +258,18 @@ func (t *Tree) rememverUnresolvedPkg(name string) {
244
258
t .UnresolvedPkgs [name ] = struct {}{}
245
259
}
246
260
261
+ func (t * Tree ) cacheResolvedPackage (pkg * Pkg ) {
262
+ t .PkgCache [pkg .Name ] = pkg
263
+ }
264
+
265
+ func (t * Tree ) getCachedPkg (name string ) * Pkg {
266
+ pkg , ok := t .PkgCache [name ]
267
+ if ! ok {
268
+ return nil
269
+ }
270
+ return pkg
271
+ }
272
+
247
273
func prettyPrintJSON (j interface {}) {
248
274
e := json .NewEncoder (os .Stdout )
249
275
e .SetIndent ("" , " " )
0 commit comments