Eslint-plugin-import: [BUG] ESLint crashes with `import/no-unused-modules` and `src` option

Created on 21 Apr 2019  路  12Comments  路  Source: benmosher/eslint-plugin-import

This issue is probably related to #1323. It is slightly different though. It's also using the latest version of eslint-plugin-import, including the fix of #1323.

ESLint crashes with the following error:

$ eslint .
TypeError: Cannot read property 'get' of undefined
Occurred while linting /home/ehmicky/no-unused-modules-bug/file.js:1
    at checkUsage (/home/ehmicky/no-unused-modules-bug/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:339:33)
    at ExportNamedDeclaration.node.declaration.declarations.forEach.declaration (/home/ehmicky/no-unused-modules-bug/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:732:15)
    at Array.forEach (<anonymous>)
    at ExportNamedDeclaration (/home/ehmicky/no-unused-modules-bug/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:731:43)
    at listeners.(anonymous function).forEach.listener (/home/ehmicky/no-unused-modules-bug/node_modules/eslint/lib/util/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/home/ehmicky/no-unused-modules-bug/node_modules/eslint/lib/util/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/ehmicky/no-unused-modules-bug/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/home/ehmicky/no-unused-modules-bug/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/home/ehmicky/no-unused-modules-bug/node_modules/eslint/lib/util/node-event-generator.js:294:14)

The current directory contains the following files:

src/main.js:

export const main = true

file.js:

export const test = true

.eslintrc.json:

{
  "parserOptions": {
    "ecmaVersion": 2019,
    "sourceType": "module"
  },
  "plugins": ["import"],
  "rules": {
    "import/no-unused-modules": [
      2,
      {
        "unusedExports": true,
        "src": ["src/"]
      }
    ]
  }
}

package.json:

{
  "name": "no-unused-modules-bug",
  "version": "1.0.0",
  "dependencies": {},
  "devDependencies": {
    "eslint": "^5.16.0",
    "eslint-plugin-import": "^2.17.2"
  }
}
bug help wanted

Most helpful comment

Yes, I can confirm that's working with this fix.
Sorry by not giving a demo in time. I was having some difficulties to reproduce it without knowing the right conditions to do it since my working project is kinda big to track all possibilities in mind.
Once @rfermann told us the problem, I could reproduce it in my WIP demo.
Thanks @rfermann!

All 12 comments

cc @rfermann

@ljharb: thanks for adding. I will tackle this issue after #1339 is solved.

Thanks @rfermann!

I also got this problem. However I didn't need to specify a src option to crash my process, so the default value is enough to reproduce.
My config is:

import/no-unused-modules:
  - error
  - missingExports: true
    unusedExports: true

@dfleury could you share a minimally reproducible example? Including:

  • eslint config
  • files to lint
  • how eslint is spawned (e.g. eslint command)
  • error message produced
  • versions (node, eslint, eslint-plugin-import)

Like this the maintainers can make sure your issue is fixed together with mine (if they have the same cause).

The problem was, that there was a file to be linted which was outside the specified src folder. These files should be excluded from being linted by this rule.

@dfleury can you verify that you are linting files outside the src folder (process.cwd() by default)?

This is fixing my problem, thanks!

@dfleury I think this issue should be closed after this PR is merged. If the PR does not solve your issue, then it means it was a separate problem, i.e. should be a separate issue. What do you think?

@ljharb Would it be possible to release a new version? Thanks!

As soon as i have the time :-) it is a weekend, after all.

Absolutely, there is no rush! :)
Thanks for the bug fix.

Yes, I can confirm that's working with this fix.
Sorry by not giving a demo in time. I was having some difficulties to reproduce it without knowing the right conditions to do it since my working project is kinda big to track all possibilities in mind.
Once @rfermann told us the problem, I could reproduce it in my WIP demo.
Thanks @rfermann!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

silvenon picture silvenon  路  3Comments

pcorpet picture pcorpet  路  3Comments

daltonamitchell picture daltonamitchell  路  3Comments

migueloller picture migueloller  路  3Comments

benmosher picture benmosher  路  3Comments