Skip to content

Commit

Permalink
fix(arborist): identify and repair invalid nodes in the virtual tree
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf authored and lukekarrys committed Mar 28, 2022
1 parent 18b8b94 commit bd96ae4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
15 changes: 15 additions & 0 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ Try using the package name instead, e.g:
})

.then(tree => {
// search the virtual tree for invalid edges, if any are found add their source to
// the depsQueue so that we'll fix it later
depth({
tree,
getChildren: (node) => [...node.edgesOut.values()].map(edge => edge.to),
filter: node => node,
visit: node => {
for (const edge of node.edgesOut.values()) {
if (!edge.valid) {
this[_depsQueue].push(node)
break // no need to continue the loop after the first hit
}
}
},
})
// null the virtual tree, because we're about to hack away at it
// if you want another one, load another copy.
this.idealTree = tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121566,7 +121566,7 @@ ArboristNode {
}
`

exports[`test/arborist/build-ideal-tree.js TAP update global > update a single dep 1`] = `
exports[`test/arborist/build-ideal-tree.js TAP update global > update a single dep, also fixes the invalid node 1`] = `
ArboristNode {
"children": Map {
"@isaacs/testing-dev-optional-flags" => ArboristNode {
Expand All @@ -121588,7 +121588,6 @@ ArboristNode {
"wrappy" => ArboristNode {
"edgesIn": Set {
EdgeIn {
"error": "INVALID",
"from": "node_modules/@isaacs/testing-dev-optional-flags",
"name": "wrappy",
"spec": "^1.0.2",
Expand All @@ -121598,7 +121597,8 @@ ArboristNode {
"location": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"name": "wrappy",
"path": "{CWD}/test/arborist/tap-testdir-build-ideal-tree-update-global/node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"version": "1.0.2",
},
},
"edgesIn": Set {
Expand All @@ -121617,7 +121617,6 @@ ArboristNode {
"type": "prod",
},
"wrappy" => EdgeOut {
"error": "INVALID",
"name": "wrappy",
"spec": "^1.0.2",
"to": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
Expand Down Expand Up @@ -121818,7 +121817,7 @@ ArboristNode {
}
`

exports[`test/arborist/build-ideal-tree.js TAP update global > updating missing dep should have no effect 1`] = `
exports[`test/arborist/build-ideal-tree.js TAP update global > updating missing dep should have no effect, but fix the invalid node 1`] = `
ArboristNode {
"children": Map {
"@isaacs/testing-dev-optional-flags" => ArboristNode {
Expand All @@ -121840,7 +121839,6 @@ ArboristNode {
"wrappy" => ArboristNode {
"edgesIn": Set {
EdgeIn {
"error": "INVALID",
"from": "node_modules/@isaacs/testing-dev-optional-flags",
"name": "wrappy",
"spec": "^1.0.2",
Expand All @@ -121850,7 +121848,8 @@ ArboristNode {
"location": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"name": "wrappy",
"path": "{CWD}/test/arborist/tap-testdir-build-ideal-tree-update-global/node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"version": "1.0.2",
},
},
"edgesIn": Set {
Expand All @@ -121869,7 +121868,6 @@ ArboristNode {
"type": "prod",
},
"wrappy" => EdgeOut {
"error": "INVALID",
"name": "wrappy",
"spec": "^1.0.2",
"to": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
Expand Down Expand Up @@ -121941,7 +121939,7 @@ ArboristNode {
}
`

exports[`test/arborist/build-ideal-tree.js TAP update global > updating sub-dep has no effect 1`] = `
exports[`test/arborist/build-ideal-tree.js TAP update global > updating sub-dep has no effect, but fixes the invalid node 1`] = `
ArboristNode {
"children": Map {
"@isaacs/testing-dev-optional-flags" => ArboristNode {
Expand All @@ -121963,7 +121961,6 @@ ArboristNode {
"wrappy" => ArboristNode {
"edgesIn": Set {
EdgeIn {
"error": "INVALID",
"from": "node_modules/@isaacs/testing-dev-optional-flags",
"name": "wrappy",
"spec": "^1.0.2",
Expand All @@ -121973,7 +121970,8 @@ ArboristNode {
"location": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"name": "wrappy",
"path": "{CWD}/test/arborist/tap-testdir-build-ideal-tree-update-global/node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"version": "1.0.2",
},
},
"edgesIn": Set {
Expand All @@ -121992,7 +121990,6 @@ ArboristNode {
"type": "prod",
},
"wrappy" => EdgeOut {
"error": "INVALID",
"name": "wrappy",
"spec": "^1.0.2",
"to": "node_modules/@isaacs/testing-dev-optional-flags/node_modules/wrappy",
Expand Down
6 changes: 3 additions & 3 deletions workspaces/arborist/test/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2213,10 +2213,10 @@ t.test('update global', async t => {
})

t.matchSnapshot(await printIdeal(path, { global: true, update: ['abbrev'] }),
'updating missing dep should have no effect')
'updating missing dep should have no effect, but fix the invalid node')

t.matchSnapshot(await printIdeal(path, { global: true, update: ['wrappy'] }),
'updating sub-dep has no effect')
'updating sub-dep has no effect, but fixes the invalid node')

const invalidArgs = [
'once@1.4.0',
Expand All @@ -2234,7 +2234,7 @@ t.test('update global', async t => {
}

t.matchSnapshot(await printIdeal(path, { global: true, update: ['once'] }),
'update a single dep')
'update a single dep, also fixes the invalid node')
t.matchSnapshot(await printIdeal(path, { global: true, update: true }),
'update all the deps')
})
Expand Down

0 comments on commit bd96ae4

Please sign in to comment.