Skip to content

Commit

Permalink
fix(arborist): safeguard against null node.target in flag calculation (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
AmirSa12 committed Jul 1, 2024
1 parent 2490b49 commit 6f33d74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 11 additions & 4 deletions workspaces/arborist/lib/calc-dep-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const calcDepFlagsStep = (node) => {

// for links, map their hierarchy appropriately
if (node.isLink) {
// node.target can be null, we check to ensure it's not null before proceeding
if (node.target == null) {
return node
}
node.target.dev = node.dev
node.target.optional = node.optional
node.target.devOptional = node.devOptional
Expand Down Expand Up @@ -97,15 +101,18 @@ const unsetFlag = (node, flag) => {
tree: node,
visit: node => {
node.extraneous = node[flag] = false
if (node.isLink) {
if (node.isLink && node.target) {
node.target.extraneous = node.target[flag] = false
}
},
getChildren: node => {
const children = []
for (const edge of node.target.edgesOut.values()) {
if (edge.to && edge.to[flag] &&
(flag !== 'peer' && edge.type === 'peer' || edge.type === 'prod')
const targetNode = node.isLink && node.target ? node.target : node
for (const edge of targetNode.edgesOut.values()) {
if (
edge.to &&
edge.to[flag] &&
((flag !== 'peer' && edge.type === 'peer') || edge.type === 'prod')
) {
children.push(edge.to)
}
Expand Down
13 changes: 13 additions & 0 deletions workspaces/arborist/test/calc-dep-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,16 @@ t.test('set parents to not extraneous when visiting', t => {
t.equal(bazLink.devOptional, false, 'bazlink not devOptional')
t.end()
})

t.test('check null target in link', async t => {
const root = new Link({
path: '/some/path',
realpath: '/some/path',
pkg: {
dependencies: { foo: '' },
},
})
t.doesNotThrow(() => calcDepFlags(root))
t.doesNotThrow(() => calcDepFlags(root, false))
t.end()
})

0 comments on commit 6f33d74

Please sign in to comment.