plantain-00 / type-coverage

A CLI tool to check type coverage for typescript code
MIT License
1.26k stars 44 forks source link

Crash in type-coverage #47

Open robin-anil opened 4 years ago

robin-anil commented 4 years ago

Version(if relevant): 2.6.1

tsc runs successfully from the same folder. by type-coverage or typescript-coverage-report both errors out with the same stacktrace

Environment(if relevant):

Mac OS

$ npx typescript-coverage-report
TypeError: Cannot read property 'lastIndexOf' of undefined
    at Object.startsWith (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:2044:20)
    at isPathRelativeToParent (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102732:23)
    at /Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102691:24
    at Object.firstDefined (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:347:26)
    at getPathRelativeToRootDirs (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102689:23)
    at tryGetModuleNameFromRootDirs (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102550:40)
    at getLocalModuleSpecifier (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102408:44)
    at /Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102395:96
    at Array.map (<anonymous>)
    at Object.getModuleSpecifiers (/Users/robinanil/work/admin/dashboard/node_modules/typescript-coverage-report/node_modules/typescript/lib/typescript.js:102395:57)
plantain-00 commented 4 years ago
  1. The stacktrace shows no code about type-coverage
  2. type-coverage calls API provided by typescript, while tsc is not, it may be typescript's API's bug, eg: same error caused by getPathRelativeToRootDirs like https://github.com/microsoft/TypeScript/issues/37731
robin-anil commented 4 years ago

Is type-coverage correctly passing cwd as the base path? Reading that bug it sounds like it ought to do that to handle code bases like mine with relative paths

plantain-00 commented 4 years ago

I don't know how to do something to fix this bug. If there is a workaround, PR is welcome.

robin-anil commented 4 years ago

I haven't dug into this code base, so it may take some time for me to issue a patch. but I can show you that the following works. So if you can resolve to absolute path when you parse the tsconfig.json it may fix it.

npx type-coverage -p /Users/robinanil/work/admin/dashboard/tsconfig.json 363638 / 382102 95.16% type-coverage success.

robin-anil commented 4 years ago

I cannot figure out how to do this with typescript-coverage-report -- they don't seem to have a -p option. So the fix may need to go directly into code