microsoft / microsoft/TypeScript

`typeVersions` substitution occurs multiple times causing resolution to fail

Đang mở
#41,284 3 bình luận 3 reaction 1 người được giao Xem trên GitHub

@rbuckton đang làm issue này rồi.

Từ ngày 4/11/2020.

Needs Investigation Rescheduled
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

TypeScript Version: 4.0.3, 4.1.0-dev.20201027

When module direcotry is imported which is mapped to typedefs through typeVersions typescript ends up doing path substitution multiple times and ends up with a wrong lookup path leading to failed resolution. I have created minimal reproducible example https://github.com/Gozala/bug-tsc-path-substitution/tree/double-substitution

Where you can see that src/index.ts imports bar/src module which seems to do the following:

  1. typesVersion substitution is applied to 'src', which matched pattern '*'

    Trying substitution 'dist/*', candidate module location: 'dist/src'.

  2. Which triggers following lookups:
  • node_modules/bar/dist/src.ts
  • node_modules/bar/dist/src.tsx
  • node_modules/bar/dist/src.d.ts
  1. After neither found instead of looking up at node_modules/bar/dist/src/index.(ts,tsx,d.ts) it seems to look at 'package.json' 'main' field and do second path substitution:

    'package.json' has 'main' field 'src/index.js' that references 'node_modules/bar/dist/src/src/index.js'.

  2. Which fails because it ends up looking for dist/src/dist/src/index instead of dist/src/index.
  3. This is especially problematic because typescript seems to generate import paths like bar/src in place of bar as illustrated by node_modules/foo/ where original import was for bar but typedefs emitted have bar/src instead (for no apparent reason)

Search Terms:

  • module resolution
  • typeVersions
  • double substitution

Code

Expected behavior:

After considering following paths:

  • node_modules/bar/dist/src.ts
  • node_modules/bar/dist/src.tsx
  • node_modules/bar/dist/src.d.ts

Following paths should be considered

  • node_modules/bar/dist/src/index.ts
  • node_modules/bar/dist/src/index.tsx
  • node_modules/bar/dist/src/index.d.ts

Actual behavior:

Instead after considering following paths

After considering following paths:

  • node_modules/bar/dist/src.ts
  • node_modules/bar/dist/src.tsx
  • node_modules/bar/dist/src.d.ts

TS seems to do another path substitute for dist/src/index.js ends up with wrong path node_modules/bar/dist/src/dist/src/index.js which cases failure in finding dist/src/index.d.ts

Playground Link:

Can't demonstrate with playground, as many files are involved. Please see demo repo instead
https://github.com/Gozala/bug-tsc-path-substitution/tree/double-substitution

Related Issues:

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.