Skip to content

Crash when package.json uses wrong type of value for typings, types, or main fields #35437

@juanmait

Description

@juanmait

TypeScript Version: 3.7.2

Search Terms:

  • charCodeAt
  • path.charCodeAt is not a function TSINTERNAL_ERROR
  • getDirectoryOrExtensionlessFileName
  • getEncodedRootLength

Code

const mainFileRelative = packageJsonContent.typings || packageJsonContent.types || packageJsonContent.main;

Expected behavior:
To handle cases in which mainFileRelative resolves to other than a string.

Actual behavior:
Crashes and unexpected bugs under some conditions when mainFileRelative resolves to for example an array. The result is then handled by getEncodedRootLength at:

function getEncodedRootLength(path: string): number {

Which end up calling charCodeAt in something that is not a string:

const ch0 = path.charCodeAt(0);

Failed to compile.

[internal]
TypeScript error in [internal](undefined,undefined):
path.charCodeAt is not a function  TSINTERNAL_ERROR
at: getDirectoryOrExtensionlessFileName
path: /***/***/ts-weird-bug/node_modules/csv-parse/lib/index.d.ts
content of types field: [ "./lib/index.d.ts", "./lib/sync.d.ts" ]

at: getEncodedRootLength

TypeError: path.charCodeAt is not a function
    at getEncodedRootLength (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:5830:27)
    at isRootedDiskPath (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:5742:16)
    at Object.toPath (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:6163:36)
    at getDirectoryOrExtensionlessFileName (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:99655:49)
    at tryGetModuleNameAsNodeModule (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:99631:70)
    at /***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:99451:88
    at Object.mapDefined (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:704:30)
    at Object.getModuleSpecifiers (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:99451:29)
    at getSpecifierForModuleSymbol (/***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:37232:62)
    at /***/***/ts-weird-bug/node_modules/typescript/lib/typescript.js:37098:39

Playground Link:

A repo that reproduces the issue: https://github.com/juanmait/ts-weird-bug. All the details are in the readme. Unfortunately the issue happen due to a combination of factors. A third party library that ships an invalid value for the field types in the package.json, some trickery made by react-scripts and this typescript bug (?

Related Issues:

#16048

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions