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

import-x/no-unresolved errors after updating eslint-import-resolver-typescript from 3.7.0 to 3.8.3 #364

Open
befabry opened this issue Feb 23, 2025 · 3 comments

Comments

@befabry
Copy link

befabry commented Feb 23, 2025

Hi,

As suggested in this comment, I'm opening a ticket to report an issue I encountered after updating eslint-import-resolver-typescript from version 3.7.0 to 3.8.3.

Context

I have two repositories:

  1. A backend repository using NestJS with a single tsconfig.json.
  2. A frontend repository using Vite & Preact with multiple tsconfig files (tsconfig.json, tsconfig.app.json, tsconfig.node.json).

The backend repository works fine, but the frontend repository started showing import-x/no-unresolved errors after the update.

Configuration

Backend ESLint Configuration:

settings: {
  'import/resolver-next': [
    createTypeScriptImportResolver({
      alwaysTryTypes: true,
      project: '<root>/tsconfig.json',
      extensions: ['.js', '.ts'],
    }),
  ],
},

Frontend ESLint Configuration:

settings: {
  'import/resolver-next': [
    createTypeScriptImportResolver({
      alwaysTryTypes: true,
      project: ['<root>/tsconfig.json', '<root>/tsconfig.app.json', '<root>/tsconfig.node.json'],
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
    }),
  ],
},

Issue

After updating from version 3.7.0 to 3.8.2 (and later to 3.8.3), my IDE stopped resolving aliased paths in the frontend repository. The import-x/no-unresolved errors started appearing.

Steps to Reproduce

  1. Clone the repository:
    git clone https://github.com/La-Fabryck/caddie-frontend-preact.git
  2. Switch to the branch:
    git switch bug/eslint-import-resolver-typescript
  3. Follow the README.md to install the project (up to step 4).
  4. Run the lint command:
    docker compose run --rm frontend npm run lint-fix
    This will show import-x/no-unresolved errors.
  5. Install version 3.7.0:
    docker compose run --rm frontend npm install [email protected]
  6. Run the lint command again:
    docker compose run --rm frontend npm run lint-fix
    No errors should appear.

Expected Behavior

The import-x/no-unresolved errors should not appear after updating to version 3.8.3, just as they don't appear in version 3.7.0.

Additional Information

  • The issue seems to be related to the multiple tsconfig files in the frontend repository.
  • The backend repository, which has a single tsconfig.json, does not exhibit this issue.

Thank you for looking into this!

@longzheng
Copy link

longzheng commented Feb 24, 2025

I also experienced this with the default Vite template which has multiple tsconfig configs using references.

I fixed it by manually specifying the project files using a glob in eslint.config.mjs but without <root>

import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript';

export default tseslint.config(
    {
        settings: {
            'import-x/resolver-next': [
                createTypeScriptImportResolver({
                    alwaysTryTypes: true,
                    project: `./tsconfig.*.json`,
                    // project: [`./tsconfig.node.json`, `./tsconfig.app.json`], // also works
                }),
            ],

@carlocorradini
Copy link
Contributor

@befabry Your repository has the following issues:

  1. You misspelled the configuration for the resolver:
    import-x/resolver-next rather than import/resolver-next
  2. Remove <root> from the project configuration as it does not work:
    project: ['tsconfig.json', 'tsconfig.app.json', 'tsconfig.node.json'] rather than project: ['<root>/tsconfig.json', '<root>/tsconfig.app.json', '<root>/tsconfig.node.json']

Explanation
Previously, it worked even when your configuration was completely ignored, because if no project is specified, it defaults to the tsconfig.json file in the current working directory.
Your tsconfig.json contains the paths entry, and versions prior to 3.8 blindly try every possible combination, choosing the first one.
With the new version, each file is/must be assigned to the appropriate mapping function. However, the tsconfig.json contains "files": [], which instructs TypeScript (and us) not to include anything. As a result, we fail to resolve the mapping because there is no mapping at all.

Hint
You can remove tsconfig.json from project because we do not yet support references and it does not contain any files (remember files: []).
When we will allow references, simply set project: true or completely omit it.

If you have additional doubts or questions, please do not hesitate to ask 🫡

@carlocorradini
Copy link
Contributor

@JounQin We can close this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants