tleunen / eslint-import-resolver-babel-module

Custom eslint resolve for babel-plugin-module-resolver
https://github.com/tleunen/babel-plugin-module-resolver
MIT License
248 stars 31 forks source link

5.3.0 doesn't work with eslint-plugin-import #117

Closed adp-psych closed 3 years ago

adp-psych commented 3 years ago

Version 5.3.0 of eslint-import-resolver-babel-module causes problems for me with eslint-plugin-import:

  30:8  error  Unable to resolve path to module 'core-js/stable/index.js'  import/no-unresolved
  28:29  error  Unable to resolve path to module 'mersennetwister'  import/no-unresolved
  32:8   error  Unable to resolve path to module 'ramda'            import/no-unresolved

Downgrading to version 5.2.0 of eslint-import-resolver-babel-module (npm install --no-save eslint-import-resolver-babel-module@5.2.0) resolves the problem.

Here's my babel.config.js (with comments removed):

const moduleResolver = require('babel-plugin-module-resolver');

const moduleResolverConfiguration = {
        'alias': {
                '~': './src',
                '~build': './build',
        },
        'root': ['./src/'],
};

module.exports = (api) => {
        api.cache(true);
        return {
                'plugins': [
                        '@babel/plugin-proposal-throw-expressions',
                        'macros',
                        [moduleResolver, moduleResolverConfiguration],
                ],
                'presets': [
                        [
                                '@babel/preset-env',
                                {
                                        'corejs': {
                                                'proposals': true,
                                                'version': 3,
                                        },
                                        'useBuiltIns': 'entry',
                                },
                        ],
                        'power-assert',
                ],
        };
};

My full Babel and ESLint configurations are on npm as @adp-psych/babel-config and @adp-psych/eslint-config.

tleunen commented 3 years ago

Thank you for reporting the issue @adp-psych. Not being an active user of both the babel plugin or the eslint plugin, I might not be the best person anymore to fix these issues unfortunately. But between 5.2.0 and 5.3.0, we didn't have any major changes in our codebase. The only issue I could see is with one of the dependencies we updated. Therefore, can I ask you to run a few tests with your codebase and see if the update to pkg-up or resolve is the source of the issue?

Here are the changes between the 2 versions btw: https://github.com/tleunen/eslint-import-resolver-babel-module/compare/v5.2.0...v5.3.0

Interestingly, I also just notice 5.3.0 should have been a major update because of the node 10 requirement now. But we'll soon force node 12 anyway so I'll make sure to make a major release then.

adp-psych commented 3 years ago

Hi @tleunen! Thanks for your quick response.

I investigated as you requested, and it seems the problem is here:

const absoluteSrc = path.join(path.dirname(file), src);

When this runs, src is null, so path.join() fails.

Commenting out this line and the if statement/block after it fixes the problem.

tleunen commented 3 years ago

Thanks for investigating this!
@dword-design, could you also take a look at this, since you've added this piece of code?

dword-design commented 3 years ago

@adp-psych @tleunen I tried to setup a minimal project with your config but I think I need further files. I added all deps and then get this error:

Error: Error while loading rule 'notice/notice': Can't find templateFile @ /Users/foo/Desktop/test-module-resolver/templates/copyright.js

Do you have a minimal failing example? Also, I guess it is maybe related to the importing statement rather than the config (don't know though).

adp-psych commented 3 years ago

@dword-design, my ESLint configuration expects templates/copyright.js to contain the template for the copyright notice at the top of every source file. You can create templates/copyright.js if you like, but you should just be able to disable the rule (and any others that bother you) via .eslintrc.js:

module.exports = {
        'extends': '@adp-psych',
        'rules': {
                'notice/notice': 'off',
        },
};
dword-design commented 3 years ago

@adp-psych Alright I could reproduce the issue. Coming up with a fix soon.

dword-design commented 3 years ago

@adp-psych @tleunen This PR fixes the issue https://github.com/tleunen/eslint-import-resolver-babel-module/pull/118.

Problem was basically that the NPM import test was too specific for only testing babel plugin resolution. I replaced it with pkg-up, which failed then. Fix was pretty simple by surrounding my code with if. resolvePath only returns non-null if it is a local import (in contrast to an NPM module import).

tleunen commented 3 years ago

That's good to know! Thank you @dword-design for the explanation, and the fix :)