@@ -1157,26 +1157,28 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11571157
11581158 function createTypeRootsWatch ( typeRootPath : Path , typeRoot : string ) : FileWatcher {
11591159 // 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+ }
11721167
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 ;
11801182 }
11811183
11821184 /**
@@ -1194,7 +1196,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11941196
11951197 // we need to assume the directories exist to ensure that we can get all the type root directories that get included
11961198 // 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 } ) ;
11981200 if ( typeRoots ) {
11991201 mutateMap (
12001202 typeRootsWatches ,
@@ -1210,12 +1212,11 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
12101212 }
12111213 }
12121214
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
12191220 const dir = getDirectoryPath ( getDirectoryPath ( nodeTypesDirectory ) ) ;
12201221 const dirPath = resolutionHost . toPath ( dir ) ;
12211222 return dirPath === rootPath || canWatchDirectoryOrFile ( dirPath ) ;
0 commit comments