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

overrides from config in npm package looks for files in node_modules #12278

Closed
gziolo opened this issue Sep 17, 2019 · 8 comments
Closed

overrides from config in npm package looks for files in node_modules #12278

gziolo opened this issue Sep 17, 2019 · 8 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion

Comments

@gziolo
Copy link

gziolo commented Sep 17, 2019

Tell us about your environment

  • ESLint Version: 6.1.0
  • Node Version: 10.16.3
  • npm Version: 6.11.3

What parser (default, Babel-ESLint, etc.) are you using?
babel-eslint

Please show your full configuration:

Configuration

I use the config file which is published to npm package when running ESLint.

Config file is located in node_modules/@wordpress/scripts/config/.eslintrc.js:

module.exports = {
	root: true,
	extends: [ 'plugin:@wordpress/eslint-plugin/recommended' ],
};

The config file used in extends contains overrides as shown below:

module.exports = {
	parser: 'babel-eslint',
	extends: [
		require.resolve( './jsx-a11y.js' ),
		require.resolve( './custom.js' ),
		require.resolve( './react.js' ),
		require.resolve( './esnext.js' ),
	],
	env: {
		node: true,
	},
	globals: {
		window: true,
		document: true,
		wp: 'readonly',
	},
	overrides: [
		{
			// Unit test files and their helpers only.
			files: [
				'**/@(test|__tests__)/**/*.js',
				'**/?(*.)test.js',
			],
			extends: [
				require.resolve( './test-unit.js' ),
			],
		},
		{
			// End-to-end test files and their helpers only.
			files: [
				'**/specs/**/*.js',
				'**/?(*.)spec.js',
			],
			extends: [
				require.resolve( './test-e2e.js' ),
			],
		},
	],
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

It looks like overrides inherits also basePath for filtering files from the location of the config file when using extend making it not that much useful when consuming config published to npm. Related PR where I discovered it: WordPress/gutenberg-examples#87 (comment)

I reported in under #12032 (comment). It was only partially fixed with #12205.

What did you expect to happen?

Overrides in the config file match against my project rather than the folder where the config file is located in node_modules subfolder.

What actually happened? Please include the actual, raw output from ESLint.

This is the line which sets basePath based on the location of the file:

const basePath = filePath ? path.dirname(filePath) : cwd;

Example of the override:

criteria:
     { includes: [Array],
       excludes: null,
       basePath:
        '/Users/gziolo/PhpstormProjects/gutenberg-examples/node_modules/@wordpress/scripts/config' },

which obviously can't match any files in the project.

Are you willing to submit a pull request to fix this bug?
I'm happy to do it if I get some guidelines what would be the best way to address it.

@gziolo gziolo added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Sep 17, 2019
@mysticatea
Copy link
Member

mysticatea commented Sep 17, 2019

Thank you for your report.

Would you provide minimal repro steps?
The basePath should be the entry file's directory path rather than shareable config's directory path.

// Adopt the base path of the entry file (the outermost base path).
if (element.criteria) {
element.criteria.basePath = basePath;
}

@mysticatea
Copy link
Member

Ah, after re-read, it looks intentional behavior. Because you use node_modules/@wordpress/scripts/config/.eslintrc.js directly, the overrides path will be handled as relative to the node_modules/@wordpress/scripts/config/.eslintrc.js.

@mysticatea mysticatea added question This issue asks a question about ESLint and removed bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Sep 17, 2019
@gziolo
Copy link
Author

gziolo commented Sep 17, 2019

Ah, after re-read, it looks intentional behavior. Because you use node_modules/@wordpress/scripts/config/.eslintrc.js directly, the overrides path will be handled as relative to the node_modules/@wordpress/scripts/config/.eslintrc.js.

Yes, this is how it works but it prevents you from using a valid config file which happens to be located in the node_modules and uses overrides in nested configs. There should be at least a way to provide the alternative base path for file matchers.

@mysticatea
Copy link
Member

To clarify, is this the same issue as #11558? (glob-based configuration along with --config option?)

@mysticatea mysticatea added bug ESLint is working incorrectly core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed question This issue asks a question about ESLint labels Sep 17, 2019
@gziolo
Copy link
Author

gziolo commented Sep 17, 2019

It seems like you are correct. I can see it as the same issue once I read its full description.

This is what is executed behind the scenes in the script which uses eslint with the config located elswehere:

eslint --no-eslintrc --config  /Users/gziolo/PhpstormProjects/gutenberg-examples/node_modules/@wordpress/scripts/config/.eslintrc.js --ignore-path /Users/gziolo/PhpstormProjects/gutenberg-examples/node_modules/@wordpress/scripts/config/.eslintignore .

By the way, .eslintignore is resolved from the root of the project rather than the folder where it is located.

@mysticatea
Copy link
Member

mysticatea commented Sep 18, 2019

By the way, .eslintignore is resolved from the root of the project rather than the folder where it is located.

It's a bug because .eslintignore patterns should be resolved from the location of the file since ESLint 4.0.0.

From https://github.com/WordPress/gutenberg/blob/1c35075833c6fba8619ee0dcfe4596e610ceef48/packages/scripts/config/.eslintignore, it's intentional behavior because those patterns don't start with /. Those patterns are valid in all directories.

@mysticatea
Copy link
Member

Thank you for confirming. I'm closing this issue as a duplicate of #11558. It's intentional behavior, but I think we should change the behavior in the next major version.

@gziolo
Copy link
Author

gziolo commented Sep 19, 2019

From https://github.com/WordPress/gutenberg/blob/1c35075833c6fba8619ee0dcfe4596e610ceef48/packages/scripts/config/.eslintignore, it's intentional behavior because those patterns don't start with /. Those patterns are valid in all directories.

That's very interesting to learn. It looks like a lucky coincidence it works properly then. Thanks for opening eslint/rfcs/pull/37 which should change it to more anticipated behavior.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 17, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

No branches or pull requests

2 participants