Skip to content

Commit fb7ba8e

Browse files
committed
fix output for incorrect dedupe
1 parent e07bc57 commit fb7ba8e

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

lib/commands/ls.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ class LS extends ArboristWorkspaceCmd {
138138
const item = json
139139
? getJsonOutputItem(node, { global, long })
140140
: parseable
141-
? {}
141+
? {
142+
pkgid: node.pkgid,
143+
path: node.path,
144+
[_dedupe]: node[_dedupe],
145+
[_parent]: node[_parent],
146+
}
142147
: getHumanOutputItem(node, { args, chalk, global, long })
143148

144149
// loop through list of node problems to add them to global list
@@ -157,13 +162,11 @@ class LS extends ArboristWorkspaceCmd {
157162
? filterDefaultDepth
158163
: (depth || 0)
159164

160-
// add root node of tree to list of seenNodes
161-
162165
const seenNodes = new Map()
163166
const problems = new Set()
164167
const cache = new Map()
165-
const traversePathMap = new Map()
166168

169+
// add root node of tree to list of seenNodes
167170
seenNodes.set(tree.path, tree)
168171

169172
const result = exploreDependencyGraph(
@@ -173,8 +176,7 @@ class LS extends ArboristWorkspaceCmd {
173176
{ json, parseable },
174177
seenNodes,
175178
problems,
176-
cache,
177-
traversePathMap
179+
cache
178180
)
179181

180182
// handle the special case of a broken package.json in the root folder
@@ -237,35 +239,36 @@ const exploreDependencyGraph = (
237239
seenNodes,
238240
problems,
239241
cache,
240-
traversePathMap,
241-
encounterCount = new Map()
242+
traversePathMap = new Map()
242243
) => {
243244
// Track the number of encounters for the current node
244245
// Why because we want to start storing/caching after the node is identified as a deduped edge
245-
const count = node.path ? (encounterCount.get(node.path) || 0) + 1 : 0
246-
node.path && encounterCount.set(node.path, count)
247246

248-
if (node.path && cache.has(node.path)) {
247+
if (cache.has(node.path)) {
249248
return cache.get(node.path)
250249
}
251250

252251
const currentNodeResult = visit(node, problems)
253252

253+
// how the this node is explored
254+
// so if the explored path contains this node again then it's a cycle
255+
// and we don't want to explore it again
254256
const traversePath = [...(traversePathMap.get(currentNodeResult[_parent]) || [])]
255257
const isCircular = traversePath?.includes(node.pkgid)
256258
traversePath.push(node.pkgid)
257259
traversePathMap.set(currentNodeResult, traversePath)
258260

259-
if (count > 1) {
261+
// we want to start using cache after node is identified as a deduped
262+
if (currentNodeResult[_dedupe]) {
260263
cache.set(node.path, currentNodeResult)
261264
}
262265

263266
// Get children of current node
264267
const children = isCircular
265268
? []
266-
: getChildren(node, currentNodeResult, seenNodes, traversePathMap)
269+
: getChildren(node, currentNodeResult, seenNodes)
267270

268-
// Recurse on each child
271+
// Recurse on each child node
269272
for (const child of children) {
270273
const childResult = exploreDependencyGraph(
271274
child,
@@ -275,10 +278,11 @@ const exploreDependencyGraph = (
275278
seenNodes,
276279
problems,
277280
cache,
278-
traversePathMap,
279-
encounterCount
281+
traversePathMap
280282
)
283+
// include current node if any of its children are included
281284
currentNodeResult[_include] = currentNodeResult[_include] || childResult[_include]
285+
282286
if (childResult[_include] && !parseable) {
283287
if (json) {
284288
currentNodeResult.dependencies = currentNodeResult.dependencies || {}
@@ -386,7 +390,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
386390
) +
387391
(isGitNode(node) ? ` (${node.resolved})` : '') +
388392
(node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
389-
(long ? `\n${node.package.description || ''}` : '')
393+
(long ? `\n${node.package?.description || ''}` : '')
390394

391395
return ({ label, nodes: [], [_include]: node[_include], [_parent]: node[_parent] })
392396
}
@@ -560,16 +564,6 @@ const augmentNodesWithMetadata = ({
560564
const sortAlphabetically = ({ pkgid: a }, { pkgid: b }) => localeCompare(a, b)
561565

562566
const humanOutput = ({ chalk, result, unicode }) => {
563-
// we need to traverse the entire tree in order to determine which items
564-
// should be included (since a nested transitive included dep will make it
565-
// so that all its ancestors should be displayed)
566-
// here is where we put items in their expected place for archy output
567-
// for (const item of seenItems) {
568-
// if (item[_include] && item[_parent]) {
569-
// item[_parent].nodes.push(item)
570-
// }
571-
// }
572-
573567
if (!result.nodes.length) {
574568
result.nodes = ['(empty)']
575569
}
@@ -591,11 +585,6 @@ const jsonOutput = ({ path, problems, result, rootError }) => {
591585
result.invalid = true
592586
}
593587

594-
// we need to traverse the entire tree in order to determine which items
595-
// should be included (since a nested transitive included dep will make it
596-
// so that all its ancestors should be displayed)
597-
// here is where we put items in their expected place for json output
598-
599588
return result
600589
}
601590

tap-snapshots/test/lib/commands/ls.js.test.cjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ exports[`test/lib/commands/ls.js TAP ls with args and dedupe entries > should pr
600600
| \`-- @npmcli/b@1.1.2 deduped
601601
+-- @npmcli/b@1.1.2
602602
\`-- @npmcli/c@1.0.0
603-
[0m \`-- [33m@npmcli/b@1.1.2[39m[0m
603+
[0m \`-- [33m@npmcli/b@1.1.2[39m [2mdeduped[22m[0m
604604

605605
`
606606

@@ -610,7 +610,7 @@ dedupe-entries@1.0.0 {CWD}/prefix
610610
| \`-- @npmcli/c@1.0.0 deduped
611611
+-- @npmcli/b@1.1.2
612612
| \`-- @npmcli/c@1.0.0 deduped
613-
\`-- @npmcli/c@1.0.0 deduped
613+
\`-- @npmcli/c@1.0.0
614614
`
615615

616616
exports[`test/lib/commands/ls.js TAP ls with dot filter arg > should output tree contaning only occurrences of filtered by package and colored output 1`] = `
@@ -648,7 +648,7 @@ dedupe-entries@1.0.0 {CWD}/prefix
648648
| \`-- @npmcli/b@1.1.2 deduped
649649
+-- @npmcli/b@1.1.2
650650
\`-- @npmcli/c@1.0.0
651-
\`-- @npmcli/b@1.1.2
651+
\`-- @npmcli/b@1.1.2 deduped
652652
`
653653

654654
exports[`test/lib/commands/ls.js TAP ls with no args dedupe entries and not displaying all > should print tree output containing deduped ref 1`] = `
@@ -672,6 +672,8 @@ test-npm-ls@1.0.0 {CWD}/prefix
672672
+-- chai@1.0.0 extraneous
673673
| \`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai
674674
| \`-- cat@1.0.0 deduped invalid: "^2.0.0" from the root project
675-
\`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai
675+
| \`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai, "^2.0.0" from node_modules/cat
676+
\`-- dog@1.0.0 invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai, "^2.0.0" from node_modules/cat
676677
\`-- cat@1.0.0 deduped invalid: "^2.0.0" from the root project
678+
\`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai, "^2.0.0" from node_modules/cat, "^2.0.0" from node_modules/cat
677679
`

0 commit comments

Comments
 (0)