Skip to content

fix(arborist): Add better error message when lockfile is malformed #8268

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

Merged
merged 1 commit into from
May 14, 2025
Merged
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
12 changes: 12 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,18 @@ 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)

if (!target) {
const err = new Error(
`Missing target in lock file: "${targetLoc}" is referenced by "${location}" but does not exist.
To fix:
1. rm package-lock.json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a bit too explosive. A more graceful fix would be to just remove the reference location from the package lock and then run npm i which fixes the issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, That was supposed to be removed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment was only about removing node modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't ever recommend people manually edit their package-lock file. The real fix would be for npm install to fix the tree, but that's a much bigger lift.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

2. npm install`
)
err.code = 'EMISSINGTARGET'
throw err
}

const link = this.#loadLink(location, targetLoc, target, meta)
nodes.set(location, link)
nodes.set(targetLoc, link.target)
Expand Down
41 changes: 41 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,44 @@ t.test('do not bundle the entire universe', async t => {
'yaml',
].sort())
})

t.test('error when link target is missing', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'root',
workspaces: ['packages/*'],
}),
'package-lock.json': JSON.stringify({
name: 'root',
lockfileVersion: 3,
packages: {
'': {
workspaces: ['packages/*'],
},
// This is the problematic entry - a link with no corresponding target
'node_modules/@my-scope/my-package': {
resolved: 'packages/some-folder/my-package',
link: true,
},
// Missing entry for 'packages/some-folder/my-package'
},
}),
packages: {
'some-folder': {
'my-package': {
'package.json': JSON.stringify({
name: '@my-scope/my-package',
version: '1.0.0',
}),
},
},
},
})

const arb = new Arborist({ path })

await t.rejects(arb.loadVirtual(), {
code: 'EMISSINGTARGET',
message: /Missing target in lock file:.*but does not exist/,
})
})