Skip to content

Commit 1ff339c

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

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

lib/commands/ls.js

Lines changed: 21 additions & 27 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,40 @@ const exploreDependencyGraph = (
237239
seenNodes,
238240
problems,
239241
cache,
240-
traversePathMap,
242+
traversePathMap = new Map(),
241243
encounterCount = new Map()
242244
) => {
243245
// Track the number of encounters for the current node
244246
// Why because we want to start storing/caching after the node is identified as a deduped edge
247+
245248
const count = node.path ? (encounterCount.get(node.path) || 0) + 1 : 0
246249
node.path && encounterCount.set(node.path, count)
247250

248-
if (node.path && cache.has(node.path)) {
251+
if (cache.has(node.path)) {
249252
return cache.get(node.path)
250253
}
251254

252255
const currentNodeResult = visit(node, problems)
253256

257+
// how the this node is explored
258+
// so if the explored path contains this node again then it's a cycle
259+
// and we don't want to explore it again
254260
const traversePath = [...(traversePathMap.get(currentNodeResult[_parent]) || [])]
255261
const isCircular = traversePath?.includes(node.pkgid)
256262
traversePath.push(node.pkgid)
257263
traversePathMap.set(currentNodeResult, traversePath)
258264

259-
if (count > 1) {
265+
// we want to start using cache after node is identified as a deduped
266+
if (count > 1 && node.path && node[_dedupe]) {
260267
cache.set(node.path, currentNodeResult)
261268
}
262269

263270
// Get children of current node
264271
const children = isCircular
265272
? []
266-
: getChildren(node, currentNodeResult, seenNodes, traversePathMap)
273+
: getChildren(node, currentNodeResult, seenNodes)
267274

268-
// Recurse on each child
275+
// Recurse on each child node
269276
for (const child of children) {
270277
const childResult = exploreDependencyGraph(
271278
child,
@@ -278,7 +285,9 @@ const exploreDependencyGraph = (
278285
traversePathMap,
279286
encounterCount
280287
)
288+
// include current node if any of its children are included
281289
currentNodeResult[_include] = currentNodeResult[_include] || childResult[_include]
290+
282291
if (childResult[_include] && !parseable) {
283292
if (json) {
284293
currentNodeResult.dependencies = currentNodeResult.dependencies || {}
@@ -386,7 +395,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
386395
) +
387396
(isGitNode(node) ? ` (${node.resolved})` : '') +
388397
(node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
389-
(long ? `\n${node.package.description || ''}` : '')
398+
(long ? `\n${node.package?.description || ''}` : '')
390399

391400
return ({ label, nodes: [], [_include]: node[_include], [_parent]: node[_parent] })
392401
}
@@ -560,16 +569,6 @@ const augmentNodesWithMetadata = ({
560569
const sortAlphabetically = ({ pkgid: a }, { pkgid: b }) => localeCompare(a, b)
561570

562571
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-
573572
if (!result.nodes.length) {
574573
result.nodes = ['(empty)']
575574
}
@@ -591,11 +590,6 @@ const jsonOutput = ({ path, problems, result, rootError }) => {
591590
result.invalid = true
592591
}
593592

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-
599593
return result
600594
}
601595

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

Lines changed: 2 additions & 2 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

@@ -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`] = `

0 commit comments

Comments
 (0)