Skip to content

Annotated dynamic import should not cause crash package name resolving #41332

@villelaitila

Description

@villelaitila

This issue is originated from madge pahen/madge#211

mangeScopedPackageName function crashes when madge is running classicNameResolver (typescript/lib/typescript.js:27324:24) for a TypeScript file that contains annotated dynamic imports such as

TypeScript Version: 4.0.5

Fails also with 3.9.5 that is the one that the most recent madge uses.

Search Terms: annoted dynamic imports

Code

Create a testfile.ts with following content:

const Graph =  React.lazy(() => import(/* webpackChunkName: 'react-graph-vis' */ `react-graph-vis`))

Initiate classicNameResolver of TypeScritp by running madge this way: cd madge && node bin/cli.js ../testfile.ts

Expected behavior: Program completes without error

**Actual behavior: Program crashes with TypeError: Cannot read property 'lastIndexOf' of undefined **

Below traceback is from TypeScript 3.9.5 version but the behavior is similar in the 4.0.5 version. Line numbers in the call stack do differ.

✖ TypeError: Cannot read property 'lastIndexOf' of undefined
    at Object.startsWith (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:2060:20)
    at mangleScopedPackageName (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30544:16)
    at mangleScopedPackageNameWithTrace (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30530:23)
    at loadModuleFromImmediateNodeModulesDirectory (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30461:83)
    at /usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30438:39
    at Object.forEachAncestorDirectory (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:6786:26)
    at loadModuleFromNearestNodeModulesDirectoryWorker (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30432:19)
    at loadModuleFromNearestNodeModulesDirectoryTypesScope (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30428:16)
    at tryResolve (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30609:28)
    at classicNameResolver (/usr/lib/node_modules/madge/node_modules/typescript/lib/typescript.js:30585:24)

Playground Link:

Related Issues:
Nothing found from TypeScript repo, but this one is the relevant related in the madge repository.
pahen/madge#211

See also comment
pahen/madge#211 (comment) that demonstrates how mangeScopedPackageName could be changed to avoid the crash.

Metadata

Metadata

Assignees

Labels

Needs More InfoThe issue still hasn't been fully clarified

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions