Skip to content
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

fix: Add better error message when lockfile is malformed #6997

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from

Conversation

Nokel81
Copy link

@Nokel81 Nokel81 commented Nov 15, 2023

Sometimes when I am working with a monorepo of many workspaces with intermittencies I encounter the following error:

npm ERR! Cannot read properties of undefined (reading 'extraneous')

Which isn't very actionable. It also isn't very actionable with I add the --verbose option. All that does is point to @npmcli/arborist/lib/arborist/load-virtual.js:297:30.

Having added some logging manually I have been always able to fix up the lock file merely but removing the offending entry and then running npm install again.

@Nokel81 Nokel81 requested a review from a team as a code owner November 15, 2023 13:17
@Nokel81
Copy link
Author

Nokel81 commented Nov 15, 2023

I would like to have added a test case for this but I have never contributed to this repo before and don't know how to mock the relevant areas.

@wraithgar
Copy link
Member

Can you go into more detail about how you fix up the lockfile?

@wraithgar wraithgar closed this Nov 15, 2023
@wraithgar wraithgar reopened this Nov 15, 2023
@Nokel81
Copy link
Author

Nokel81 commented Nov 15, 2023

sure,

I found I was in the state that the node_modules/@my-scope/my-package entry existed as an entry similar to:

"node_modules/@my-scope/my-package": {
  "resolved": "packages/some-folder/my-package",
  "link": true
},

but the corresponding entry with key "packages/some-folder/my-package" did not exist.

@wraithgar
Copy link
Member

Is this the kind of thing we should be manually fixing up instead?

@Nokel81
Copy link
Author

Nokel81 commented Nov 16, 2023

That would be even better, but I don't think I know enough about arborist to make that change.

@wraithgar
Copy link
Member

wraithgar commented Nov 16, 2023

We can start by building on your own reproduction case, which appears to be a good one.

If I'm understanding correctly you have a workspace that's properly linked into node_modules, and somehow the link gets removed in the tree?

If that's really the case then once you fix the lockfile and run npm install what does npm do? Does it rebuild the lockfile identically to before you removed that line and make the symlink?

@wraithgar
Copy link
Member

Or is it the other way around, where the link still exists but the underlying package was moved?

@Nokel81
Copy link
Author

Nokel81 commented Nov 16, 2023

if I'm understanding correctly you have a workspace that's properly linked into node_modules, and somehow the link gets removed in the tree?

If that's really the case then once you fix the lockfile and run npm install what does npm do? Does it rebuild the lockfile identically to before you removed that line and make the symlink?

npm install does indeed recreate the lockfile identically except it adds the node_modules/@my-scope/my-package entry back and it also adds the packages/some-folder/my-package entry with the name, version, license, dependencies, etc.. as expected.

@Nokel81
Copy link
Author

Nokel81 commented Nov 16, 2023

And the link from the top level node_modules/@my-scope/my-package folder is created correctly too

@wraithgar
Copy link
Member

So in the broken state where there is a entry in the lock that looks like this

"node_modules/@my-scope/my-package": {
  "resolved": "packages/some-folder/my-package",
  "link": true
},

Does packages/some-folder/my-package still exist? Is just the link missing?

@Nokel81
Copy link
Author

Nokel81 commented Nov 16, 2023

No that entry in the lock file doesn't exist. And the symlink also doesn't exist

Which is why I think I see the initial error:

npm ERR! Cannot read properties of undefined (reading 'extraneous')

@wraithgar
Copy link
Member

Ok, sorry I'm confused as to what the error state is. Is there a reproducible way to trigger it?

@Nokel81
Copy link
Author

Nokel81 commented Nov 16, 2023

Yeah, let me try and make a repo

@Nokel81
Copy link
Author

Nokel81 commented Dec 5, 2023

@wraithgar Here is the demo repo https://github.com/Nokel81/npm-cli-error-case

@wraithgar
Copy link
Member

That is a very succinct reproduction case, thank you.

How did you get the repo into that state?

@Nokel81
Copy link
Author

Nokel81 commented Dec 5, 2023

To make the demo I manually modified the package-lock.json. I don't remember how it happened for me the very first time.

@wraithgar wraithgar changed the title chore: Add better error message when lockfile is malformed fix: Add better error message when lockfile is malformed Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants