microsoft / microsoft/TypeScript

`typeVersions` substitution occurs multiple times causing resolution to fail

Aperta
#41,284 3 commenti 3 reazioni 1 assegnatario Vedi su GitHub

@rbuckton ci sta già lavorando.

Dal 4/11/2020.

Needs Investigation Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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:

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.