@@ -467,27 +467,28 @@ namespace ts.server {
467467 }
468468
469469 setCompilerOptionsForInferredProjects ( projectCompilerOptions : protocol . ExternalProjectCompilerOptions , projectRootPath ?: string ) : void {
470- // ignore this settings if we are not creating inferred projects per project root.
471- if ( projectRootPath && ! this . useInferredProjectPerProjectRoot ) return ;
470+ Debug . assert ( projectRootPath === undefined || this . useInferredProjectPerProjectRoot , "Setting compiler options per project root path is only supported when useInferredProjectPerProjectRoot is enabled" ) ;
472471
473- const compilerOptionsForInferredProjects = convertCompilerOptions ( projectCompilerOptions ) ;
472+ const compilerOptions = convertCompilerOptions ( projectCompilerOptions ) ;
474473
475474 // always set 'allowNonTsExtensions' for inferred projects since user cannot configure it from the outside
476475 // previously we did not expose a way for user to change these settings and this option was enabled by default
477- compilerOptionsForInferredProjects . allowNonTsExtensions = true ;
476+ compilerOptions . allowNonTsExtensions = true ;
478477
479478 if ( projectRootPath ) {
480- this . compilerOptionsForInferredProjectsPerProjectRoot . set ( projectRootPath , compilerOptionsForInferredProjects ) ;
479+ this . compilerOptionsForInferredProjectsPerProjectRoot . set ( projectRootPath , compilerOptions ) ;
481480 }
482481 else {
483- this . compilerOptionsForInferredProjects = compilerOptionsForInferredProjects ;
482+ this . compilerOptionsForInferredProjects = compilerOptions ;
484483 }
485484
486485 const updatedProjects : Project [ ] = [ ] ;
487486 for ( const project of this . inferredProjects ) {
488- if ( project . projectRootPath === projectRootPath || ( project . projectRootPath && ! this . compilerOptionsForInferredProjectsPerProjectRoot . has ( project . projectRootPath ) ) ) {
489- project . setCompilerOptions ( compilerOptionsForInferredProjects ) ;
490- project . compileOnSaveEnabled = compilerOptionsForInferredProjects . compileOnSave ;
487+ if ( projectRootPath ?
488+ project . projectRootPath === projectRootPath :
489+ ! project . projectRootPath || ! this . compilerOptionsForInferredProjectsPerProjectRoot . has ( project . projectRootPath ) ) {
490+ project . setCompilerOptions ( compilerOptions ) ;
491+ project . compileOnSaveEnabled = compilerOptions . compileOnSave ;
491492 updatedProjects . push ( project ) ;
492493 }
493494 }
@@ -1319,7 +1320,8 @@ namespace ts.server {
13191320 return this . createInferredProject ( /*isSingleInferredProject*/ false , projectRootPath ) ;
13201321 }
13211322
1322- // we don't have an explicit root path, so we should try to find an inferred project that best matches the file.
1323+ // we don't have an explicit root path, so we should try to find an inferred project
1324+ // that more closely contains the file.
13231325 let bestMatch : InferredProject ;
13241326 for ( const project of this . inferredProjects ) {
13251327 // ignore single inferred projects (handled elsewhere)
@@ -1340,6 +1342,14 @@ namespace ts.server {
13401342 return undefined ;
13411343 }
13421344
1345+ // If `useInferredProjectPerProjectRoot` is not enabled, then there will only be one
1346+ // inferred project for all files. If `useInferredProjectPerProjectRoot` is enabled
1347+ // then we want to put all files that are not opened with a `projectRootPath` into
1348+ // the same inferred project.
1349+ //
1350+ // To avoid the cost of searching through the array and to optimize for the case where
1351+ // `useInferredProjectPerProjectRoot` is not enabled, we will always put the inferred
1352+ // project for non-rooted files at the front of the array.
13431353 if ( this . inferredProjects . length > 0 && this . inferredProjects [ 0 ] . projectRootPath === undefined ) {
13441354 return this . inferredProjects [ 0 ] ;
13451355 }
0 commit comments