Skip to content

Commit 6f33d74

Browse files
authored
fix(arborist): safeguard against null node.target in flag calculation (#7579)
<!-- What / Why --> If a node represents a symbolic link or a file dep (node.isLink is true), its target is expected to reference another node in the dependency tree. If the linking is not done correctly or is incomplete, node.target might be null. <!-- Describe the request in detail. What it does and why it's being changed. --> in this PR, a null check is added to ensure node.target is not null or before proceeding, which will prevent causing errors like: `npm error Cannot set properties of null (setting 'peer')` ## References Related to #7065, Fixes #6622, #5007, Closes #6622, #5007
1 parent 2490b49 commit 6f33d74

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

workspaces/arborist/lib/calc-dep-flags.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const calcDepFlagsStep = (node) => {
3131

3232
// for links, map their hierarchy appropriately
3333
if (node.isLink) {
34+
// node.target can be null, we check to ensure it's not null before proceeding
35+
if (node.target == null) {
36+
return node
37+
}
3438
node.target.dev = node.dev
3539
node.target.optional = node.optional
3640
node.target.devOptional = node.devOptional
@@ -97,15 +101,18 @@ const unsetFlag = (node, flag) => {
97101
tree: node,
98102
visit: node => {
99103
node.extraneous = node[flag] = false
100-
if (node.isLink) {
104+
if (node.isLink && node.target) {
101105
node.target.extraneous = node.target[flag] = false
102106
}
103107
},
104108
getChildren: node => {
105109
const children = []
106-
for (const edge of node.target.edgesOut.values()) {
107-
if (edge.to && edge.to[flag] &&
108-
(flag !== 'peer' && edge.type === 'peer' || edge.type === 'prod')
110+
const targetNode = node.isLink && node.target ? node.target : node
111+
for (const edge of targetNode.edgesOut.values()) {
112+
if (
113+
edge.to &&
114+
edge.to[flag] &&
115+
((flag !== 'peer' && edge.type === 'peer') || edge.type === 'prod')
109116
) {
110117
children.push(edge.to)
111118
}

workspaces/arborist/test/calc-dep-flags.js

+13
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,16 @@ t.test('set parents to not extraneous when visiting', t => {
264264
t.equal(bazLink.devOptional, false, 'bazlink not devOptional')
265265
t.end()
266266
})
267+
268+
t.test('check null target in link', async t => {
269+
const root = new Link({
270+
path: '/some/path',
271+
realpath: '/some/path',
272+
pkg: {
273+
dependencies: { foo: '' },
274+
},
275+
})
276+
t.doesNotThrow(() => calcDepFlags(root))
277+
t.doesNotThrow(() => calcDepFlags(root, false))
278+
t.end()
279+
})

0 commit comments

Comments
 (0)