Skip to content

jimbloemkolk/issue-eslint-import-resolver-typescript-subpath-import

Repository files navigation

ESLint Import Resolution Issue

Bug Description

ESLint's eslint-import-resolver-typescript does not properly resolve Node.js subpath imports when multiple file extensions are specified in the array pattern. It only checks the first extension in the array and fails to fall back to subsequent extensions.

Expected Behavior

When a subpath import is configured with multiple extensions like:

"#app/ts-first/*": ["./src/ts-first/*.ts", "./src/ts-first/*.tsx"]

The resolver should try each extension in order until it finds a matching file, just like Node.js does.

Actual Behavior

The resolver only tries the first extension (.ts) and reports "Unable to resolve path to module" even when a .tsx file exists.

Setup

This reproduction case demonstrates the issue with two subpath imports having different extension orders:

  • #app/ts-first/* - lists .ts before .tsx
  • #app/tsx-first/* - lists .tsx before .ts

Each directory contains both helper.ts and helper.tsx files.

Reproduction

  1. Install dependencies:

    npm install
  2. Run ESLint:

    npm run lint
  3. Observe that #app/ts-first/helper resolves successfully (finds .ts first), while #app/tsx-first/helper fails to resolve (only checks .tsx, doesn't fall back to .ts).

  4. For detailed output:

    DEBUG=* npx eslint index.ts

Expected vs Actual

  • TypeScript correctly resolves both imports (verified with npm run check)
  • ESLint fails to resolve imports where the first extension doesn't match an existing file

Root Cause

The issue is in the underlying unrs-resolver package used by eslint-import-resolver-typescript. The unrs-resolver package only tries the first pattern in a subpath import array and does not fall back to subsequent patterns if the first one doesn't match.

Direct unrs-resolver Reproduction

A minimal test case using unrs-resolver directly (without ESLint) is provided in test-unrs-resolver.cjs:

node test-unrs-resolver.cjs

This test demonstrates that:

  1. #app/ts-first/helper-ts ✓ resolves (finds .ts file with first pattern)
  2. #app/ts-first/helper-tsx ✗ fails (only tries .ts pattern, doesn't try .tsx)
  3. #app/tsx-first/helper-ts ✗ fails (only tries .tsx pattern, doesn't try .ts)
  4. #app/tsx-first/helper-tsx ✓ resolves (finds .tsx file with first pattern)

The resolver only succeeds when the first extension pattern matches an existing file, proving the bug is in unrs-resolver itself.

About

Subpath imports handled differently in node/tsc than in eslint-import-resolver-typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors