microsoft / microsoft/TypeScript

`typeVersions` substitution occurs multiple times causing resolution to fail

未关闭
#41,284 3 条评论 3 个 reaction 已指派 1 人 在 GitHub 查看

@rbuckton 已经在做这个了。

开始于 2020年11月4日。

Needs Investigation Rescheduled
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

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:

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。