Skip to content

Commit 79a1f29

Browse files
committed
In recursive directory watching ignore folders and files in node_modules starting with "."
Fixes microsoft#30004
1 parent d2364f5 commit 79a1f29

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/compiler/sys.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ namespace ts {
371371
else {
372372
directoryWatcher = {
373373
watcher: host.watchDirectory(dirName, fileName => {
374+
if (isInNodeModulesStartingWithDot(fileName)) return;
375+
374376
// Call the actual callback
375377
callbackCache.forEach((callbacks, rootDirName) => {
376378
if (rootDirName === dirPath || (startsWith(dirPath, rootDirName) && dirPath[rootDirName.length] === directorySeparator)) {
@@ -426,7 +428,7 @@ namespace ts {
426428
const childFullName = getNormalizedAbsolutePath(child, parentDir);
427429
// Filter our the symbolic link directories since those arent included in recursive watch
428430
// which is same behaviour when recursive: true is passed to fs.watch
429-
return filePathComparer(childFullName, normalizePath(host.realpath(childFullName))) === Comparison.EqualTo ? childFullName : undefined;
431+
return !isInNodeModulesStartingWithDot(childFullName) && filePathComparer(childFullName, normalizePath(host.realpath(childFullName))) === Comparison.EqualTo ? childFullName : undefined;
430432
}) : emptyArray,
431433
existingChildWatches,
432434
(child, childWatcher) => filePathComparer(child, childWatcher.dirName),
@@ -452,6 +454,16 @@ namespace ts {
452454
(newChildWatches || (newChildWatches = [])).push(childWatcher);
453455
}
454456
}
457+
458+
function isInNodeModulesStartingWithDot(path: string) {
459+
return isInPath(path, "/node_modules/.");
460+
}
461+
462+
function isInPath(path: string, searchPath: string) {
463+
if (stringContains(path, searchPath)) return true;
464+
if (host.useCaseSensitiveFileNames) return false;
465+
return stringContains(toCanonicalFilePath(path), searchPath);
466+
}
455467
}
456468

457469
// TODO: GH#18217 Methods on System are often used as if they are certainly defined

src/testRunner/unittests/tsserver/watchEnvironment.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,63 @@ namespace ts.projectSystem {
132132
verifyRootedDirectoryWatch("c:/users/username/");
133133
});
134134
});
135+
136+
it(`unittests:: tsserver:: watchEnvironment:: tsserverProjectSystem recursive watch directory implementation does not watch files/directories in node_modules starting with "."`, () => {
137+
const projectFolder = "/a/username/project";
138+
const projectSrcFolder = `${projectFolder}/src`;
139+
const configFile: File = {
140+
path: `${projectFolder}/tsconfig.json`,
141+
content: "{}"
142+
};
143+
const index: File = {
144+
path: `${projectSrcFolder}/index.ts`,
145+
content: `import {} from "file"`
146+
};
147+
const file1: File = {
148+
path: `${projectSrcFolder}/file1.ts`,
149+
content: ""
150+
};
151+
const nodeModulesExistingUnusedFile: File = {
152+
path: `${projectFolder}/node_modules/someFile.d.ts`,
153+
content: ""
154+
};
155+
156+
const fileNames = [index, file1, configFile, libFile].map(file => file.path);
157+
// All closed files(files other than index), project folder, project/src folder and project/node_modules/@types folder
158+
const expectedWatchedFiles = arrayToMap(fileNames.slice(1), identity, () => 1);
159+
const expectedWatchedDirectories = arrayToMap([projectFolder, projectSrcFolder, `${projectFolder}/${nodeModules}`, `${projectFolder}/${nodeModulesAtTypes}`], identity, () => 1);
160+
161+
const environmentVariables = createMap<string>();
162+
environmentVariables.set("TSC_WATCHDIRECTORY", Tsc_WatchDirectory.NonRecursiveWatchDirectory);
163+
const host = createServerHost([index, file1, configFile, libFile, nodeModulesExistingUnusedFile], { environmentVariables });
164+
const projectService = createProjectService(host);
165+
projectService.openClientFile(index.path);
166+
167+
const project = Debug.assertDefined(projectService.configuredProjects.get(configFile.path));
168+
verifyProject();
169+
170+
const nodeModulesIgnoredFileFromIgnoreDirectory: File = {
171+
path: `${projectFolder}/node_modules/.cache/someFile.d.ts`,
172+
content: ""
173+
};
174+
host.ensureFileOrFolder(nodeModulesIgnoredFileFromIgnoreDirectory);
175+
host.checkTimeoutQueueLength(0);
176+
verifyProject();
177+
178+
const nodeModulesIgnoredFile: File = {
179+
path: `${projectFolder}/node_modules/.cacheFile.ts`,
180+
content: ""
181+
};
182+
host.ensureFileOrFolder(nodeModulesIgnoredFile);
183+
host.checkTimeoutQueueLength(0);
184+
verifyProject();
185+
186+
function verifyProject() {
187+
checkWatchedDirectories(host, emptyArray, /*recursive*/ true);
188+
checkWatchedFilesDetailed(host, expectedWatchedFiles);
189+
checkWatchedDirectoriesDetailed(host, expectedWatchedDirectories, /*recursive*/ false);
190+
checkProjectActualFiles(project, fileNames);
191+
}
192+
});
193+
135194
}

0 commit comments

Comments
 (0)