Skip to content

Commit 5a3f7f9

Browse files
jasonkuhrtsxzzbot
andauthored
fix(tsgo): emit package sources through referenced projects (#332)
* fix(tsgo): emit package sources through referenced projects * test(tsgo): link project-reference fixture packages in place * refactor(tsgo): simplify project reference handling --------- Co-authored-by: Kevin Bot <[email protected]>
1 parent abe1cf0 commit 5a3f7f9

12 files changed

Lines changed: 138 additions & 10 deletions

File tree

‎src/generator/tsgo.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Item } from '@fixture/model'
2+
3+
export interface Wrapper<T> {
4+
readonly item: Item
5+
readonly value: T
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"files": ["src/index.ts"],
4+
"references": [{ "path": "../lib/tsconfig.production.json" }]
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@fixture/base",
3+
"type": "module",
4+
"exports": "./src/index.ts"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Item {
2+
readonly value: string
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"include": ["src/*.ts"]
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@fixture/model",
3+
"type": "module",
4+
"exports": "./src/index.ts"
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { Item } from '@fixture/base'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"include": ["src/*.ts"],
4+
"references": [{ "path": "../base" }]
5+
}

0 commit comments

Comments
 (0)