@@ -1157,26 +1157,28 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1157
1157
1158
1158
function createTypeRootsWatch ( typeRootPath : Path , typeRoot : string ) : FileWatcher {
1159
1159
// Create new watch and recursive info
1160
- return resolutionHost . watchTypeRootsDirectory ( typeRoot , fileOrDirectory => {
1161
- const fileOrDirectoryPath = resolutionHost . toPath ( fileOrDirectory ) ;
1162
- if ( cachedDirectoryStructureHost ) {
1163
- // Since the file existence changed, update the sourceFiles cache
1164
- cachedDirectoryStructureHost . addOrDeleteFileOrDirectory ( fileOrDirectory , fileOrDirectoryPath ) ;
1165
- }
1166
-
1167
- // For now just recompile
1168
- // We could potentially store more data here about whether it was/would be really be used or not
1169
- // and with that determine to trigger compilation but for now this is enough
1170
- hasChangedAutomaticTypeDirectiveNames = true ;
1171
- resolutionHost . onChangedAutomaticTypeDirectiveNames ( ) ;
1160
+ return canWatchTypeRootPath ( typeRootPath ) ?
1161
+ resolutionHost . watchTypeRootsDirectory ( typeRoot , fileOrDirectory => {
1162
+ const fileOrDirectoryPath = resolutionHost . toPath ( fileOrDirectory ) ;
1163
+ if ( cachedDirectoryStructureHost ) {
1164
+ // Since the file existence changed, update the sourceFiles cache
1165
+ cachedDirectoryStructureHost . addOrDeleteFileOrDirectory ( fileOrDirectory , fileOrDirectoryPath ) ;
1166
+ }
1172
1167
1173
- // Since directory watchers invoked are flaky, the failed lookup location events might not be triggered
1174
- // So handle to failed lookup locations here as well to ensure we are invalidating resolutions
1175
- const dirPath = getDirectoryToWatchFailedLookupLocationFromTypeRoot ( typeRoot , typeRootPath ) ;
1176
- if ( dirPath ) {
1177
- scheduleInvalidateResolutionOfFailedLookupLocation ( fileOrDirectoryPath , dirPath === fileOrDirectoryPath ) ;
1178
- }
1179
- } , WatchDirectoryFlags . Recursive ) ;
1168
+ // For now just recompile
1169
+ // We could potentially store more data here about whether it was/would be really be used or not
1170
+ // and with that determine to trigger compilation but for now this is enough
1171
+ hasChangedAutomaticTypeDirectiveNames = true ;
1172
+ resolutionHost . onChangedAutomaticTypeDirectiveNames ( ) ;
1173
+
1174
+ // Since directory watchers invoked are flaky, the failed lookup location events might not be triggered
1175
+ // So handle to failed lookup locations here as well to ensure we are invalidating resolutions
1176
+ const dirPath = getDirectoryToWatchFailedLookupLocationFromTypeRoot ( typeRoot , typeRootPath ) ;
1177
+ if ( dirPath ) {
1178
+ scheduleInvalidateResolutionOfFailedLookupLocation ( fileOrDirectoryPath , dirPath === fileOrDirectoryPath ) ;
1179
+ }
1180
+ } , WatchDirectoryFlags . Recursive ) :
1181
+ noopFileWatcher ;
1180
1182
}
1181
1183
1182
1184
/**
@@ -1194,7 +1196,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1194
1196
1195
1197
// we need to assume the directories exist to ensure that we can get all the type root directories that get included
1196
1198
// But filter directories that are at root level to say directory doesnt exist, so that we arent watching them
1197
- const typeRoots = getEffectiveTypeRoots ( options , { directoryExists : directoryExistsForTypeRootWatch , getCurrentDirectory } ) ;
1199
+ const typeRoots = getEffectiveTypeRoots ( options , { getCurrentDirectory } ) ;
1198
1200
if ( typeRoots ) {
1199
1201
mutateMap (
1200
1202
typeRootsWatches ,
@@ -1210,12 +1212,11 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1210
1212
}
1211
1213
}
1212
1214
1213
- /**
1214
- * Use this function to return if directory exists to get type roots to watch
1215
- * If we return directory exists then only the paths will be added to type roots
1216
- * Hence return true for all directories except root directories which are filtered from watching
1217
- */
1218
- function directoryExistsForTypeRootWatch ( nodeTypesDirectory : string ) {
1215
+ function canWatchTypeRootPath ( nodeTypesDirectory : string ) {
1216
+ // If type roots is specified, watch that path
1217
+ if ( resolutionHost . getCompilationSettings ( ) . typeRoots ) return true ;
1218
+
1219
+ // Otherwise can watch directory only if we can watch the parent directory of node_modules/@types
1219
1220
const dir = getDirectoryPath ( getDirectoryPath ( nodeTypesDirectory ) ) ;
1220
1221
const dirPath = resolutionHost . toPath ( dir ) ;
1221
1222
return dirPath === rootPath || canWatchDirectoryOrFile ( dirPath ) ;
0 commit comments