Skip to content

Commit 4b19d12

Browse files
committed
Rearrange code such that project Root path of open file is always used in watching and removing the config file watches
1 parent ce5d5d8 commit 4b19d12

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/server/editorServices.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,6 @@ namespace ts.server {
891891
info.close(fileExists);
892892
this.stopWatchingConfigFilesForClosedScriptInfo(info);
893893

894-
this.openFiles.delete(info.path);
895894
const canonicalFileName = this.toCanonicalFileName(info.fileName);
896895
if (this.openFilesWithNonRootedDiskPath.get(canonicalFileName) === info) {
897896
this.openFilesWithNonRootedDiskPath.delete(canonicalFileName);
@@ -927,16 +926,19 @@ namespace ts.server {
927926
p.markAsDirty();
928927
}
929928
}
929+
930930
if (projectsToRemove) {
931931
for (const project of projectsToRemove) {
932932
this.removeProject(project);
933933
}
934934

935935
// collect orphaned files and assign them to inferred project just like we treat open of a file
936936
this.openFiles.forEach((projectRootPath, path) => {
937-
const f = this.getScriptInfoForPath(path as Path);
938-
if (f.isOrphan()) {
939-
this.assignOrphanScriptInfoToInferredProject(f, projectRootPath);
937+
if (info.path !== path) {
938+
const f = this.getScriptInfoForPath(path as Path);
939+
if (f.isOrphan()) {
940+
this.assignOrphanScriptInfoToInferredProject(f, projectRootPath);
941+
}
940942
}
941943
});
942944

@@ -945,6 +947,8 @@ namespace ts.server {
945947
// we wont end up creating same script infos
946948
}
947949

950+
this.openFiles.delete(info.path);
951+
948952
// If the current info is being just closed - add the watcher file to track changes
949953
// But if file was deleted, handle that part
950954
if (fileExists) {
@@ -1141,7 +1145,7 @@ namespace ts.server {
11411145
* This is called by inferred project whenever script info is added as a root
11421146
*/
11431147
/* @internal */
1144-
startWatchingConfigFilesForInferredProjectRoot(info: ScriptInfo, projectRootPath: NormalizedPath | undefined) {
1148+
startWatchingConfigFilesForInferredProjectRoot(info: ScriptInfo) {
11451149
Debug.assert(info.isScriptOpen());
11461150
this.forEachConfigFileLocation(info, (configFileName, canonicalConfigFilePath) => {
11471151
let configFileExistenceInfo = this.configFileExistenceInfoCache.get(canonicalConfigFilePath);
@@ -1163,7 +1167,7 @@ namespace ts.server {
11631167
!this.getConfiguredProjectByCanonicalConfigFilePath(canonicalConfigFilePath)) {
11641168
this.createConfigFileWatcherOfConfigFileExistence(configFileName, canonicalConfigFilePath, configFileExistenceInfo);
11651169
}
1166-
}, projectRootPath);
1170+
});
11671171
}
11681172

11691173
/**
@@ -1194,14 +1198,14 @@ namespace ts.server {
11941198
* The server must start searching from the directory containing
11951199
* the newly opened file.
11961200
*/
1197-
private forEachConfigFileLocation(info: ScriptInfo,
1198-
action: (configFileName: NormalizedPath, canonicalConfigFilePath: string) => boolean | void,
1199-
projectRootPath?: NormalizedPath) {
1200-
1201+
private forEachConfigFileLocation(info: ScriptInfo, action: (configFileName: NormalizedPath, canonicalConfigFilePath: string) => boolean | void) {
12011202
if (this.syntaxOnly) {
12021203
return undefined;
12031204
}
12041205

1206+
Debug.assert(this.openFiles.has(info.path));
1207+
const projectRootPath = this.openFiles.get(info.path);
1208+
12051209
let searchPath = asNormalizedPath(getDirectoryPath(info.fileName));
12061210

12071211
while (!projectRootPath || containsPath(projectRootPath, searchPath, this.currentDirectory, !this.host.useCaseSensitiveFileNames)) {
@@ -1236,13 +1240,12 @@ namespace ts.server {
12361240
* The server must start searching from the directory containing
12371241
* the newly opened file.
12381242
*/
1239-
private getConfigFileNameForFile(info: ScriptInfo, projectRootPath: NormalizedPath | undefined) {
1243+
private getConfigFileNameForFile(info: ScriptInfo) {
12401244
Debug.assert(info.isScriptOpen());
12411245
this.logger.info(`Search path: ${getDirectoryPath(info.fileName)}`);
12421246
const configFileName = this.forEachConfigFileLocation(info,
12431247
(configFileName, canonicalConfigFilePath) =>
12441248
this.configFileExists(configFileName, canonicalConfigFilePath, info),
1245-
projectRootPath
12461249
);
12471250
if (configFileName) {
12481251
this.logger.info(`For info: ${info.fileName} :: Config file name: ${configFileName}`);
@@ -1905,7 +1908,7 @@ namespace ts.server {
19051908
// we first detect if there is already a configured project created for it: if so,
19061909
// we re- read the tsconfig file content and update the project only if we havent already done so
19071910
// otherwise we create a new one.
1908-
const configFileName = this.getConfigFileNameForFile(info, this.openFiles.get(path));
1911+
const configFileName = this.getConfigFileNameForFile(info);
19091912
if (configFileName) {
19101913
const project = this.findConfiguredProjectByProjectName(configFileName);
19111914
if (!project) {
@@ -2008,9 +2011,10 @@ namespace ts.server {
20082011
let configFileErrors: ReadonlyArray<Diagnostic>;
20092012

20102013
const info = this.getOrCreateScriptInfoOpenedByClientForNormalizedPath(fileName, projectRootPath ? this.getNormalizedAbsolutePath(projectRootPath) : this.currentDirectory, fileContent, scriptKind, hasMixedContent);
2014+
this.openFiles.set(info.path, projectRootPath);
20112015
let project: ConfiguredProject | ExternalProject | undefined = this.findExternalProjectContainingOpenScriptInfo(info);
20122016
if (!project && !this.syntaxOnly) { // Checking syntaxOnly is an optimization
2013-
configFileName = this.getConfigFileNameForFile(info, projectRootPath);
2017+
configFileName = this.getConfigFileNameForFile(info);
20142018
if (configFileName) {
20152019
project = this.findConfiguredProjectByProjectName(configFileName);
20162020
if (!project) {
@@ -2048,7 +2052,6 @@ namespace ts.server {
20482052
}
20492053

20502054
Debug.assert(!info.isOrphan());
2051-
this.openFiles.set(info.path, projectRootPath);
20522055

20532056
// Remove the configured projects that have zero references from open files.
20542057
// This was postponed from closeOpenFile to after opening next file,

src/server/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ namespace ts.server {
11921192

11931193
addRoot(info: ScriptInfo) {
11941194
Debug.assert(info.isScriptOpen());
1195-
this.projectService.startWatchingConfigFilesForInferredProjectRoot(info, this.projectService.openFiles.get(info.path));
1195+
this.projectService.startWatchingConfigFilesForInferredProjectRoot(info);
11961196
if (!this._isJsInferredProject && info.isJavaScript()) {
11971197
this.toggleJsInferredProject(/*isJsInferredProject*/ true);
11981198
}

0 commit comments

Comments
 (0)