@@ -869,26 +869,28 @@ namespace ts {
869
869
870
870
function createTypeRootsWatch ( typeRootPath : Path , typeRoot : string ) : FileWatcher {
871
871
// Create new watch and recursive info
872
- return resolutionHost . watchTypeRootsDirectory ( typeRoot , fileOrDirectory => {
873
- const fileOrDirectoryPath = resolutionHost . toPath ( fileOrDirectory ) ;
874
- if ( cachedDirectoryStructureHost ) {
875
- // Since the file existence changed, update the sourceFiles cache
876
- cachedDirectoryStructureHost . addOrDeleteFileOrDirectory ( fileOrDirectory , fileOrDirectoryPath ) ;
877
- }
878
-
879
- // For now just recompile
880
- // We could potentially store more data here about whether it was/would be really be used or not
881
- // and with that determine to trigger compilation but for now this is enough
882
- hasChangedAutomaticTypeDirectiveNames = true ;
883
- resolutionHost . onChangedAutomaticTypeDirectiveNames ( ) ;
872
+ return canWatchTypeRootPath ( typeRootPath ) ?
873
+ resolutionHost . watchTypeRootsDirectory ( typeRoot , fileOrDirectory => {
874
+ const fileOrDirectoryPath = resolutionHost . toPath ( fileOrDirectory ) ;
875
+ if ( cachedDirectoryStructureHost ) {
876
+ // Since the file existence changed, update the sourceFiles cache
877
+ cachedDirectoryStructureHost . addOrDeleteFileOrDirectory ( fileOrDirectory , fileOrDirectoryPath ) ;
878
+ }
884
879
885
- // Since directory watchers invoked are flaky, the failed lookup location events might not be triggered
886
- // So handle to failed lookup locations here as well to ensure we are invalidating resolutions
887
- const dirPath = getDirectoryToWatchFailedLookupLocationFromTypeRoot ( typeRoot , typeRootPath ) ;
888
- if ( dirPath ) {
889
- scheduleInvalidateResolutionOfFailedLookupLocation ( fileOrDirectoryPath , dirPath === fileOrDirectoryPath ) ;
890
- }
891
- } , WatchDirectoryFlags . Recursive ) ;
880
+ // For now just recompile
881
+ // We could potentially store more data here about whether it was/would be really be used or not
882
+ // and with that determine to trigger compilation but for now this is enough
883
+ hasChangedAutomaticTypeDirectiveNames = true ;
884
+ resolutionHost . onChangedAutomaticTypeDirectiveNames ( ) ;
885
+
886
+ // Since directory watchers invoked are flaky, the failed lookup location events might not be triggered
887
+ // So handle to failed lookup locations here as well to ensure we are invalidating resolutions
888
+ const dirPath = getDirectoryToWatchFailedLookupLocationFromTypeRoot ( typeRoot , typeRootPath ) ;
889
+ if ( dirPath ) {
890
+ scheduleInvalidateResolutionOfFailedLookupLocation ( fileOrDirectoryPath , dirPath === fileOrDirectoryPath ) ;
891
+ }
892
+ } , WatchDirectoryFlags . Recursive ) :
893
+ noopFileWatcher ;
892
894
}
893
895
894
896
/**
@@ -906,7 +908,7 @@ namespace ts {
906
908
907
909
// we need to assume the directories exist to ensure that we can get all the type root directories that get included
908
910
// But filter directories that are at root level to say directory doesnt exist, so that we arent watching them
909
- const typeRoots = getEffectiveTypeRoots ( options , { directoryExists : directoryExistsForTypeRootWatch , getCurrentDirectory } ) ;
911
+ const typeRoots = getEffectiveTypeRoots ( options , { getCurrentDirectory } ) ;
910
912
if ( typeRoots ) {
911
913
mutateMap (
912
914
typeRootsWatches ,
@@ -922,12 +924,11 @@ namespace ts {
922
924
}
923
925
}
924
926
925
- /**
926
- * Use this function to return if directory exists to get type roots to watch
927
- * If we return directory exists then only the paths will be added to type roots
928
- * Hence return true for all directories except root directories which are filtered from watching
929
- */
930
- function directoryExistsForTypeRootWatch ( nodeTypesDirectory : string ) {
927
+ function canWatchTypeRootPath ( nodeTypesDirectory : string ) {
928
+ // If type roots is specified, watch that path
929
+ if ( resolutionHost . getCompilationSettings ( ) . typeRoots ) return true ;
930
+
931
+ // Otherwise can watch directory only if we can watch the parent directory of node_modules/@types
931
932
const dir = getDirectoryPath ( getDirectoryPath ( nodeTypesDirectory ) ) ;
932
933
const dirPath = resolutionHost . toPath ( dir ) ;
933
934
return dirPath === rootPath || canWatchDirectory ( dirPath ) ;
0 commit comments