Skip to content

Commit c76f8ba

Browse files
committed
fix(tsgo): skip cleanup in tsgo debug mode
1 parent 1924c04 commit c76f8ba

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/generate.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fork, type ChildProcess } from 'node:child_process'
22
import { existsSync } from 'node:fs'
3-
import { readFile, rm } from 'node:fs/promises'
3+
import { readFile } from 'node:fs/promises'
44
import path from 'node:path'
55
import { parse } from '@babel/parser'
66
import { createDebug } from 'obug'
@@ -24,7 +24,7 @@ import {
2424
invalidateContextFile,
2525
type TscContext,
2626
} from './tsc/context.ts'
27-
import { runTsgo } from './tsgo.ts'
27+
import { runTsgo, type TsgoContext } from './tsgo.ts'
2828
import type { OptionsResolved } from './options.ts'
2929
import type { TscOptions, TscResult } from './tsc/index.ts'
3030
import type { TscFunctions } from './tsc/worker.ts'
@@ -106,15 +106,15 @@ export function createGeneratePlugin({
106106
let rpc: BirpcReturn<TscFunctions> | undefined
107107
let tscModule: typeof import('./tsc/index.ts')
108108
let tscContext: TscContext | undefined
109-
let tsgoDist: string | undefined
109+
let tsgoContext: TsgoContext | undefined
110110
const rootDir = tsconfig ? path.dirname(tsconfig) : cwd
111111

112112
return {
113113
name: 'rolldown-plugin-dts:generate',
114114

115115
async buildStart(options) {
116116
if (tsgo) {
117-
tsgoDist = await runTsgo(rootDir, tsconfig, sourcemap, tsgo.path)
117+
tsgoContext = await runTsgo(rootDir, tsconfig, sourcemap, tsgo.path)
118118
} else if (!oxc) {
119119
// tsc
120120
if (parallel) {
@@ -237,7 +237,7 @@ export function createGeneratePlugin({
237237
if (RE_VUE.test(id))
238238
throw new Error('tsgo does not support Vue files.')
239239
const dtsPath = path.resolve(
240-
tsgoDist!,
240+
tsgoContext!.path,
241241
path.relative(path.resolve(rootDir), filename_to_dts(id)),
242242
)
243243
if (!existsSync(dtsPath)) {
@@ -359,10 +359,8 @@ export { __json_default_export as default }`
359359

360360
async buildEnd() {
361361
childProcess?.kill()
362-
if (!debug.enabled && tsgoDist) {
363-
await rm(tsgoDist, { recursive: true, force: true }).catch(() => {})
364-
}
365-
tsgoDist = undefined
362+
await tsgoContext?.dispose()
363+
tsgoContext = undefined
366364
if (newContext) {
367365
tscContext = undefined
368366
}

src/tsgo.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawn } from 'node:child_process'
2-
import { mkdtemp } from 'node:fs/promises'
2+
import { mkdtemp, rm } from 'node:fs/promises'
33
import { tmpdir } from 'node:os'
44
import path from 'node:path'
55
import { createDebug } from 'obug'
@@ -21,12 +21,17 @@ export async function getTsgoPathFromNodeModules(): Promise<string> {
2121
return getExePath()
2222
}
2323

24+
export interface TsgoContext {
25+
path: string
26+
dispose: () => Promise<void>
27+
}
28+
2429
export async function runTsgo(
2530
rootDir: string,
2631
tsconfig?: string,
2732
sourcemap?: boolean,
2833
tsgoPath?: string,
29-
): Promise<string> {
34+
): Promise<TsgoContext> {
3035
debug('[tsgo] rootDir', rootDir)
3136

3237
let tsgo: string
@@ -57,5 +62,16 @@ export async function runTsgo(
5762
debug('[tsgo] args %o', args)
5863

5964
await spawnAsync(tsgo, args, { stdio: 'inherit' })
60-
return tsgoDist
65+
66+
return {
67+
path: tsgoDist,
68+
async dispose() {
69+
if (debug.enabled) {
70+
debug('[tsgo] skip cleanup of tsgoDist', tsgoDist)
71+
} else {
72+
debug('[tsgo] disposing tsgoDist', tsgoDist)
73+
await rm(tsgoDist, { recursive: true, force: true }).catch(() => {})
74+
}
75+
},
76+
}
6177
}

0 commit comments

Comments
 (0)