Skip to content

Commit 306ed12

Browse files
committed
Invalidate resolutions from typeRoots watch event as a fallback mechanism
There are times when the directory watcher for failed lookup locations isnt invoked and is not very determinitstic So as a fallback, when we receive typeRoots watch event, handle failed lookup locations as well
1 parent 67670d8 commit 306ed12

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

src/compiler/resolutionCache.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,13 @@ namespace ts {
361361
return { dir: rootDir, dirPath: rootPath };
362362
}
363363

364-
let dir = getDirectoryPath(getNormalizedAbsolutePath(failedLookupLocation, getCurrentDirectory()));
365-
let dirPath = getDirectoryPath(failedLookupLocationPath);
364+
return getDirectoryToWatchFromFailedLookupLocationDirectory(
365+
getDirectoryPath(getNormalizedAbsolutePath(failedLookupLocation, getCurrentDirectory())),
366+
getDirectoryPath(failedLookupLocationPath)
367+
);
368+
}
366369

370+
function getDirectoryToWatchFromFailedLookupLocationDirectory(dir: string, dirPath: Path) {
367371
// If directory path contains node module, get the most parent node_modules directory for watching
368372
while (stringContains(dirPath, "/node_modules/")) {
369373
dir = getDirectoryPath(dir);
@@ -621,7 +625,19 @@ namespace ts {
621625
clearMap(typeRootsWatches, closeFileWatcher);
622626
}
623627

624-
function createTypeRootsWatch(_typeRootPath: string, typeRoot: string): FileWatcher {
628+
function getDirectoryToWatchFailedLookupLocationFromTypeRoot(typeRoot: string, typeRootPath: Path): Path | undefined {
629+
if (allFilesHaveInvalidatedResolution) {
630+
return undefined;
631+
}
632+
633+
if (isInDirectoryPath(rootPath, typeRootPath)) {
634+
return rootPath;
635+
}
636+
const { dirPath, ignore } = getDirectoryToWatchFromFailedLookupLocationDirectory(typeRoot, typeRootPath);
637+
return !ignore && directoryWatchesOfFailedLookups.has(dirPath) && dirPath;
638+
}
639+
640+
function createTypeRootsWatch(typeRootPath: Path, typeRoot: string): FileWatcher {
625641
// Create new watch and recursive info
626642
return resolutionHost.watchTypeRootsDirectory(typeRoot, fileOrDirectory => {
627643
const fileOrDirectoryPath = resolutionHost.toPath(fileOrDirectory);
@@ -634,6 +650,13 @@ namespace ts {
634650
// We could potentially store more data here about whether it was/would be really be used or not
635651
// and with that determine to trigger compilation but for now this is enough
636652
resolutionHost.onChangedAutomaticTypeDirectiveNames();
653+
654+
// Since directory watchers invoked are flaky, the failed lookup location events might not be triggered
655+
// So handle to failed lookup locations here as well to ensure we are invalidating resolutions
656+
const dirPath = getDirectoryToWatchFailedLookupLocationFromTypeRoot(typeRoot, typeRootPath);
657+
if (dirPath && invalidateResolutionOfFailedLookupLocation(fileOrDirectoryPath, dirPath === fileOrDirectoryPath)) {
658+
resolutionHost.onInvalidatedResolution();
659+
}
637660
}, WatchDirectoryFlags.Recursive);
638661
}
639662

src/harness/unittests/tsserverProjectSystem.ts

+38
Original file line numberDiff line numberDiff line change
@@ -6211,6 +6211,44 @@ namespace ts.projectSystem {
62116211
verifyNpmInstall(/*timeoutDuringPartialInstallation*/ false);
62126212
});
62136213
});
6214+
6215+
it("when node_modules dont receive event for the @types file addition", () => {
6216+
const projectLocation = "/user/username/folder/myproject";
6217+
const app: FileOrFolder = {
6218+
path: `${projectLocation}/app.ts`,
6219+
content: `import * as debug from "debug"`
6220+
};
6221+
const tsconfig: FileOrFolder = {
6222+
path: `${projectLocation}/tsconfig.json`,
6223+
content: ""
6224+
};
6225+
6226+
const files = [app, tsconfig, libFile];
6227+
const host = createServerHost(files);
6228+
const service = createProjectService(host);
6229+
service.openClientFile(app.path);
6230+
6231+
const project = service.configuredProjects.get(tsconfig.path);
6232+
checkProjectActualFiles(project, files.map(f => f.path));
6233+
assert.deepEqual(project.getLanguageService().getSemanticDiagnostics(app.path).map(diag => diag.messageText), ["Cannot find module 'debug'."]);
6234+
6235+
const debugTypesFile: FileOrFolder = {
6236+
path: `${projectLocation}/node_modules/@types/debug/index.d.ts`,
6237+
content: "export {}"
6238+
};
6239+
files.push(debugTypesFile);
6240+
// Do not invoke recursive directory watcher for anything other than node_module/@types
6241+
const invoker = host.invokeWatchedDirectoriesRecursiveCallback;
6242+
host.invokeWatchedDirectoriesRecursiveCallback = (fullPath, relativePath) => {
6243+
if (fullPath.endsWith("@types")) {
6244+
invoker.call(host, fullPath, relativePath);
6245+
}
6246+
};
6247+
host.reloadFS(files);
6248+
host.runQueuedTimeoutCallbacks();
6249+
checkProjectActualFiles(project, files.map(f => f.path));
6250+
assert.deepEqual(project.getLanguageService().getSemanticDiagnostics(app.path).map(diag => diag.messageText), []);
6251+
});
62146252
});
62156253

62166254
describe("tsserverProjectSystem ProjectsChangedInBackground", () => {

src/harness/virtualFileSystemWithWatch.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ interface Array<T> {}`
547547
// Invoke directory and recursive directory watcher for the folder
548548
// Here we arent invoking recursive directory watchers for the base folders
549549
// since that is something we would want to do for both file as well as folder we are deleting
550-
invokeWatcherCallbacks(this.watchedDirectories.get(fileOrDirectory.path), cb => this.directoryCallback(cb, relativePath));
551-
invokeWatcherCallbacks(this.watchedDirectoriesRecursive.get(fileOrDirectory.path), cb => this.directoryCallback(cb, relativePath));
550+
this.invokeWatchedDirectoriesCallback(fileOrDirectory.fullPath, relativePath);
551+
this.invokeWatchedDirectoriesRecursiveCallback(fileOrDirectory.fullPath, relativePath);
552552
}
553553

554554
if (basePath !== fileOrDirectory.path) {
@@ -561,9 +561,17 @@ interface Array<T> {}`
561561
}
562562
}
563563

564-
private invokeFileWatcher(fileFullPath: string, eventKind: FileWatcherEventKind) {
565-
const callbacks = this.watchedFiles.get(this.toPath(fileFullPath));
566-
invokeWatcherCallbacks(callbacks, ({ cb }) => cb(fileFullPath, eventKind));
564+
// For overriding the methods
565+
invokeWatchedDirectoriesCallback(folderFullPath: string, relativePath: string) {
566+
invokeWatcherCallbacks(this.watchedDirectories.get(this.toPath(folderFullPath)), cb => this.directoryCallback(cb, relativePath));
567+
}
568+
569+
invokeWatchedDirectoriesRecursiveCallback(folderFullPath: string, relativePath: string) {
570+
invokeWatcherCallbacks(this.watchedDirectoriesRecursive.get(this.toPath(folderFullPath)), cb => this.directoryCallback(cb, relativePath));
571+
}
572+
573+
invokeFileWatcher(fileFullPath: string, eventKind: FileWatcherEventKind, useFileNameInCallback?: boolean) {
574+
invokeWatcherCallbacks(this.watchedFiles.get(this.toPath(fileFullPath)), ({ cb, fileName }) => cb(useFileNameInCallback ? fileName : fileFullPath, eventKind));
567575
}
568576

569577
private getRelativePathToDirectory(directoryFullPath: string, fileFullPath: string) {
@@ -576,8 +584,8 @@ interface Array<T> {}`
576584
private invokeDirectoryWatcher(folderFullPath: string, fileName: string) {
577585
const relativePath = this.getRelativePathToDirectory(folderFullPath, fileName);
578586
// Folder is changed when the directory watcher is invoked
579-
invokeWatcherCallbacks(this.watchedFiles.get(this.toPath(folderFullPath)), ({ cb, fileName }) => cb(fileName, FileWatcherEventKind.Changed));
580-
invokeWatcherCallbacks(this.watchedDirectories.get(this.toPath(folderFullPath)), cb => this.directoryCallback(cb, relativePath));
587+
this.invokeFileWatcher(folderFullPath, FileWatcherEventKind.Changed, /*useFileNameInCallback*/ true);
588+
this.invokeWatchedDirectoriesCallback(folderFullPath, relativePath);
581589
this.invokeRecursiveDirectoryWatcher(folderFullPath, fileName);
582590
}
583591

@@ -590,7 +598,7 @@ interface Array<T> {}`
590598
*/
591599
private invokeRecursiveDirectoryWatcher(fullPath: string, fileName: string) {
592600
const relativePath = this.getRelativePathToDirectory(fullPath, fileName);
593-
invokeWatcherCallbacks(this.watchedDirectoriesRecursive.get(this.toPath(fullPath)), cb => this.directoryCallback(cb, relativePath));
601+
this.invokeWatchedDirectoriesRecursiveCallback(fullPath, relativePath);
594602
const basePath = getDirectoryPath(fullPath);
595603
if (this.getCanonicalFileName(fullPath) !== this.getCanonicalFileName(basePath)) {
596604
this.invokeRecursiveDirectoryWatcher(basePath, fileName);

0 commit comments

Comments
 (0)