Skip to content

Commit 4606a4b

Browse files
authored
Merge pull request microsoft#28209 from Microsoft/ignorePathsStartingWithDotInNodeModules
Ignore any changes to file or folder that are in node_modules and start with "."
2 parents 903e681 + c9fadf1 commit 4606a4b

File tree

6 files changed

+112
-11
lines changed

6 files changed

+112
-11
lines changed

src/compiler/resolutionCache.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ namespace ts {
7171
nonRecursive?: boolean;
7272
}
7373

74+
export function isPathInNodeModulesStartingWithDot(path: Path) {
75+
return stringContains(path, "/node_modules/.");
76+
}
77+
7478
export const maxNumberOfFilesToIterateForInvalidation = 256;
7579

7680
type GetResolutionWithResolvedFileName<T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName> =
@@ -691,6 +695,9 @@ namespace ts {
691695
isChangedFailedLookupLocation = location => isInDirectoryPath(fileOrDirectoryPath, resolutionHost.toPath(location));
692696
}
693697
else {
698+
// If something to do with folder/file starting with "." in node_modules folder, skip it
699+
if (isPathInNodeModulesStartingWithDot(fileOrDirectoryPath)) return false;
700+
694701
// Some file or directory in the watching directory is created
695702
// Return early if it does not have any of the watching extension or not the custom failed lookup path
696703
const dirOfFileOrDirectory = getDirectoryPath(fileOrDirectoryPath);

src/compiler/watch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ namespace ts {
938938
}
939939
nextSourceFileVersion(fileOrDirectoryPath);
940940

941+
if (isPathInNodeModulesStartingWithDot(fileOrDirectoryPath)) return;
942+
941943
// If the the added or created file or directory is not supported file name, ignore the file
942944
// But when watched directory is added/removed, we need to reload the file list
943945
if (fileOrDirectoryPath !== directory && hasExtension(fileOrDirectoryPath) && !isSupportedSourceFileName(fileOrDirectory, compilerOptions)) {

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ interface Array<T> {}`
587587
}
588588
this.invokeFileWatcher(fileOrDirectory.fullPath, FileWatcherEventKind.Created);
589589
if (isFsFolder(fileOrDirectory)) {
590-
this.invokeDirectoryWatcher(fileOrDirectory.fullPath, "");
591-
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, "");
590+
this.invokeDirectoryWatcher(fileOrDirectory.fullPath, fileOrDirectory.fullPath);
591+
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, fileOrDirectory.fullPath);
592592
}
593593
this.invokeDirectoryWatcher(folder.fullPath, fileOrDirectory.fullPath);
594594
}

src/server/editorServices.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ namespace ts.server {
964964
fileOrDirectory => {
965965
const fileOrDirectoryPath = this.toPath(fileOrDirectory);
966966
project.getCachedDirectoryStructureHost().addOrDeleteFileOrDirectory(fileOrDirectory, fileOrDirectoryPath);
967+
if (isPathInNodeModulesStartingWithDot(fileOrDirectoryPath)) return;
967968
const configFilename = project.getConfigFilePath();
968969

969970
// If the the added or created file or directory is not supported file name, ignore the file
@@ -2042,6 +2043,8 @@ namespace ts.server {
20422043
watchDir,
20432044
(fileOrDirectory) => {
20442045
const fileOrDirectoryPath = this.toPath(fileOrDirectory);
2046+
if (isPathInNodeModulesStartingWithDot(fileOrDirectoryPath)) return;
2047+
20452048
// Has extension
20462049
Debug.assert(result.refCount > 0);
20472050
if (watchDir === fileOrDirectoryPath) {

src/testRunner/unittests/tscWatchMode.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ namespace ts.tscWatch {
3434
return () => watch.getCurrentProgram().getProgram();
3535
}
3636

37-
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], host: WatchedSystem, options: CompilerOptions = {}) {
38-
const watch = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, host));
37+
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], host: WatchedSystem, options: CompilerOptions = {}, maxNumberOfFilesToIterateForInvalidation?: number) {
38+
const compilerHost = createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, host);
39+
compilerHost.maxNumberOfFilesToIterateForInvalidation = maxNumberOfFilesToIterateForInvalidation;
40+
const watch = createWatchProgram(compilerHost);
3941
return () => watch.getCurrentProgram().getProgram();
4042
}
4143

@@ -2467,6 +2469,46 @@ declare module "fs" {
24672469
checkProgramActualFiles(watch(), [file.path, libFile.path, `${currentDirectory}/node_modules/@types/qqq/index.d.ts`]);
24682470
checkOutputErrorsIncremental(host, emptyArray);
24692471
});
2472+
2473+
describe("ignores files/folder changes in node_modules that start with '.'", () => {
2474+
const projectPath = "/user/username/projects/project";
2475+
const npmCacheFile: File = {
2476+
path: `${projectPath}/node_modules/.cache/babel-loader/89c02171edab901b9926470ba6d5677e.ts`,
2477+
content: JSON.stringify({ something: 10 })
2478+
};
2479+
const file1: File = {
2480+
path: `${projectPath}/test.ts`,
2481+
content: `import { x } from "somemodule";`
2482+
};
2483+
const file2: File = {
2484+
path: `${projectPath}/node_modules/somemodule/index.d.ts`,
2485+
content: `export const x = 10;`
2486+
};
2487+
const files = [libFile, file1, file2];
2488+
const expectedFiles = files.map(f => f.path);
2489+
it("when watching node_modules in inferred project for failed lookup", () => {
2490+
const host = createWatchedSystem(files);
2491+
const watch = createWatchOfFilesAndCompilerOptions([file1.path], host, {}, /*maxNumberOfFilesToIterateForInvalidation*/ 1);
2492+
checkProgramActualFiles(watch(), expectedFiles);
2493+
host.checkTimeoutQueueLength(0);
2494+
2495+
host.ensureFileOrFolder(npmCacheFile);
2496+
host.checkTimeoutQueueLength(0);
2497+
});
2498+
it("when watching node_modules as part of wild card directories in config project", () => {
2499+
const config: File = {
2500+
path: `${projectPath}/tsconfig.json`,
2501+
content: "{}"
2502+
};
2503+
const host = createWatchedSystem(files.concat(config));
2504+
const watch = createWatchOfConfigFile(config.path, host);
2505+
checkProgramActualFiles(watch(), expectedFiles);
2506+
host.checkTimeoutQueueLength(0);
2507+
2508+
host.ensureFileOrFolder(npmCacheFile);
2509+
host.checkTimeoutQueueLength(0);
2510+
});
2511+
});
24702512
});
24712513

24722514
describe("tsc-watch with when module emit is specified as node", () => {

0 commit comments

Comments
 (0)