un-ts / eslint-plugin-import-x

A fork of `eslint-plugin-import` using `get-tsconfig` to replace `tsconfig-paths` and heavy `typescript` under the hood.
https://npm.im/eslint-plugin-import-x
MIT License
243 stars 12 forks source link

`Unable to resolve path to module` on `bun` or modules with `bun:` prefix #92

Open karlhorky opened 3 weeks ago

karlhorky commented 3 weeks ago

Importing modules with bun: prefix (eg. bun:test) or bun itself with @types/bun installed results in a resolution error with import-x/no-unresolved

Unable to resolve path to module 'bun:test'. eslint import-x/no-unresolved

index.test.ts

import { expect, test } from 'bun:test';

test('2 + 2', () => {
  expect(2 + 2).toBe(4);
});

Screenshot 2024-06-14 at 16 42 13

karlhorky commented 3 weeks ago

Configuration Solution / Workaround

If automating this based on the TS types in @types/bun is not possible / feasible / wanted, then one way that I've found to work around this is to use the ignore option of import-x/no-unresolved:

eslint.config.js

const configArray = [
    rules: {
      'import-x/no-unresolved':
-        'error',
+        [
+          'error',
+          {
+            ignore: ['^bun(:\\w+)?$'],
+          },
+        ],
    },
  },
];
Full config ```js import eslintTypescript from '@typescript-eslint/eslint-plugin'; import typescriptParser from '@typescript-eslint/parser'; import eslintImportX from 'eslint-plugin-import-x'; import globals from 'globals'; /** @type * {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigArray} * */ const configArray = [ { // Lint common extensions by default with rules above files: [ '**/*.js', '**/*.jsx', '**/*.cjs', '**/*.mjs', '**/*.ts', '**/*.tsx', '**/*.cts', '**/*.mts', ], languageOptions: { parser: typescriptParser, parserOptions: { project: './tsconfig.json', // typescript-eslint specific options warnOnUnsupportedTypeScriptVersion: true, }, globals: { ...globals.browser, ...globals.node, ...globals.commonjs, ...globals.es2021, // Allow using React as a global without importing it React: true, }, }, plugins: { '@typescript-eslint': { rules: eslintTypescript.rules, }, 'import-x': eslintImportX, }, settings: { 'import-x/parsers': { '@typescript-eslint/parser': ['.ts', '.tsx'], }, 'import-x/resolver': { // Load /tsconfig.json typescript: true, node: true, }, }, rules: { // Error on imports that don't match the underlying file // system // https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/no-unresolved.md 'import-x/no-unresolved': [ 'error', { ignore: ['^bun:\\w+$'], }, ], }, }, ]; export default configArray; ```
SukkaW commented 1 week ago

See also https://github.com/import-js/eslint-import-resolver-typescript/pull/266