Skip to content

Commit d775404

Browse files
committed
Use correct default project for file if that project is opened at later time
Fixes #31926
1 parent b0ea83b commit d775404

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/server/editorServices.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,14 @@ namespace ts.server {
15331533
return undefined;
15341534
}
15351535

1536+
/*@internal*/
1537+
findDefaultConfiguredProject(info: ScriptInfo) {
1538+
if (!info.isScriptOpen()) return undefined;
1539+
const configFileName = this.getConfigFileNameForFile(info);
1540+
return configFileName &&
1541+
this.findConfiguredProjectByProjectName(configFileName);
1542+
}
1543+
15361544
/**
15371545
* This function tries to search for a tsconfig.json for the given file.
15381546
* This is different from the method the compiler uses because

src/server/scriptInfo.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,21 +491,40 @@ namespace ts.server {
491491
case 1:
492492
return this.containingProjects[0];
493493
default:
494-
// if this file belongs to multiple projects, the first configured project should be
495-
// the default project; if no configured projects, the first external project should
496-
// be the default project; otherwise the first inferred project should be the default.
494+
// If this file belongs to multiple projects, below is the order in which default project is used
495+
// - for open script info, its default configured project during opening is default if info is part of it
496+
// - first configured project of which script info is not a source of project reference redirect
497+
// - first configured project
498+
// - first external project
499+
// - first inferred project
497500
let firstExternalProject;
498501
let firstConfiguredProject;
499-
for (const project of this.containingProjects) {
502+
let firstNonSourceOfProjectReferenceRedirect;
503+
let defaultConfiguredProject: ConfiguredProject | false | undefined;
504+
for (let index = 0; index < this.containingProjects.length; index++) {
505+
const project = this.containingProjects[index];
500506
if (project.projectKind === ProjectKind.Configured) {
501-
if (!project.isSourceOfProjectReferenceRedirect(this.fileName)) return project;
507+
if (!project.isSourceOfProjectReferenceRedirect(this.fileName)) {
508+
// If we havent found default configuredProject and
509+
// its not the last one, find it and use that one if there
510+
if (defaultConfiguredProject === undefined &&
511+
index !== this.containingProjects.length - 1) {
512+
defaultConfiguredProject = project.projectService.findDefaultConfiguredProject(this) || false;
513+
}
514+
if (defaultConfiguredProject === project) return project;
515+
if (!firstNonSourceOfProjectReferenceRedirect) firstNonSourceOfProjectReferenceRedirect = project;
516+
}
502517
if (!firstConfiguredProject) firstConfiguredProject = project;
503518
}
504519
else if (project.projectKind === ProjectKind.External && !firstExternalProject) {
505520
firstExternalProject = project;
506521
}
507522
}
508-
return firstConfiguredProject || firstExternalProject || this.containingProjects[0];
523+
return defaultConfiguredProject ||
524+
firstNonSourceOfProjectReferenceRedirect ||
525+
firstConfiguredProject ||
526+
firstExternalProject ||
527+
this.containingProjects[0];
509528
}
510529
}
511530

0 commit comments

Comments
 (0)