Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

fix: npm dedupe property path of null error #308

Closed
wants to merge 1 commit into from

Conversation

ParadoxInfinite
Copy link

Related to npm/cli#3632
Copy pasting from the above closed PR as it's the same change.

From what I noticed by console logging dep when running npm dedupe, when dep.isLink was true, dep.target was null. Also, dep.realpath exists, so I am not 100% sure, but making an educated guess that we are supposed to use that instead of trying to look for the real path in the target which is null.

Would appreciate feedback, if any!

Also, I would appreciate if someone can help me out with writing a regression test for this? I looked into the test/place-dep.js file, but wasn't sure how I could write one for this condition specifically. The way I tested this was using the https://github.com/npm/cli/ repo with the test case provided in the issue below, so any help here is awesome!

References

Fixes npm/cli#3565

isaacs added a commit that referenced this pull request Aug 12, 2021
When a dependency graph cycles back on itself incompatibly like this:

```
a@1 -> b@1
b@1 -> a@2
a@2 -> b@2
b@2 -> a@1
```

We would find ourselves unable to handle the conflict via nesting.  For
example:

```
root
+-- a@1 -> b@1
+-- b@1 -> a@2
    +-- a@2 -> b@2
    +-- b@2 -> a@1
        +-- a@1 -> b@1
        +-- b@1 -> a@2
            +-- a@2 -> b@2
            +-- b@2 -> a@1 (cycling forever)
```

In order to address this, we create a link when such a cycle is
detected.

```
root
+-- a@1 -> b@1
+-- b@1 -> a@2
    +-- a@2 -> b@2
    +-- b@2 -> a@1
        +-- a@1 -> b@1
        +-- b@1 -> link to root/node_modules/b@1
```

Prior to the recent refactor to move much of the dependency placement
logic out of Arborist.buildIdealTree and into the PlaceDep class, this
link was created right at the moment when a new dependency was created
in a temp tree.

However, if we feed that Link object into the PlaceDep flow, it will
(correctly) see that the Link does not match the Node further up the
tree, and attempt to replace it.

Compounding the problem (and why it appeared in `npm dedupe` and not
`npm install`) is the fact that explicitly named updates are _always_
treated as a "problem edge", so that they can be re-evaluated.

So, rather than creating a Node to be tested against the tree, it was
creating a Link object, and then attempting to replace the Link's target
with the Link itself, which caused some havoc.

This patch moves the loop detection and remediating Link creation into
the PlaceDep class, which is the more proper place for it, as that class
owns the "put deps into the tree" logic, and this is clearly a "put deps
into the tree" type of situation.

Via: @ParadoxInfinite
Close: npm/cli#3632
Close: #308
Fix: npm/cli#3565
@isaacs
Copy link
Contributor

isaacs commented Aug 12, 2021

This addresses the surface symptom, and it's a perfectly fine defensive change to make, but doesn't actually make npm dedupe behave quite right in the situation presented in npm/cli#3565. Root caused and fixed in #309 (which incorporates this patch as well).

@ParadoxInfinite
Copy link
Author

This addresses the surface symptom, and it's a perfectly fine defensive change to make, but doesn't actually make npm dedupe behave quite right in the situation presented

I understand, as someone who wasn't very familiar with the codebase, this seemed like a logical change as it resolved the issue (or so I thought). I will go through the PR you've mentioned to see if I can understand the issue and the solution better. Hopefully it makes sense to me.

Thank you for taking the time and checking out this PR though!

@isaacs isaacs closed this in 77e279a Aug 12, 2021
@isaacs
Copy link
Contributor

isaacs commented Aug 12, 2021

@ParadoxInfinite You did most of the work of finding it, thanks ;) Happy to have you keep poking at Arborist, let us know if you have questions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] npm dedupe fails with TypeError: Cannot read property 'path' of null
2 participants