Skip to content

Commit 4ee3f2b

Browse files
committed
Watch subdirectories in project root instead of watching project root so as to avoid watching excluded directories that are not part of config file
Fixes #25629 and microsoft/vscode#51139
1 parent feca91c commit 4ee3f2b

File tree

6 files changed

+135
-111
lines changed

6 files changed

+135
-111
lines changed

src/compiler/resolutionCache.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,17 @@ namespace ts {
398398

399399
function getDirectoryToWatchFailedLookupLocation(failedLookupLocation: string, failedLookupLocationPath: Path): DirectoryOfFailedLookupWatch {
400400
if (isInDirectoryPath(rootPath, failedLookupLocationPath)) {
401-
// Always watch root directory recursively
402-
return { dir: rootDir!, dirPath: rootPath }; // TODO: GH#18217
401+
failedLookupLocation = isRootedDiskPath(failedLookupLocation) ? failedLookupLocation : getNormalizedAbsolutePath(failedLookupLocation, getCurrentDirectory());
402+
Debug.assert(failedLookupLocation.length === failedLookupLocationPath.length, `FailedLookup: ${failedLookupLocation} failedLookupLocationPath: ${failedLookupLocationPath}`); // tslint:disable-line
403+
const subDirectoryInRoot = failedLookupLocationPath.indexOf(directorySeparator, rootPath.length + 1);
404+
if (subDirectoryInRoot !== -1) {
405+
// Instead of watching root, watch directory in root to avoid watching excluded directories not needed for module resolution
406+
return { dir: failedLookupLocation.substr(0, subDirectoryInRoot), dirPath: failedLookupLocationPath.substr(0, subDirectoryInRoot) as Path };
407+
}
408+
else {
409+
// Always watch root directory non recursively
410+
return { dir: rootDir!, dirPath: rootPath, nonRecursive: false }; // TODO: GH#18217
411+
}
403412
}
404413

405414
return getDirectoryToWatchFromFailedLookupLocationDirectory(
@@ -478,6 +487,7 @@ namespace ts {
478487
customFailedLookupPaths.set(failedLookupLocationPath, refCount + 1);
479488
}
480489
if (dirPath === rootPath) {
490+
Debug.assert(!nonRecursive);
481491
setAtRoot = true;
482492
}
483493
else {
@@ -487,8 +497,8 @@ namespace ts {
487497
}
488498

489499
if (setAtRoot) {
490-
// This is always recursive
491-
setDirectoryWatcher(rootDir!, rootPath); // TODO: GH#18217
500+
// This is always non recursive
501+
setDirectoryWatcher(rootDir!, rootPath, /*nonRecursive*/ true); // TODO: GH#18217
492502
}
493503
}
494504

src/harness/virtualFileSystemWithWatch.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@ interface Array<T> {}`
583583
return;
584584
}
585585
this.invokeFileWatcher(fileOrDirectory.fullPath, FileWatcherEventKind.Created);
586+
if (isFsFolder(fileOrDirectory)) {
587+
this.invokeDirectoryWatcher(fileOrDirectory.fullPath, "");
588+
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, "");
589+
}
586590
this.invokeDirectoryWatcher(folder.fullPath, fileOrDirectory.fullPath);
587591
}
588592

@@ -599,12 +603,11 @@ interface Array<T> {}`
599603
this.invokeFileWatcher(fileOrDirectory.fullPath, FileWatcherEventKind.Deleted);
600604
if (isFsFolder(fileOrDirectory)) {
601605
Debug.assert(fileOrDirectory.entries.length === 0 || isRenaming);
602-
const relativePath = this.getRelativePathToDirectory(fileOrDirectory.fullPath, fileOrDirectory.fullPath);
603606
// Invoke directory and recursive directory watcher for the folder
604607
// Here we arent invoking recursive directory watchers for the base folders
605608
// since that is something we would want to do for both file as well as folder we are deleting
606-
this.invokeWatchedDirectoriesCallback(fileOrDirectory.fullPath, relativePath);
607-
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, relativePath);
609+
this.invokeWatchedDirectoriesCallback(fileOrDirectory.fullPath, "");
610+
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, "");
608611
}
609612

610613
if (basePath !== fileOrDirectory.path) {

src/testRunner/unittests/tscWatchMode.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2262,8 +2262,11 @@ declare module "fs" {
22622262
const files = [file, module, libFile];
22632263
const host = createWatchedSystem(files, { currentDirectory });
22642264
const watch = createWatchOfFilesAndCompilerOptions([file.path], host);
2265+
22652266
checkProgramActualFiles(watch(), [file.path, libFile.path]);
22662267
checkOutputErrorsInitial(host, [getDiagnosticModuleNotFoundOfFile(watch(), file, "qqq")]);
2268+
checkWatchedDirectories(host, emptyArray, /*recursive*/ false);
2269+
checkWatchedDirectories(host, [`${currentDirectory}/node_modules`, `${currentDirectory}/node_modules/@types`], /*recursive*/ true);
22672270

22682271
host.renameFolder(`${currentDirectory}/node_modules2`, `${currentDirectory}/node_modules`);
22692272
host.runQueuedTimeoutCallbacks();
@@ -2622,7 +2625,7 @@ declare module "fs" {
26222625
createWatchOfConfigFile("tsconfig.json", host);
26232626
checkWatchedFilesDetailed(host, [libFile.path, mainFile.path, config.path, linkedPackageIndex.path, linkedPackageOther.path], 1);
26242627
checkWatchedDirectories(host, emptyArray, /*recursive*/ false);
2625-
checkWatchedDirectoriesDetailed(host, [mainPackageRoot, linkedPackageRoot, `${mainPackageRoot}/node_modules/@types`, `${projectRoot}/node_modules/@types`], 1, /*recursive*/ true);
2628+
checkWatchedDirectoriesDetailed(host, [`${mainPackageRoot}/@scoped`, `${mainPackageRoot}/node_modules`, linkedPackageRoot, `${mainPackageRoot}/node_modules/@types`, `${projectRoot}/node_modules/@types`], 1, /*recursive*/ true);
26262629
});
26272630
});
26282631

0 commit comments

Comments
 (0)