@@ -491,21 +491,40 @@ namespace ts.server {
491
491
case 1 :
492
492
return this . containingProjects [ 0 ] ;
493
493
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
497
500
let firstExternalProject ;
498
501
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 ] ;
500
506
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
+ }
502
517
if ( ! firstConfiguredProject ) firstConfiguredProject = project ;
503
518
}
504
519
else if ( project . projectKind === ProjectKind . External && ! firstExternalProject ) {
505
520
firstExternalProject = project ;
506
521
}
507
522
}
508
- return firstConfiguredProject || firstExternalProject || this . containingProjects [ 0 ] ;
523
+ return defaultConfiguredProject ||
524
+ firstNonSourceOfProjectReferenceRedirect ||
525
+ firstConfiguredProject ||
526
+ firstExternalProject ||
527
+ this . containingProjects [ 0 ] ;
509
528
}
510
529
}
511
530
0 commit comments