From ea17fa31eadbb5a5beb4dede0e703beea690e689 Mon Sep 17 00:00:00 2001 From: Kevin Bot Date: Thu, 19 Nov 2026 21:43:23 +0900 Subject: [PATCH] fix: report tsgo CLI output via errors and avoid false CJS warning for JSON (#372) --- src/generator/tsc.ts | 8 ++++++++ src/generator/tsgo-cli.ts | 18 +++++++++++++----- tests/tsc.test.ts | 5 +++++ tests/tsgo.test.ts | 4 +++- vitest.config.ts | 2 ++ 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/generator/tsc.ts b/src/generator/tsc.ts index 4587185..33ef40f 100644 --- a/src/generator/tsc.ts +++ b/src/generator/tsc.ts @@ -171,9 +171,17 @@ function createTscWorker(vue: false | VueLanguageOptions): TscWorker { } } +const RE_JSON_EXPORT_ASSIGNMENT = /^export = _exports;$/m + function patchJsonDts(code: string): string { // If the declaration contains invalid JSON keys, add named exports. if (code.includes('declare const _exports')) { + // tsc declares such JSON modules with `export = _exports`. Use an ES + // default export instead, so it isn't reported as CommonJS dts input. + code = code.replace( + RE_JSON_EXPORT_ASSIGNMENT, + 'export { _exports as default };', + ) if ( code.includes('declare const _exports: {') && !code.includes('\n}[];') diff --git a/src/generator/tsgo-cli.ts b/src/generator/tsgo-cli.ts index 20d09c3..289733e 100644 --- a/src/generator/tsgo-cli.ts +++ b/src/generator/tsgo-cli.ts @@ -16,14 +16,22 @@ import type { SourceMapInput } from 'rolldown' // TypeScript 7.1+ uses the API generator. To be removed in the next minor. const debug = createDebug('rolldown-plugin-dts:tsgo') +// Output is piped rather than inherited so diagnostics reach the user through +// the thrown error, i.e. through Rolldown, instead of being written straight +// to the terminal. const spawnAsync = (...args: Parameters) => new Promise((resolve, reject) => { const child = spawn(...args) + let output = '' + child.stdout?.on('data', (chunk) => (output += chunk)) + child.stderr?.on('data', (chunk) => (output += chunk)) child.on('close', (code, signal) => { - if (code === 0) resolve() - else if (signal) - reject(new Error(`tsgo process terminated by signal ${signal}`)) - else reject(new Error(`tsgo process exited with code ${code}`)) + if (code === 0) return resolve() + const message = signal + ? `tsgo process terminated by signal ${signal}` + : `tsgo process exited with code ${code}` + output = output.trim() + reject(new Error(output ? `${message}\n${output}` : message)) }) child.on('error', (error) => reject(error)) }) @@ -162,7 +170,7 @@ async function runTsgo( ] debug('[tsgo] args %o', args) - await spawnAsync(tsgoPath, args, { stdio: 'inherit' }) + await spawnAsync(tsgoPath, args, { stdio: ['ignore', 'pipe', 'pipe'] }) return { path: tsgoDist, diff --git a/tests/tsc.test.ts b/tests/tsc.test.ts index 61f4d31..87d2264 100644 --- a/tests/tsc.test.ts +++ b/tests/tsc.test.ts @@ -303,6 +303,11 @@ describe('tsc', () => { dtsOnly: true, }), ], + { + onwarn(warning) { + expect.unreachable(warning.message) + }, + }, ) expect(snapshot).toMatchSnapshot() diff --git a/tests/tsgo.test.ts b/tests/tsgo.test.ts index 9fd2188..b44ab45 100644 --- a/tests/tsgo.test.ts +++ b/tests/tsgo.test.ts @@ -470,7 +470,9 @@ test('tsgo CLI fails on type errors with `typecheck`', async () => { const checked = new TsgoCliGenerator({ ...options, typecheck: true }) try { - await expect(checked.init()).rejects.toThrow('tsgo process exited') + await expect(checked.init()).rejects.toThrow( + /tsgo process exited.*error\.ts.*TS2322/s, + ) } finally { await checked.dispose() } diff --git a/vitest.config.ts b/vitest.config.ts index d99362f..bbfed9e 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,6 +6,8 @@ export default defineConfig({ }, test: { testTimeout: 10_000, + // Logs from passing tests are noise; failing tests still print theirs. + silent: 'passed-only', }, server: { watch: { -- knotmirror 0.0.1