Skip to content

Commit 78c12cd

Browse files
authored
Merge pull request microsoft#22808 from Microsoft/moduleResolutionEvents
Invalidate resolutions from typeRoots watch event as a fallback mechanism
2 parents 6524fc5 + 306ed12 commit 78c12cd

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
@@ -6294,6 +6294,44 @@ namespace ts.projectSystem {
62946294
verifyNpmInstall(/*timeoutDuringPartialInstallation*/ false);
62956295
});
62966296
});
6297+
6298+
it("when node_modules dont receive event for the @types file addition", () => {
6299+
const projectLocation = "/user/username/folder/myproject";
6300+
const app: FileOrFolder = {
6301+
path: `${projectLocation}/app.ts`,
6302+
content: `import * as debug from "debug"`
6303+
};
6304+
const tsconfig: FileOrFolder = {
6305+
path: `${projectLocation}/tsconfig.json`,
6306+
content: ""
6307+
};
6308+
6309+
const files = [app, tsconfig, libFile];
6310+
const host = createServerHost(files);
6311+
const service = createProjectService(host);
6312+
service.openClientFile(app.path);
6313+
6314+
const project = service.configuredProjects.get(tsconfig.path);
6315+
checkProjectActualFiles(project, files.map(f => f.path));
6316+
assert.deepEqual(project.getLanguageService().getSemanticDiagnostics(app.path).map(diag => diag.messageText), ["Cannot find module 'debug'."]);
6317+
6318+
const debugTypesFile: FileOrFolder = {
6319+
path: `${projectLocation}/node_modules/@types/debug/index.d.ts`,
6320+
content: "export {}"
6321+
};
6322+
files.push(debugTypesFile);
6323+
// Do not invoke recursive directory watcher for anything other than node_module/@types
6324+
const invoker = host.invokeWatchedDirectoriesRecursiveCallback;
6325+
host.invokeWatchedDirectoriesRecursiveCallback = (fullPath, relativePath) => {
6326+
if (fullPath.endsWith("@types")) {
6327+
invoker.call(host, fullPath, relativePath);
6328+
}
6329+
};
6330+
host.reloadFS(files);
6331+
host.runQueuedTimeoutCallbacks();
6332+
checkProjectActualFiles(project, files.map(f => f.path));
6333+
assert.deepEqual(project.getLanguageService().getSemanticDiagnostics(app.path).map(diag => diag.messageText), []);
6334+
});
62976335
});
62986336

62996337
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)