Skip to content

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

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

Closed
karlhorky opened this issue Jun 14, 2024 · 14 comments
Closed

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.

@SukkaW
Copy link
Collaborator

SukkaW commented Aug 26, 2024

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

@SukkaW SukkaW closed this as completed Aug 26, 2024
@karlhorky
Copy link
Author

karlhorky commented Aug 27, 2024

@SukkaW thanks for the heads up!

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

Relevant PR from @SunsetTechuila :

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

cc @Jarred-Sumner

@karlhorky
Copy link
Author

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

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?

@JounQin
Copy link
Member

JounQin commented Mar 25, 2025

@karlhorky Sorry for breaking, feel free to PR!

And considering import-js/eslint-import-resolver-typescript#402 will be released soon, some doc changes would be needed.


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

@karlhorky
Copy link
Author

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

@JounQin
Copy link
Member

JounQin commented Mar 25, 2025

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

@karlhorky
Copy link
Author

karlhorky commented Mar 25, 2025

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

I'll take a look at:

@JounQin
Copy link
Member

JounQin commented Mar 25, 2025

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

@karlhorky
Copy link
Author

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.

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