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 TS Project References #229

Open
carlocorradini opened this issue Jan 27, 2025 · 6 comments
Open

import-x/no-unresolved TS Project References #229

carlocorradini opened this issue Jan 27, 2025 · 6 comments

Comments

@carlocorradini
Copy link

I'm using TypeScript Project References, where project A depends on project B:

  • B's package.json is correctly configured to export cjs, esm, and types
  • A's package.json has entry B in dependencies
  • TypeScript successfully infers all types and goes to definitions.
    Even though projects A and B have not yet been built, everything is working properly (thanks to project references).
    Unfortunately, while importing B into A in a TypeScript file, ESLint cannot locate the files for B (it has not yet been built) and returns the error import-x/no-unresolved.
    Is it possible to instruct import-x to infer/use TypeScript project references or there is another trick or workaround?
    The only option I've found is to build project B, which will make import-x happy.
    Thanks
@tobigumo
Copy link

I ran into the same issue and discovered it might be related to eslint-import-resolver-typescript:
import-js/eslint-import-resolver-typescript#94

As noted in comment, you may be able to fix it by adding certain settings under import/resolver.
import-js/eslint-import-resolver-typescript#94 (comment)

However, because I was referencing a single shared tsconfig.json—which contained the common settings for two different projects—that adjustment alone did not work for me.
Ultimately, I fixed it by moving the shared settings into each project's individual tsconfig file instead.

It's not the prettiest solution, but hopefully it helps if you run into the same problem.

@SalahAdDin
Copy link

@tobigumo do you have any example for ESLint 9?

I'm unable to make it works for aliased paths.

@carlocorradini
Copy link
Author

@SalahAdDin Yup, I'll share it with you if you want.
Note that I'm waiting for import-js/eslint-import-resolver-typescript#299 and import-js/eslint-import-resolver-typescript#246 to be fixed

@SalahAdDin
Copy link

@carlocorradini Well, I followed the documentation about ESLint 9:

import js from '@eslint/js'
import eslintPluginImportX from 'eslint-plugin-import-x'
import tsParser from '@typescript-eslint/parser'

export default [
  js.configs.recommended,
  eslintPluginImportX.flatConfigs.recommended,
  eslintPluginImportX.flatConfigs.typescript,
  {
    files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
    ignores: ['eslint.config.js'],
    languageOptions: {
      parser: tsParser,
      ecmaVersion: 'latest',
      sourceType: 'module',
    },
    rules: {
      'no-unused-vars': 'off',
      'import-x/no-dynamic-require': 'warn',
      'import-x/no-nodejs-modules': 'warn',
    },
  },
]

I followed the steps but I have a problem:

/media/Storage/Projects/Experiments/react-ts-vite-template/src/presentation/components/ErrorBox/ErrorBox.test.tsx
  1:32  error  Unable to resolve path to module '@application/utils/test-utils'  import-x/no-unresolved

/media/Storage/Projects/Experiments/react-ts-vite-template/src/presentation/components/Spinner/Spiner.test.tsx
  1:32  error  Unable to resolve path to module '@application/utils/test-utils'  import-x/no-unresolved

@SalahAdDin
Copy link

I found the way to do it:

import tanStackQuery from "@tanstack/eslint-plugin-query";
import { createTypeScriptImportResolver } from "eslint-import-resolver-typescript";

export default tseslint.config(
  { ignores: ["coverage", "dist"] },
  sonarjs.configs.recommended,
  {
    extends: [importX.flatConfigs.recommended],
    languageOptions: {
      parser: tsParser,
      ecmaVersion: "latest",
    },
    settings: {
      "import-x/resolver-next": [
        createTypeScriptImportResolver({
          alwaysTryTypes: true,
          project: ["./tsconfig.app.json", "./tsconfig.node.json"],
        }),
      ],
    },
  },

It seems the plugin is unable to reach the nested typescript configuration files, so we have to add both to the project property.

createTypeScriptImportResolver is the only way to make it work, we tried the way to do it for mjs/cjs but It didn't work.

This solution works for both JS and TS files.

Is there anything I could improve in this configuration?

@carlocorradini
Copy link
Author

@SalahAdDin See import-js/eslint-import-resolver-typescript#94 (comment)

Nonetheless, there is the need to fix the other issues first :)

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

No branches or pull requests

3 participants