Skip to content

fix(aborist): handle missing link targets in load-virtual.js #8200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions workspaces/arborist/lib/arborist/load-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ module.exports = cls => class VirtualLoader extends cls {
const targetPath = resolve(this.path, meta.resolved)
const targetLoc = relpath(this.path, targetPath)
const target = nodes.get(targetLoc)
// Skip loading the target if it doesn't exist
// This can happen if the edge to it has error MISSING
// For example, where a workspace has a broken link dependency
if (!target) {
continue
}
const link = this.#loadLink(location, targetLoc, target, meta)
nodes.set(location, link)
nodes.set(targetLoc, link.target)
Expand Down
34 changes: 34 additions & 0 deletions workspaces/arborist/test/arborist/load-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,37 @@ t.test('do not bundle the entire universe', async t => {
'yaml',
].sort())
})

t.test('load-virtual does not fail if a link target does not exist', async t => {
// Create a mock Shrinkwrap with a link to a non-existent target
const mockPath = t.testdir({
'package.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
}),
'package-lock.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
lockfileVersion: 2,
requires: true,
packages: {
'': {
name: 'test-package',
version: '1.0.0',
dependencies: {
'missing-pkg': 'file:./missing-pkg',
},
},
'node_modules/missing-pkg': {
link: true,
resolved: './missing-pkg',
},
},
}),
})

// This should not throw an error because the #resolveLinks method
// should skip loading the target if it doesn't exist
const tree = await loadVirtual(mockPath)
t.ok(tree, 'tree loaded successfully despite missing target')
})