Skip to content

Commit 7ac5fa7

Browse files
author
Benjamin Lichtman
committed
Refactor and add wildcard scenario
1 parent 6b92cca commit 7ac5fa7

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

src/compiler/resolutionCache.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ namespace ts {
699699
// If something to do with folder/file starting with "." in node_modules folder, skip it
700700
if (isPathIgnored(fileOrDirectoryPath)) return false;
701701

702+
// prevent saving an open file from over-eagerly triggering invalidation
703+
if (resolutionHost.fileIsOpen(fileOrDirectoryPath)) {
704+
return false;
705+
}
706+
702707
// Some file or directory in the watching directory is created
703708
// Return early if it does not have any of the watching extension or not the custom failed lookup path
704709
const dirOfFileOrDirectory = getDirectoryPath(fileOrDirectoryPath);
@@ -714,10 +719,6 @@ namespace ts {
714719
if (!isPathWithDefaultFailedLookupExtension(fileOrDirectoryPath) && !customFailedLookupPaths.has(fileOrDirectoryPath)) {
715720
return false;
716721
}
717-
// prevent saving an open file from over-eagerly triggering invalidation
718-
if (resolutionHost.fileIsOpen(fileOrDirectoryPath)) {
719-
return false;
720-
}
721722
// Ignore emits from the program
722723
if (isEmittedFileOfProgram(resolutionHost.getCurrentProgram(), fileOrDirectoryPath)) {
723724
return false;

src/server/editorServices.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,13 @@ namespace ts.server {
10021002
directory,
10031003
fileOrDirectory => {
10041004
const fileOrDirectoryPath = this.toPath(fileOrDirectory);
1005-
project.getCachedDirectoryStructureHost().addOrDeleteFileOrDirectory(fileOrDirectory, fileOrDirectoryPath);
1005+
const fileSystemResult = project.getCachedDirectoryStructureHost().addOrDeleteFileOrDirectory(fileOrDirectory, fileOrDirectoryPath);
1006+
1007+
// don't trigger callback on open, existing files
1008+
if (project.fileIsOpen(fileOrDirectoryPath) && fileSystemResult && fileSystemResult.fileExists) {
1009+
return;
1010+
}
1011+
10061012
if (isPathIgnored(fileOrDirectoryPath)) return;
10071013
const configFilename = project.getConfigFilePath();
10081014

src/testRunner/unittests/tsserver/projects.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,26 @@ var x = 10;`
13411341
}
13421342
});
13431343

1344+
it("no project structure update on directory watch invoke on open file save", () => {
1345+
const projectRootPath = "/users/username/projects/project";
1346+
const fileA: File = {
1347+
path: `${projectRootPath}/a.ts`,
1348+
content: "export const a = 10;"
1349+
};
1350+
const config: File = {
1351+
path: `${projectRootPath}/tsconfig.json`,
1352+
content: "{}"
1353+
};
1354+
const files = [fileA, config];
1355+
const host = createServerHost(files);
1356+
const service = createProjectService(host);
1357+
service.openClientFile(fileA.path);
1358+
checkNumberOfProjects(service, { configuredProjects: 1 });
1359+
1360+
host.invokeWatchedDirectoriesRecursiveCallback(projectRootPath, "a.ts");
1361+
host.checkTimeoutQueueLength(0);
1362+
});
1363+
13441364
it("handles delayed directory watch invoke on file creation", () => {
13451365
const projectRootPath = "/users/username/projects/project";
13461366
const fileB: File = {

src/testRunner/unittests/tsserver/resolutionCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ export const x = 10;`
977977
});
978978

979979
describe("avoid unnecessary invalidation", () => {
980-
it("failed lookup invalidation", () => {
980+
it("unnecessary lookup invalidation on save", () => {
981981
const expectedNonRelativeDirectories = [`${projectLocation}/node_modules`, `${projectLocation}/src`];
982982
const module1Name = "module1";
983983
const module2Name = "module2";
@@ -999,6 +999,7 @@ export const x = 10;`
999999
verifyTrace(resolutionTrace, expectedTrace);
10001000
verifyWatchesWithConfigFile(host, files, file1, expectedNonRelativeDirectories);
10011001

1002+
// invoke callback to simulate saving
10021003
host.invokeWatchedDirectoriesRecursiveCallback(projectLocation + "/src", "file1.ts");
10031004
host.checkTimeoutQueueLengthAndRun(0);
10041005
});

0 commit comments

Comments
 (0)