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

Bug: Multiple packageDirs in no-extraneous-dependencies #1175

Closed
aravindet opened this issue Sep 28, 2018 · 3 comments
Closed

Bug: Multiple packageDirs in no-extraneous-dependencies #1175

aravindet opened this issue Sep 28, 2018 · 3 comments

Comments

@aravindet
Copy link

According to the docs, packageDirs accepts an array of paths, and (presumably) the package.json's found at all the paths have some effect.

In the current implementation, only the last element in the array is used.

This is because Object.assign() is used to merge the dependencies found in each file. As Object.assign() performs a shallow merge, and the extractDepFields function adds default empty objects for fields that are not defined in package.json, every field ends up being overridden in each iteration.

I suggest that a deep merge be performed here instead of Object.assign.

@ljharb
Copy link
Member

ljharb commented Sep 28, 2018

You're right that this logic is incorrect. It'd be great to get a failing test :-)

@aravindet
Copy link
Author

I'm not familiar enough with this repo to send a PR, but I can suggest a minimal test:

Directory structure:

root
+- package.json
+- .eslintrc
+- bar
   +- package.json
   +- index.js

root/.eslintrc

{
  "rules": {
    "import/no-extraneous-dependencies": ["error", {
      "packageDir": [".", ".."]
    }]
  }
}

root/package.json

{
  "dependencies": { "foodep": "x" }
}

root/bar/package.json

{
  "dependencies": { "bardep": "x" }
}

root/bar/index.js

require('foodep');
require('bardep');
require('bazdep');

Assertion

eslint bar/index.js in all the scenarios should show exactly one error: on the require('bazdep') line.

Other scenarios

In other tests test, packageDir can be:

  • ["..", "."] (order swapped)
  • ["/absolute_root", "/absolute_root/bar"]
  • ["/absolute_root/bar", "/absolute_root"]

(Only allowing relative paths is extremely limiting, and it is not clear why this limitation exists in the documentation while path.resolve() accepts absolute paths. I would suggest that absolute paths should also be supported.)

@pzhine
Copy link

pzhine commented Oct 8, 2018

@aravindet @ljharb Already fixed this in a PR, with a test that fails for the current version.
@benmosher Can you review #1176 and merge if it looks good?

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

No branches or pull requests

3 participants