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: eslint-plugin-import can't resolve typescript-eslint modules in flat eslint config #7565

Closed
4 tasks done
b0o opened this issue Aug 30, 2023 · 1 comment
Closed
4 tasks done
Labels
bug Something isn't working working as intended Issues that are closed as they are working as intended

Comments

@b0o
Copy link

b0o commented Aug 30, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Issue Description

I'm using typescript-eslint in conjunction with eslint-plugin-import. I've migrated to the new flat eslint config format. Here is a minimal reproduction of my issue:

import js from '@eslint/js'
import typescriptPlugin from '@typescript-eslint/eslint-plugin'
import typescriptParser from '@typescript-eslint/parser'
import importPlugin from 'eslint-plugin-import'

export default [
  js.configs.recommended,
  {
    files: ['**/*.@(js|ts|jsx|tsx)'],
    plugins: { import: importPlugin },
    rules: {
      ...importPlugin.configs.recommended.rules,
    },
  },
  {
    files: ['**/*.@(ts|tsx)'],
    languageOptions: {
      parser: typescriptParser,
      parserOptions: {
        sourceType: 'module',
        project: './tsconfig.eslint.json',
      },
    },
    settings: {
      ...importPlugin.configs.typescript.settings,
      'import/parsers': {
        '@typescript-eslint/parser': ['.ts', '.tsx', '.mts', '.cts'],
      },
    },
    plugins: {
      '@typescript-eslint': typescriptPlugin,
      import: importPlugin,
    },
    rules: {
      ...typescriptPlugin.configs.recommended.rules,
      ...typescriptPlugin.configs.strict.rules,
      ...importPlugin.configs.recommended.rules,
      ...importPlugin.configs.typescript.rules,
    },
  },
]

Naturally, I run eslint on the eslint.config.js file itself, which gives the following errors:

ts-eslint-import-bug/eslint.config.js
  2:30  error  Unable to resolve path to module '@typescript-eslint/eslint-plugin'  import/no-unresolved
  3:30  error  Unable to resolve path to module '@typescript-eslint/parser'         import/no-unresolved

✖ 2 problems (2 errors, 0 warnings)

This wasn't happening pre-6.0 - importing these modules worked without any issue.

I think the underlying issue is import-js/eslint-plugin-import#2132 and import-js/eslint-plugin-import#2703. It seems that starting in v6.0 of this repo, there is no longer a main field in package.json.

I tested my theory by manually adding "main": "./dist/index.js" to node_modules/@typescript-eslint/parser/package.json and node_modules/@typescript-eslint/eslint-plugin/package.json, and this resolved the eslint errors.

It seems this is also semi-related to #7300, wherein it was decided to not re-add the main field to package.json. Perhaps my issue can act as another data point supporting re-adding it?

Disclaimer: I understand that this is not a "bug" in typescript-eslint. Hopefully support for exports will come to eslint-plugin-import, but until then, it seems like this issue could be more easily be resolved here? (Would @ljharb like to chime in?)

Reproduction Repository Link

https://github.com/b0o/ts-eslint-import-bug

Repro Steps

  1. clone the repo
  2. npm install
  3. eslint eslint.config.js

Versions

package version
@typescript-eslint/eslint-plugin 6.5.0
@typescript-eslint/parser 6.5.0
eslint-plugin-import 2.28.1
TypeScript 5.2.2
ESLint 8.48.0
node 20.5.1
@b0o b0o added bug Something isn't working triage Waiting for maintainers to take a look labels Aug 30, 2023
@bradzacher
Copy link
Member

Note that we currently do not officially support flat configs and consuming our tooling without the compact layers will lead to incorrect behaviour!

I.e. Spreading configs like that is not going to properly include the config.


See the meta issue #7284 - it is intentional that we don't have a main declared as we no longer support <node14, and we do not support node10 module resolution in TS.


Also note that in TS projects we generally strongly recommend against a lot of the rules in eslint-plugin-import that require disk lookups. TS is already doing those lookups for you and without an extra import resolver package - so the additional checks are going to slow your lint runs down for little gain.

https://typescript-eslint.io/linting/troubleshooting/performance-troubleshooting#eslint-plugin-import


Closing, given all of the above.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2023
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for maintainers to take a look labels Aug 30, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants