@@ -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