Skip to content

Commit 11ac006

Browse files
authored
Fix issue that randomly closes configured projects (#15080)
* Fix issue that randomly closes configured projects * Correct the default project selection
1 parent 1798e8f commit 11ac006

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/server/editorServices.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,18 +666,23 @@ namespace ts.server {
666666
// when creation inferred project for some file has added other open files into this project (i.e. as referenced files)
667667
// we definitely don't want to delete the project that was just created
668668
for (const f of this.openFiles) {
669-
if (f.containingProjects.length === 0) {
669+
if (f.containingProjects.length === 0 || !inferredProject.containsScriptInfo(f)) {
670670
// this is orphaned file that we have not processed yet - skip it
671671
continue;
672672
}
673-
const defaultProject = f.getDefaultProject();
674-
if (isRootFileInInferredProject(info) && defaultProject !== inferredProject && inferredProject.containsScriptInfo(f)) {
675-
// open file used to be root in inferred project,
676-
// this inferred project is different from the one we've just created for current file
677-
// and new inferred project references this open file.
678-
// We should delete old inferred project and attach open file to the new one
679-
this.removeProject(defaultProject);
680-
f.attachToProject(inferredProject);
673+
674+
for (const fContainingProject of f.containingProjects) {
675+
if (fContainingProject.projectKind === ProjectKind.Inferred &&
676+
fContainingProject.isRoot(f) &&
677+
fContainingProject !== inferredProject) {
678+
679+
// open file used to be root in inferred project,
680+
// this inferred project is different from the one we've just created for current file
681+
// and new inferred project references this open file.
682+
// We should delete old inferred project and attach open file to the new one
683+
this.removeProject(fContainingProject);
684+
f.attachToProject(inferredProject);
685+
}
681686
}
682687
}
683688
}

src/server/scriptInfo.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,26 @@ namespace ts.server {
261261
}
262262

263263
getDefaultProject() {
264-
if (this.containingProjects.length === 0) {
265-
return Errors.ThrowNoProject();
264+
switch (this.containingProjects.length) {
265+
case 0:
266+
return Errors.ThrowNoProject();
267+
case 1:
268+
return this.containingProjects[0];
269+
default:
270+
// if this file belongs to multiple projects, the first configured project should be
271+
// the default project; if no configured projects, the first external project should
272+
// be the default project; otherwise the first inferred project should be the default.
273+
let firstExternalProject;
274+
for (const project of this.containingProjects) {
275+
if (project.projectKind === ProjectKind.Configured) {
276+
return project;
277+
}
278+
else if (project.projectKind === ProjectKind.External && !firstExternalProject) {
279+
firstExternalProject = project;
280+
}
281+
}
282+
return firstExternalProject || this.containingProjects[0];
266283
}
267-
return this.containingProjects[0];
268284
}
269285

270286
registerFileUpdate(): void {

src/server/session.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,25 @@ namespace ts.server {
758758
return projects;
759759
}
760760

761+
private getDefaultProject(args: protocol.FileRequestArgs) {
762+
if (args.projectFileName) {
763+
const project = this.getProject(args.projectFileName);
764+
if (project) {
765+
return project;
766+
}
767+
}
768+
const info = this.projectService.getScriptInfo(args.file);
769+
return info.getDefaultProject();
770+
}
771+
761772
private getRenameLocations(args: protocol.RenameRequestArgs, simplifiedResult: boolean): protocol.RenameResponseBody | RenameLocation[] {
762773
const file = toNormalizedPath(args.file);
763774
const info = this.projectService.getScriptInfoForNormalizedPath(file);
764775
const position = this.getPosition(args, info);
765776
const projects = this.getProjects(args);
766777
if (simplifiedResult) {
767778

768-
const defaultProject = projects[0];
779+
const defaultProject = this.getDefaultProject(args);
769780
// The rename info should be the same for every project
770781
const renameInfo = defaultProject.getLanguageService().getRenameInfo(file, position);
771782
if (!renameInfo) {
@@ -864,7 +875,7 @@ namespace ts.server {
864875
const file = toNormalizedPath(args.file);
865876
const projects = this.getProjects(args);
866877

867-
const defaultProject = projects[0];
878+
const defaultProject = this.getDefaultProject(args);
868879
const scriptInfo = defaultProject.getScriptInfoForNormalizedPath(file);
869880
const position = this.getPosition(args, scriptInfo);
870881
if (simplifiedResult) {

0 commit comments

Comments
 (0)