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/no-unresolved on NX monorepos starting from version 3.8.x #376

Closed
estiller opened this issue Mar 12, 2025 · 11 comments · Fixed by #377
Closed

import/no-unresolved on NX monorepos starting from version 3.8.x #376

estiller opened this issue Mar 12, 2025 · 11 comments · Fixed by #377

Comments

@estiller
Copy link

estiller commented Mar 12, 2025

Hi,
Starting from version 3.8.x of eslint-import-resolver-typescript, I started getting import/no-unresolved on references between projects in my NX monorepo with aliases.

It worked fine until version 3.7.0, but it stopped working since. I also tested it on the recently released 3.8.6 but it's still the same issue.

There are some related issues starting from 3.8.x, but this issue wasn't resolved as part of the fixes:

I created a reproduction repo here. It has two libraries, with my-lib2 referencing my-lib.

Reproduction steps:

  1. Clone the repository and run pnpm install. This will install version 3.7.0.
  2. Run pnpm lint. Everything should work fine.
  3. Run pnpm add -D [email protected].
  4. Run pnpm lint and you will get a lint error.
   ✖  nx run my-lib2:lint
      > eslint .
      
      
      /Users/eran/estiller/eslint-import-resolver-issue/libs/my-lib2/src/lib/my-lib2.ts
        1:23  error  Unable to resolve path to module '@eslint-import-resolver-issue/my-lib'  import/no-unresolved
      
      ✖ 1 problem (1 error, 0 warnings)

I'd appreciate any help. Thanks!

@JounQin
Copy link
Collaborator

JounQin commented Mar 13, 2025

Can you helps to try next major version?

#368 (comment)

Although there are some special edge cases failing, but I think most things would just work as expected.

@estiller
Copy link
Author

Can you helps to try next major version?

#368 (comment)

Although there are some special edge cases failing, but I think most things would just work as expected.

@JounQin, how can I test it? I don't see a pre-release package on NPM.

@JounQin
Copy link
Collaborator

JounQin commented Mar 13, 2025

@estiller
Copy link
Author

@JounQin, no, I missed it. 😄

Anyway, it seems to work ok with this version:

pnpm add -D eslint-import-resolver-typescript@https://pkg.csb.dev/import-js/eslint-import-resolver-typescript/commit/1a15b976/eslint-import-resolver-typescript/_pkg.tgz

@JounQin
Copy link
Collaborator

JounQin commented Mar 13, 2025

@carlocorradini @SukkaW

I think v3.8+ bug fix at #345 brings more headaches than imagination, I'd like to revert back and leave the rest in next major rewrite at #368, what do you think?

@JounQin
Copy link
Collaborator

JounQin commented Mar 13, 2025

@JounQin, no, I missed it. 😄

Anyway, it seems to work ok with this version:

pnpm add -D eslint-import-resolver-typescript@https://pkg.csb.dev/import-js/eslint-import-resolver-typescript/commit/1a15b976/eslint-import-resolver-typescript/_pkg.tgz

@estiller Great to hear, did you notice any performance improvement?

@estiller
Copy link
Author

@estiller Great to hear, did you notice any performance improvement?

I just tried it on the "real" project, and on my local machine, it got the total lint time from ~15 seconds to ~13 seconds, which is a sizeable improvement given that we have other plugins we use as well.

@jmeinlschmidt
Copy link

same issue here, after downgrading to v3.7.0, the problem dissapears

@carlocorradini
Copy link
Contributor

I know the problem, etc...
I'll send an update ASAP

@carlocorradini
Copy link
Contributor

We have a subtle issue:
The root file eslint.config.mjs specifies the project as ./tsconfig.base.json. Each library has its own eslint.config.mjs file, which is an exact copy of the root file. The project is defined as tsconfig.base.json and is identical to the root. However, because the latter file is not present in each library directory, we fall back to reading the file tsconfig.json. The initialization of mappers is empty (ignored as we found 0 TypeScript files due to files: [] and include: []) because we do not currently support references (until the next major release), and as a result, we are unable to resolve the path.
A simple and effective solution:

--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -62,7 +62,7 @@ export default [
       ],
     },
     settings: {
-      'import/resolver': { typescript: { project: './tsconfig.base.json' } },
+      'import/resolver': { typescript: { project: './tsconfig.lib.json' } },
     },
   },
 ];

The paths are mapped correctly in PR #368 because it adds support for project references, so all linked/referenced `tsconfig.json are detected and mapped.
Nonetheless, we need to make a simple change and delete

if (files.length === 0) {

In PR #372, we added the ability to search for a path across all mappers if the current file is unknown. Therefore, even though the files are empty, we still need to add the mapper. The latter not only adds a minifx for #372, but it also allows your repro to work correctly (even if it's incorrect... amazing luck!)

@estiller
Copy link
Author

I can confirm that version 3.8.7 fixes this issue. Thank you so much!

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