eslint-plugin-import-x icon indicating copy to clipboard operation
eslint-plugin-import-x copied to clipboard

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

Open karlhorky opened this issue 1 year ago • 5 comments

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 avatar Jun 14 '24 14:06 karlhorky

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 avatar Jun 14 '24 14:06 karlhorky

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

SukkaW avatar Jun 24 '24 10:06 SukkaW

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).

SukkaW avatar Jul 11 '24 03:07 SukkaW

Ah just saw this other PR:

  • https://github.com/import-js/eslint-import-resolver-typescript/pull/288

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

karlhorky avatar Jul 15 '24 05:07 karlhorky

@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.

SukkaW avatar Jul 15 '24 06:07 SukkaW

The latest version of eslint-import-resolver-typescript (which was released about 15 hours ago) should have fixed the issue.

SukkaW avatar Aug 26 '24 23:08 SukkaW

@SukkaW thanks for the heads up!

Yeah indeed, I can confirm that [email protected] resolves the issue 🎉

Relevant PR from @SunsetTechuila :

  • https://github.com/import-js/eslint-import-resolver-typescript/pull/288

This uses the new is-bun-module package (GitHub), nice 👀

cc @Jarred-Sumner

karlhorky avatar Aug 27 '24 10:08 karlhorky

Ah, just upgraded dependencies and received Unable to resolve path to module 'bun:test' from import-x/no-unresolved again:

$ yarn eslint . --max-warnings 0
$ /home/runner/work/courses/courses/node_modules/.bin/eslint . --max-warnings 0
/home/runner/work/courses/courses/packages/curriculum-versions/curriculumVersions/2024-3-fall-pern-extensive-eu/drone/__tests__/smokeTest.test.ts
Error:   2:30  error  Unable to resolve path to module 'bun:test'  import-x/no-unresolved
/home/runner/work/courses/courses/packages/curriculum-versions/curriculumVersions/2025-1-winter-pern-extensive-eu/drone/__tests__/smokeTest.test.ts
Error:   2:30  error  Unable to resolve path to module 'bun:test'  import-x/no-unresolved
/home/runner/work/courses/courses/packages/fusion.upleveled.io/__tests__/smokeTestDevServer.test.ts
Error:   6:30  error  Unable to resolve path to module 'bun:test'  import-x/no-unresolved
/home/runner/work/courses/courses/packages/learn.upleveled.io/__tests__/smokeTestDevServer.test.ts
Error:   6:30  error  Unable to resolve path to module 'bun:test'  import-x/no-unresolved

Looking into the last dependency changes, it appears that [email protected] caused this change:

Minor Changes

  • https://github.com/import-js/eslint-import-resolver-typescript/pull/391 c8121e5 Thanks @JounQin! - feat: make is-bun-module as optional peer dependency

Technically this is a BREAKING CHANGE, but considering we just raise out v4 recently and this only affects bun users, bun --bun eslint even works without this dependency, so I'd consider this as a minor change.

So for bun users, there are three options:

  1. install is-bun-module dependency manually and use bun: true option
  2. run eslint with bun --bun eslint w/o bun: true option
  3. enable run#bun in bunfig.toml w/o bun: true option

@JounQin @SukkaW should this be added to the readme of eslint-plugin-import-x?

karlhorky avatar Mar 25 '25 09:03 karlhorky

@karlhorky Sorry for breaking, feel free to PR!

~~And considering https://github.com/import-js/eslint-import-resolver-typescript/pull/402 will be released soon,~~ some doc changes would be needed.


eslint-import-resolver-typescript v4.2.3 has just been released.

JounQin avatar Mar 25 '25 09:03 JounQin

@JounQin thanks for the release!

I'll try to track down what changes are needed (maybe even code changes to eslint-plugin-import-x?

Some things which I tried so far which didn't work:

  1. Adding is-bun-module as a dependency
  2. Upgrading to [email protected]

karlhorky avatar Mar 25 '25 09:03 karlhorky

@karlhorky No change needed in eslint-plugin-import-x.

Did you enable bun: true option for eslint-import-resolver-typescript? Or just try bun --bun eslint?

JounQin avatar Mar 25 '25 09:03 JounQin

Ah yeah, I forgot that I configured import-x/resolver in our shared config.

I'll take a look at:

  • [x] Reconfiguring this in eslint-config-upleveled https://github.com/upleveled/eslint-config-upleveled/commit/c01e0db0d0121344eb1bb5d8714fdbc811be2df1
  • [x] Document bun option in eslint-import-resolver-typescript https://github.com/import-js/eslint-import-resolver-typescript/pull/404
  • [x] Updating the eslint-plugin-import-x readme for the import-x/resolver config https://github.com/un-ts/eslint-plugin-import-x/pull/262

karlhorky avatar Mar 25 '25 09:03 karlhorky

@karlhorky I'd recommend using import-x/resolver-next when possible for flat config. See aslo https://github.com/import-js/eslint-import-resolver-typescript#eslintconfigjs

(This part is also missing in eslint-plugin-import-x's README itself unfortunately. 😂

JounQin avatar Mar 25 '25 10:03 JounQin

I'd recommend using import-x/resolver-next when possible for flat config.

Nice, this looks great! I'll wait until it's not marked next anymore to upgrade.

karlhorky avatar Mar 25 '25 10:03 karlhorky