vscode-eslint: Error `unable to resolve path to module. eslint import/no-unresolved` but only in VSCode, not in ESLint CLI

I upgraded the package eslint-import-resolver-typescript from 3.7.0 to 4.2.1 and VSCode ESLint shows errors unable to resolve path to module. eslint import/no-unresolved on my imports.

Image

I created an issue on the eslint-import-resolver-typescript repo (https://github.com/import-js/eslint-import-resolver-typescript/issues/396) but as the error is related to VSCode and not ESLint CLI, the main contributor suggested me to create an issue on your repository. The package uses a napi module: rspack-resolver under the hood.

You can easily reproduct the issue by cloning my project : docker-express-postgres-boilerplate and upgrading the library eslint-import-resolver-typescript

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 48 (14 by maintainers)

Commits related to this issue

Most upvoted comments

I will close the issue since the fix will be provided by @JounQin in the ESLint plugin. Thank you @JounQin

@dbaeumer Did you check https://github.com/import-js/eslint-import-resolver-typescript/issues/447#issuecomment-2857344827?

I think this issue could happen when installing VSCode as arm64 arch, but installing unrs-resolver in terminal in x86_64 although I don’t know what’s happening, maybe the system Node binary is installed via Rosetta 2? cc @valleywood

Anyway, maybe a new target universal-apple-darwin for napi-rs would help.

@JounQin you can change the working directory using process.cwd('new path'). The cache I had in mind was https://github.com/un-ts/eslint-plugin-import-x/blob/master/src/utils/module-cache.ts but please be aware that I only looked through the code today for ~20 minutes.

@JounQin I investigate @auirarrazaval repro and here is what is happening:

  • since he uses working directories validating the files happens on different current working directories of the server (e.g. once process.cwd() is packages\eslint or apps\app-1 depending on which file is validated
  • the eslint server is a long running process. The directory changes are done by doing a process.cwd call
  • I looked that implementation here https://github.com/un-ts/eslint-plugin-import-x and it looks like you cache some module resolution.

Could it be that the caching is not expected the process.cwd to change? That would explain the different results depending on which files opens first.

OK, then that’s really a new issue compared with https://github.com/import-js/eslint-import-resolver-typescript/issues/447#issuecomment-2857344827… I have no idea.

Thank you @renchap!! After referencing TS config in ESLint config like

import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const tsConfigEsLintPath = path.resolve(__dirname, 'tsconfig.eslint.json');

const config = [
  ...
  {
    settings: {
      'import/resolver': {
        typescript: {
          project: tsConfigEsLintPath,
        },
      },
    },
  },
];

export default config;

(https://github.com/iwan-uschka/vscode-eslint-issue-1994/commit/36405e54a75cdfae0974b8758b8dc99f60dec880)

i couldn’t reproduce the issue 🎉 So cool. Hopefully the problem is now fixed and not just less flaky.


@dbaeumer Before in my demo monorepo i gave

"eslint.workingDirectories": [{ "pattern": "./{apps,packages}/*/" }]

and

"eslint.workingDirectories": ["./apps/storybook-ui", "./packages/ui", "./packages/ui2"]

a try without any luck.

I have this config:

    "eslint.workingDirectories": [
      { "pattern": "./packages/*/" },
    ],

I also tried to list the packages directly, but it still sometimes stop working randomly

Right now the only way I found to not have those random breakages is to hardcode the path in the eslint import config:

    settings: {
      "import/resolver": {
        typescript: {
          project: "/Users/renchap/dev/project/packages/web/tsconfig.json",
        },
      },
    },

If I omit it, or use a relative directory, after some point eslint ran by the vscode plugin stops resolving the paths proprely.

@Serhansolo I would really prefer a GitHub repository I can clone. This ensures that I do use the same exact content and settings as you do. This repository should also contain you local VS Code settings if you changed any.

@renchap do you have a GitHub repository I can clone that demos this. May be I can reproduce it there. Without being able to reproduce the error it is very hard to say what is the root cause of this.

I’m also facing the same issue. It seems to be reproducible when upgrading eslint-import-resolver-typescript to version 3.9.0 or later. For reference, I’m also using a macOS.