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

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
13 changes: 13 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,19 @@ 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 -rf node_modules
Copy link
Member

Choose a reason for hiding this comment

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

Is this overkill? Having node_modules will make this go much faster and keep churn down on the lockfile.

2. rm package-lock.json
3. 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/,
})
})