microsoft / microsoft/TypeScript

`typeVersions` substitution occurs multiple times causing resolution to fail

Open
#41,284 3 comments 3 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Nov 4, 2020.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.