mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 10:41:56 -05:00
Use correct default project for file if that project is opened at later time
Fixes #31926
This commit is contained in:
@@ -1533,6 +1533,14 @@ namespace ts.server {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/*@internal*/
|
||||
findDefaultConfiguredProject(info: ScriptInfo) {
|
||||
if (!info.isScriptOpen()) return undefined;
|
||||
const configFileName = this.getConfigFileNameForFile(info);
|
||||
return configFileName &&
|
||||
this.findConfiguredProjectByProjectName(configFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tries to search for a tsconfig.json for the given file.
|
||||
* This is different from the method the compiler uses because
|
||||
|
||||
@@ -491,21 +491,40 @@ namespace ts.server {
|
||||
case 1:
|
||||
return this.containingProjects[0];
|
||||
default:
|
||||
// if this file belongs to multiple projects, the first configured project should be
|
||||
// the default project; if no configured projects, the first external project should
|
||||
// be the default project; otherwise the first inferred project should be the default.
|
||||
// If this file belongs to multiple projects, below is the order in which default project is used
|
||||
// - for open script info, its default configured project during opening is default if info is part of it
|
||||
// - first configured project of which script info is not a source of project reference redirect
|
||||
// - first configured project
|
||||
// - first external project
|
||||
// - first inferred project
|
||||
let firstExternalProject;
|
||||
let firstConfiguredProject;
|
||||
for (const project of this.containingProjects) {
|
||||
let firstNonSourceOfProjectReferenceRedirect;
|
||||
let defaultConfiguredProject: ConfiguredProject | false | undefined;
|
||||
for (let index = 0; index < this.containingProjects.length; index++) {
|
||||
const project = this.containingProjects[index];
|
||||
if (project.projectKind === ProjectKind.Configured) {
|
||||
if (!project.isSourceOfProjectReferenceRedirect(this.fileName)) return project;
|
||||
if (!project.isSourceOfProjectReferenceRedirect(this.fileName)) {
|
||||
// If we havent found default configuredProject and
|
||||
// its not the last one, find it and use that one if there
|
||||
if (defaultConfiguredProject === undefined &&
|
||||
index !== this.containingProjects.length - 1) {
|
||||
defaultConfiguredProject = project.projectService.findDefaultConfiguredProject(this) || false;
|
||||
}
|
||||
if (defaultConfiguredProject === project) return project;
|
||||
if (!firstNonSourceOfProjectReferenceRedirect) firstNonSourceOfProjectReferenceRedirect = project;
|
||||
}
|
||||
if (!firstConfiguredProject) firstConfiguredProject = project;
|
||||
}
|
||||
else if (project.projectKind === ProjectKind.External && !firstExternalProject) {
|
||||
firstExternalProject = project;
|
||||
}
|
||||
}
|
||||
return firstConfiguredProject || firstExternalProject || this.containingProjects[0];
|
||||
return defaultConfiguredProject ||
|
||||
firstNonSourceOfProjectReferenceRedirect ||
|
||||
firstConfiguredProject ||
|
||||
firstExternalProject ||
|
||||
this.containingProjects[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -846,7 +846,6 @@ namespace ts.projectSystem {
|
||||
});
|
||||
|
||||
it("when multiple projects are open, detects correct default project", () => {
|
||||
//const projectLocation: `/user/username/projects/myproject`;
|
||||
const barConfig: File = {
|
||||
path: `${projectRoot}/bar/tsconfig.json`,
|
||||
content: JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user