Skip to content

Commit 0e5aa29

Browse files
committed
fix(npm): v1, prune tree
1 parent 969861c commit 0e5aa29

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

module/npm/v1/model.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (l *Lockfile) Build(requires [][2]string, strict bool) ([]*shared.Node, err
1515
for _, require := range requires {
1616
name := require[0]
1717
versionCons := require[1]
18-
n, e := buildTree(name, versionCons, &l.root.lockPkg, nil, strict)
18+
n, e := buildTree(name, versionCons, &l.root.lockPkg, nil, strict, map[[2]string]struct{}{})
1919
if e != nil {
2020
if !strict {
2121
continue
@@ -49,7 +49,7 @@ func postprocessPkg(pkg *lockPkg, parent *lockPkg) {
4949
}
5050
}
5151

52-
func buildTree(name string, versionConstraint string, current *lockPkg, visited *shared.Visited, strict bool) (*shared.Node, error) {
52+
func buildTree(name string, versionConstraint string, current *lockPkg, visited *shared.Visited, strict bool, pruneVisited map[[2]string]struct{}) (*shared.Node, error) {
5353
childVisited := visited.CreateSub(name, versionConstraint)
5454
if childVisited == nil {
5555
return nil, shared.CreateRevisitError(visited)
@@ -72,18 +72,21 @@ func buildTree(name string, versionConstraint string, current *lockPkg, visited
7272
} else {
7373
node.Dev = false
7474
}
75-
for childName, versionCons := range childPkg.Requires {
76-
childNode, e := buildTree(childName, versionCons, childPkg, childVisited, strict)
77-
if e != nil {
78-
if !strict {
79-
continue
75+
if _, ok := pruneVisited[[2]string{node.Name, node.Version}]; !ok {
76+
pruneVisited[[2]string{node.Name, node.Version}] = struct{}{}
77+
for childName, versionCons := range childPkg.Requires {
78+
childNode, e := buildTree(childName, versionCons, childPkg, childVisited, strict, pruneVisited)
79+
if e != nil {
80+
if !strict {
81+
continue
82+
}
83+
return nil, e
8084
}
81-
return nil, e
82-
}
83-
if childNode == nil {
84-
panic("childNode == nil")
85+
if childNode == nil {
86+
panic("childNode == nil")
87+
}
88+
node.Children = append(node.Children, childNode)
8589
}
86-
node.Children = append(node.Children, childNode)
8790
}
8891
return &node, nil
8992
}

0 commit comments

Comments
 (0)