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: allow packageDir and readPkgUp logic to play nice #1104

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

hulkish
Copy link
Contributor

@hulkish hulkish commented May 17, 2018

Fixes #1103, #1109

@hulkish
Copy link
Contributor Author

hulkish commented May 17, 2018

@benmosher @ljharb This is a fix to what was missed in #1085

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.09%) to 93.37% when pulling 65209e4 on hulkish:master into ebafcbf on benmosher:master.

@coveralls
Copy link

coveralls commented May 17, 2018

Coverage Status

Coverage increased (+2.8%) to 97.273% when pulling d09bf9a on hulkish:master into ebafcbf on benmosher:master.

ljharb
ljharb previously requested changes May 18, 2018
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Please provide some tests that would fail without this change?

Copy link
Member

@benmosher benmosher left a comment

Choose a reason for hiding this comment

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

agreed with @ljharb, need tests that would otherwise fail without this so that we know it's working. though it is good to know (though a bit weird?) that it doesn't impact any existing tests. wasn't paying close enough attention, it did break existing tests, so the real question is whether it is the new ones for the last release

@hulkish
Copy link
Contributor Author

hulkish commented May 18, 2018

@benmosher @ljharb So, I think this test is actually incorrect.

With my fix, this test fails (succeeds where expected to fail). I think that particular test should instead be:

    test({
      code: 'import leftPad from "left-pad";',
      filename: path.join(packageDirMonoRepoWithNested, 'foo.js'),
      errors: [{
        ruleId: 'no-extraneous-dependencies',
        message: '\'left-pad\' should be listed in the project\'s dependencies. Run \'npm i -S left-pad\' to add it',
      }],
    }),

Because, react is available via readPkgUp per this test case. And, left-pad is not - unless you define packageDir: <path to monorepo root>.

That covers the "invalid" use case. To cover the valid one, I am thinking to add this as one of the valid tests:

    test({
      code: 'import leftPad from "left-pad";',
      filename: path.join(packageDirMonoRepoWithNested, 'foo.js'),
      options: [{packageDir: packageDirMonoRepoRoot}],
    }),

Make sense?

@hulkish
Copy link
Contributor Author

hulkish commented May 18, 2018

I've updated this pr to reflect my last comment.

@hulkish hulkish force-pushed the master branch 11 times, most recently from 243487a to d633420 Compare May 19, 2018 16:09
@@ -243,24 +248,23 @@ ruleTester.run('no-extraneous-dependencies', rule, {
options: [{packageDir: packageDirMonoRepoWithNested}],
errors: [{
ruleId: 'no-extraneous-dependencies',
message: "'left-pad' should be listed in the project's dependencies. Run 'npm i -S left-pad' to add it",
message: '\'left-pad\' should be listed in the project\'s dependencies. Run \'npm i -S left-pad\' to add it',
Copy link
Member

Choose a reason for hiding this comment

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

are all these quotes changes necessary?

Copy link
Contributor Author

@hulkish hulkish May 21, 2018

Choose a reason for hiding this comment

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

@ljharb Yea, oddly enough - they were actually failing lint for me...Strings must use singlequote. (quotes). So, I just made it consistent with the rest of the file.

Copy link
Member

Choose a reason for hiding this comment

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

hm, strange. the linter should be ignoring cases where it minimizes escaping backslashes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea.. shouldn't it prefer backticks?

Copy link
Member

Choose a reason for hiding this comment

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

Only if there's interpolations.

Copy link
Member

Choose a reason for hiding this comment

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

This still seems like it should be reverted.

Copy link
Contributor Author

@hulkish hulkish Jun 6, 2018

Choose a reason for hiding this comment

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

@ljharb https://github.com/hulkish/eslint-plugin-import/blob/2e41085228ddd026ac7735eff36247b7f4d53d61/tests/src/rules/no-extraneous-dependencies.js as u can see, this change is consistent with the rest of the file. It actually fails lint without this change for me with the existing (in master).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually, i can see now this was corrected in master, so ill revert.

}],
}),
test({
code: 'import react from "react";',
Copy link
Member

Choose a reason for hiding this comment

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

if this test case is supposed to pass, let's move it to "valid"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I solved this by:

context.report({
message: 'The package.json file could not be parsed: ' + e.message,
loc: { line: 0, column: 0 },
})
} else {
throw e
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ljharb Thought I should make note of this.. We were swallowing "all else" errors before. I thought that was a mistake.

Copy link
Member

Choose a reason for hiding this comment

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

Throwing these errors will crash eslint - I'm not sure if that's better than silently logging them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, what if its a permission error? Shouldn't that crash eslint?

Copy link
Member

Choose a reason for hiding this comment

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

it perhaps should warn on the line, but not crash eslint altogether

Copy link
Contributor Author

@hulkish hulkish May 21, 2018

Choose a reason for hiding this comment

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

What way should i do that? I'm thinking:

    } else {
      context.report({
        message: e.message,
        loc: { line: 0, column: 0 },
      })
    }

Atlhough, I think this is an error related to the package.json loading/parsing in this instance, and should probably somehow be attached to that instead?

Copy link
Contributor Author

@hulkish hulkish Jun 5, 2018

Choose a reason for hiding this comment

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

Ok, this is updated. @ljharb, @benmosher anything else?

@dan-kez
Copy link

dan-kez commented Jun 6, 2018

Just wanted to check in on this PR and see if there's any additional changes that need to be made for it to be merged.

…ated filename instead of context.getFilename()
@hulkish
Copy link
Contributor Author

hulkish commented Jun 6, 2018

@dmk255 I think this is all - unless there's more req changes.

@hulkish
Copy link
Contributor Author

hulkish commented Jun 8, 2018

@benmosher ok i think all concerns mentioned above are resolved, now. No more changes planned from me, unless requested.

@Tymek
Copy link

Tymek commented Jun 13, 2018

Might fix #1109

@dvsekhvalnov
Copy link

Hey guys, any update on this one? Will it be merged/released soonish? :)

Thank you.

@dan-kez
Copy link

dan-kez commented Jul 10, 2018

Just chiming in given that there's been a bit of time since the last comment - any chance this may be merged soonish?

Thank you!

@hulkish
Copy link
Contributor Author

hulkish commented Jul 12, 2018

@benmosher any update on this? I don't think it's understood that the packageDir option is.currently broken without this.

@ljharb
Copy link
Member

ljharb commented Jul 18, 2018

After thinking about this and discussing offline with @hulkish, it kind of seems to me like the current packageDir option is actually not useful.

Perhaps instead of "fixing" it with this new merging behavior, we should add a per-category packageDir option that has the new behavior? Something like this:

'import/no-extraneous-dependencies': [2, {
  dependencies: false, // or true
  optionalDependencies: ['pattern/*/1', 'pattern/**/2'],
  peerDependencies: {
    patterns: ['pattern/*/1', 'pattern/**/2'],
    additionalPackageDirs: ['../', 'somewhere else'],
  },
}]

Thoughts?

@hulkish
Copy link
Contributor Author

hulkish commented Jul 18, 2018

@ljharb In your example snippet, I'm assuming patterns represents a glob to match against package names?

@ljharb
Copy link
Member

ljharb commented Jul 18, 2018

@hulkish it would be the same value that's currently already supported in the schema i indicated under "optionalDependencies".

@dan-kez
Copy link

dan-kez commented Jul 19, 2018

I'm a little confused as to the use of optionalDependencies and peerDependencies.patterns.

Also, as a user of a monorepo, my root package.json doesn't contain "peerDependencies" in the traditional sense but rather devDependencies that are shared across my packages.

I would find having the following valuable:

  • Each leaf package has the ability to reference the root package.json to get access to the devDependencies. It would be ideal if this could be done without needing to make a custom .eslintrc.js in each leaf package but that's not a big deal.
  • Each leaf package can resolve my symlinked packages that are included in my root node_modules. Right now I use https://github.com/Dreamscapes/eslint-import-resolver-lerna to help me do this. Otherwise eslint-plugin-import can't resolve my sibling packages and says they are not found.

Presently this PR fixes the first point. I've been able to use it by forking @hulkish's repo and importing that in my package.json. I'm entirely not sure what benefits @ljharb's proposal will grant though I definitely could be missing something.

@ljharb
Copy link
Member

ljharb commented Jul 19, 2018

@dmk255 with my suggestion, you'd configure eslint to allow devDeps to be declared in the root, but regular deps to only be declared in the subproject's package.json.

@dan-kez
Copy link

dan-kez commented Jul 19, 2018

@ljharb That sounds great - would you be able to give an example of the syntax you're considering to be able to do that? I don't immediately see how it fits into the example above.

@ljharb
Copy link
Member

ljharb commented Jul 19, 2018

@dmk255

'import/no-extraneous-dependencies': [2, {
  dependencies: true,
  devDependencies: {
    additionalPackageDirs: ['.'],
  },
}]

This would require that deps be in the closest package.json, but devDeps could be there OR in the current dir's package.json.

@dvsekhvalnov
Copy link

dvsekhvalnov commented Jul 26, 2018

Hey, let me ask as well. Here is our use case:

  • we have monorepo, where root package.json defines 'baseline' dependencies (not devDeps) for all subprojects (let's say React15)

  • each subproject can override baseline as it moving forward, (e.g. upgrade to React16).

  • normally 90% of subprojects are fine with baseline and simply do not define anything new in dependencies

Will proposed solution be able to handle that case? (e.g. eslint to lookup dependencies in root package.json when linting subproject with empty deps).

P.S> we usually run eslint from root, like: eslint --ext .js,.jsx . to lint everything in single pass. And prefer to have single (common) .eslintrc on root level as well.

Thank you.

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

@dvsekhvalnov yes, since it's "additional" - it merges - both this PR and my suggestion would address your use case.

@dvsekhvalnov
Copy link

dvsekhvalnov commented Jul 26, 2018

Ok, sounds good. Any update on when any of proposed fixes can be released? :)

@hulkish
Copy link
Contributor Author

hulkish commented Jul 26, 2018

@dvsekhvalnov I am going to refactor this pr a bit to meet the functionality described in
#1104 (comment)

From there, I think the plan is to merge that functionality (assuming all is well). Since, currently the packageDir option doesn't quite fit the need.

@tizmagik
Copy link
Contributor

What is remaining on this PR? Anything I can do to help get this merged?

@benmosher benmosher self-requested a review October 11, 2018 11:29
@benmosher benmosher dismissed their stale review October 11, 2018 11:30

dismissing my review because it seems like @ljharb and @hulkish are on the same page

@benmosher
Copy link
Member

@ljharb @hulkish I am not a user of this rule. it's not clear to me based on discussion if this PR is in a useful releasable state. if it is useful in its current state and solves your problems, I'm fine to merge it.

@dvsekhvalnov
Copy link

@benmosher and all, i have use case for this change, can try to test if you can explain how i can get workable copy of non-released version.

@tizmagik
Copy link
Contributor

@dvsekhvalnov I think you should be able to check out @hulkish branch and then link it in your project. Something like:

git clone https://github.com/hulkish/eslint-plugin-import
cd eslint-plugin-import
npm install
npm run prepublish
npm link
cd ~/my-project
npm link eslint-plugin-import

Then try running the linter. That should hopefully have it so that your project's eslint-plugin-import is actually linked to @hulkish 's version

@tylerlee
Copy link

I cloned @hulkish 's fork and this seems to fix my monorepo woes.

@benmosher
Copy link
Member

I would merge but it's not clear to me why there are conflicts nor how to resolve them for the issues you are all facing. @hulkish are you able to resolve?

@DavideDaniel
Copy link

DavideDaniel commented Feb 11, 2020

is the PR not in a testable state? I cloned @hulkish's fork and attempted seeing if this would resolve our monorepo lint issues particular to eslint-plugin-import and it did not... as I was about to post I realized that the pr comments are from 2018 but there are referenced from 2 days ago so the conflicts are probably real

@ljharb
Copy link
Member

ljharb commented Jun 5, 2020

ping @hulkish - any interest in rebasing this PR?

@ljharb ljharb marked this pull request as draft February 1, 2021 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Bug: no-extraneous-packages packageDir should not override readPkgUp logic
10 participants