fork-ts-checker-webpack-plugin: Strange "Cannot find module" error when using with TSLint and the no-unused-variable rule
This is a very weird bug which took me hours to whittle down to a minimal example, so I hope it’s of use. Steps to reproduce:
-
git clone https://github.com/pelotom/fork-ts-checker-bug cd fork-ts-checker-bug npm install npm start
- Everything should compile fine on the first run. Now open the file
foo.ts
, add a newline or some other innocuous change, and save it. - Now there is an error in a completely different file:
ERROR in /Users/tom/code/fork-ts-checker-bug/index.ts (1,25): Cannot find module 'moment'.
What the hey!?
Some observations:
- Undoing the change has no effect; the error remains. However modifying
index.ts
and saving it fixes the problem somehow. - Killing the watch process and restarting fixes the problem; first builds always work
- The problem only occurs with the
no-unused-variable
TSLint rule enabled - If another library besides
moment
is imported inindex.ts
, it may or may not exhibit the problem. For example,typestyle
exhibits the problem butrxjs
doesn’t. I’m not sure what the commonality is among packages that run afoul of this.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 33
- Comments: 18 (6 by maintainers)
@andrewbranch I changed the title
realized the project I was working with extended a few different configs, so I put
no-unused-variable
in asfalse
and its working.I stepped through the code and it looks like it happens for packages that specify
typings
in their package.json. Becauseno-unused-variable
pollutes the module resolution cache inSourceFile
instances, if they are reused after linting, and if the set of changed files does not cause typescript to re-resolve these packages, it fails to resolve them altogether.A quick workaround is to do this:
But obviously this has some perf impacts. I’m not sure how this can be properly fixed.