microsoft / microsoft/TypeScript
`typeVersions` substitution occurs multiple times causing resolution to fail
@rbuckton is already working on this.
Since Nov 4, 2020.
- 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:
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.