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

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

Open
karlhorky opened this issue Jun 14, 2024 · 5 comments
Open

Comments

@karlhorky
Copy link

karlhorky commented Jun 14, 2024

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
Copy link
Author

karlhorky commented Jun 14, 2024

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
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 <rootdir>/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;

@karlhorky karlhorky changed the title Unable to resolve path to module on modules with 'bun:' prefix Unable to resolve path to module on bun or modules with bun: prefix Jun 14, 2024
@SukkaW
Copy link
Collaborator

SukkaW commented Jun 24, 2024

See also import-js/eslint-import-resolver-typescript#266

@SukkaW
Copy link
Collaborator

SukkaW commented Jul 11, 2024

import { isBuiltin } from 'node:module'

console.log(isBuiltin('bun:sqlite'))
// In Node.js: false
// In Bun: true

Thus we can't use isBuiltin here as ESLint might still be running under Node.js rather than Bun (E.g. in IDE).

@karlhorky
Copy link
Author

Ah just saw this other PR:

I guess once this is published and eslint-plugin-import-x upgrades to it, it will resolve the issue

@SukkaW
Copy link
Collaborator

SukkaW commented Jul 15, 2024

@karlhorky You don't have to upgrade eslint-plugin-import-x. You only need to upgrade eslint-import-resolver-typescript.

I just became the co-maintainer of eslint-import-resolver-typescript and I will release a new version of eslint-import-resolver-typescript as soon as possible.

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

2 participants