microsoft / microsoft/TypeScript
`typeVersions` substitution occurs multiple times causing resolution to fail
@rbuckton y travaille déjà.
Depuis le 4/11/2020.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 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:
- typesVersion substitution is applied to 'src', which matched pattern '*'
Trying substitution 'dist/*', candidate module location: 'dist/src'.
- Which triggers following lookups:
node_modules/bar/dist/src.tsnode_modules/bar/dist/src.tsxnode_modules/bar/dist/src.d.ts
- 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'.
- Which fails because it ends up looking for
dist/src/dist/src/indexinstead ofdist/src/index. - This is especially problematic because typescript seems to generate import paths like
bar/srcin place ofbaras illustrated bynode_modules/foo/where original import was forbarbut typedefs emitted havebar/srcinstead (for no apparent reason)
Search Terms:
- module resolution
- typeVersions
- double substitution
Code
Expected behavior:
After considering following paths:
node_modules/bar/dist/src.tsnode_modules/bar/dist/src.tsxnode_modules/bar/dist/src.d.ts
Following paths should be considered
node_modules/bar/dist/src/index.tsnode_modules/bar/dist/src/index.tsxnode_modules/bar/dist/src/index.d.ts
Actual behavior:
Instead after considering following paths
After considering following paths:
node_modules/bar/dist/src.tsnode_modules/bar/dist/src.tsxnode_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:
- https://github.com/microsoft/TypeScript/issues/41281
- As mentioned earlier generated typedefs seems to add
/srcto otherwisebarimports (which don't exhibit this problem). Which on it's own seems surprising making this a lot more problematic.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Évaluation
Cette issue n'a pas encore été évaluée.