@@ -42,6 +42,7 @@ export class TsgoGenerator implements Generator {
4242 private pendingFiles = new Map < string , string > ( )
4343 private openedFiles = new Set < string > ( )
4444 private loadedSourceFiles = new Set < string > ( )
45+ private rootFileProjects = new Map < string , string > ( )
4546 private emitQueue : Promise < void > = Promise . resolve ( )
4647
4748 constructor ( options : TsgoGeneratorOptions ) {
@@ -69,6 +70,34 @@ export class TsgoGenerator implements Generator {
6970 this . snapshot = await this . api . createSnapshot ( {
7071 openProjects : [ tsconfig ] ,
7172 } )
73+ this . indexProjectRoots ( )
74+
75+ // Referenced projects are not opened automatically. Their sources are
76+ // external-library files in this program and must be emitted by their owner.
77+ const seen = new Set ( [ this . api . getCanonicalFileName ( tsconfig ) ] )
78+ let configs = [ tsconfig ]
79+ while ( configs . length ) {
80+ configs = configs
81+ . flatMap (
82+ ( config ) =>
83+ this . snapshot ! . getConfiguredProject ( config ) ?. parsedCommandLine
84+ . projectReferences ?? [ ] ,
85+ )
86+ . map ( ( { path : ref } ) =>
87+ ref . endsWith ( '.json' ) ? ref : path . join ( ref , 'tsconfig.json' ) ,
88+ )
89+ . filter ( ( config ) => {
90+ const key = this . api ! . getCanonicalFileName ( config )
91+ if ( seen . has ( key ) ) return false
92+ seen . add ( key )
93+ return true
94+ } )
95+ if ( configs . length ) {
96+ await this . replaceSnapshot (
97+ await this . snapshot . update ( { openProjects : configs } ) ,
98+ )
99+ }
100+ }
72101
73102 // eslint-disable-next-line unicorn/prefer-early-return
74103 if ( this . options . languageContext . contentMapperExtensions . length ) {
@@ -129,6 +158,29 @@ export class TsgoGenerator implements Generator {
129158 return path . resolve ( this . options . languageContext . toTsFilename ( fileName ) )
130159 }
131160
161+ private async replaceSnapshot ( nextSnapshot : Snapshot ) : Promise < void > {
162+ const oldSnapshot = this . snapshot !
163+ this . snapshot = nextSnapshot
164+ this . projectFiles = undefined
165+ if ( oldSnapshot === nextSnapshot ) return
166+ this . indexProjectRoots ( )
167+ await oldSnapshot . dispose ( )
168+ }
169+
170+ private indexProjectRoots ( ) : void {
171+ this . rootFileProjects . clear ( )
172+ const requested = this . snapshot ! . getConfiguredProject ( this . options . tsconfig )
173+ for ( const project of this . snapshot ! . getProjects ( ) ) {
174+ if ( ! project . configFileName ) continue
175+ for ( const fileName of project . parsedCommandLine . fileNames ) {
176+ const key = this . api ! . getCanonicalFileName ( path . resolve ( fileName ) )
177+ if ( ! this . rootFileProjects . has ( key ) || project . id === requested ?. id ) {
178+ this . rootFileProjects . set ( key , project . configFileName )
179+ }
180+ }
181+ }
182+ }
183+
132184 private async loadSourceFiles ( ) : Promise < void > {
133185 const { loadSource } = this . options
134186 if ( ! loadSource ) return
@@ -194,12 +246,7 @@ export class TsgoGenerator implements Generator {
194246 for ( const fileName of openFiles ) {
195247 this . openedFiles . add ( fileName )
196248 }
197- const oldSnapshot = this . snapshot
198- this . snapshot = nextSnapshot
199- this . projectFiles = undefined
200- if ( oldSnapshot !== nextSnapshot ) {
201- await oldSnapshot . dispose ( )
202- }
249+ await this . replaceSnapshot ( nextSnapshot )
203250 }
204251
205252 private async emitFromSnapshot ( fileName : string ) : Promise < GeneratorResult > {
@@ -235,9 +282,11 @@ export class TsgoGenerator implements Generator {
235282 } )
236283 }
237284 const fileNames = await this . projectFiles
238- const project =
239- configuredProject &&
240- fileNames ?. has ( this . api . getCanonicalFileName ( tsFileName ) )
285+ const canonicalFileName = this . api . getCanonicalFileName ( tsFileName )
286+ const ownerConfig = this . rootFileProjects . get ( canonicalFileName )
287+ const project = ownerConfig
288+ ? this . snapshot . getConfiguredProject ( ownerConfig )
289+ : configuredProject && fileNames ?. has ( canonicalFileName )
241290 ? configuredProject
242291 : await this . snapshot
243292 . getDefaultProjectForFile ( tsFileName )
0 commit comments