Skip to content

Commit

Permalink
fix(query): properly return :missing nodes (#7320)
Browse files Browse the repository at this point in the history
Fixes #7316
  • Loading branch information
wraithgar authored Mar 27, 2024
1 parent 1da5cf0 commit 9bffa13
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ class Query extends BaseCommand {
// builds a normalized inventory
buildResponse (items) {
for (const node of items) {
if (!this.#seen.has(node.target.location)) {
if (!node.target.location || !this.#seen.has(node.target.location)) {
const item = new QuerySelectorItem(node)
this.#response.push(item)
this.#seen.add(item.location)
if (node.target.location) {
this.#seen.add(item.location)
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions tap-snapshots/test/lib/commands/query.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ exports[`test/lib/commands/query.js TAP linked node > should return linked node
]
`

exports[`test/lib/commands/query.js TAP missing > should return missing node 1`] = `
[
{
"name": "b",
"version": "^1.0.0",
"_id": "b@^1.0.0",
"pkgid": "b@^1.0.0",
"path": null,
"realpath": null,
"resolved": null,
"from": [
""
],
"to": [],
"dev": true,
"inBundle": false,
"deduped": false,
"overridden": false,
"queryContext": {
"missing": true
}
}
]
`

exports[`test/lib/commands/query.js TAP package-lock-only with package lock > should return valid response with only lock info 1`] = `
[
{
Expand Down
22 changes: 22 additions & 0 deletions test/lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,25 @@ t.test('expect entries', t => {
})
t.end()
})

t.test('missing', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir: {
node_modules: {
a: {
name: 'a',
version: '1.0.0',
},
},
'package.json': JSON.stringify({
name: 'project',
dependencies: {
a: '^1.0.0',
b: '^1.0.0',
},
}),
},
})
await npm.exec('query', [':missing'])
t.matchSnapshot(joinedOutput(), 'should return missing node')
})
7 changes: 6 additions & 1 deletion workspaces/arborist/lib/query-selector-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,12 @@ class Results {
for (const edge of node.edgesOut.values()) {
if (edge.missing) {
const pkg = { name: edge.name, version: edge.spec }
res.push(new this.#targetNode.constructor({ pkg }))
const item = new this.#targetNode.constructor({ pkg })
item.queryContext = {
missing: true,
}
item.edgesIn = new Set([edge])
res.push(item)
}
}
return res
Expand Down

0 comments on commit 9bffa13

Please sign in to comment.