Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const calcBundledPackages = (options: {
export async function generateDts(data: DtsGenOptions): Promise<void> {
const {
bundle,
abortOnError,
dtsEntry,
dtsEmitPath,
tsconfigPath,
Expand Down Expand Up @@ -269,6 +270,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
tsConfigResult,
declarationDir,
dtsExtension,
abortOnError,
redirect,
rootDir,
paths,
Expand All @@ -282,7 +284,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
);

if (tsgo) {
if (!hasError) {
if (!hasError || !abortOnError) {
await bundleDtsIfNeeded();
}
} else {
Expand Down
21 changes: 14 additions & 7 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const formatHost: FormatDiagnosticsHost = {
};

export type EmitDtsOptions = {
abortOnError: boolean | undefined;
name: string;
cwd: string;
configPath: string;
Expand All @@ -42,6 +43,7 @@ export type EmitDtsOptions = {
};

async function handleDiagnosticsAndProcessFiles(
abortOnError: boolean | undefined,
diagnostics: readonly Diagnostic[],
configPath: string,
bundle: boolean,
Expand Down Expand Up @@ -81,12 +83,14 @@ async function handleDiagnosticsAndProcessFiles(
logger.error(logPrefixTsc, message);
}

const error = new Error(
`Failed to generate declaration files. ${color.dim(`(${name})`)}`,
);
// do not log the stack trace, diagnostic messages are enough
error.stack = '';
throw error;
if (abortOnError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When bundle: false with abortOnError: false, the error should still be throwed I think. abortOnError means not exit the process instead of not throwing error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we throw an error here, the outer dts generation flow will fail due to promise rejection, no matter whether bundle is true or false.

packages/plugin-dts/src/dts.ts:265

In other words, even when abortOnError is set to false, throwing here will still cause the whole external pipeline to stop and skip subsequent steps.

That’s why the error is handled without throwing in this case, allowing the process to continue as intended when abortOnError is disabled.

Copy link
Contributor

@Timeless0911 Timeless0911 Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise rejection is handled in main process.

for (const result of promisesResult) {
if (result.status === 'error') {
if (options.abortOnError) {
const error = new Error(result.errorMessage);
// do not log the stack trace, it is not helpful for users
error.stack = '';
throw error;
}
result.errorMessage && logger.error(result.errorMessage);
logger.warn(
'With `abortOnError` configuration currently disabled, type errors will not fail the build, but proper type declaration output cannot be guaranteed.',
);
}
}
});

When abortOnError is set to false, a warning will be printed and the process continue.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may optimize the behaviour when abortOnError set to false with bundle set to true. Now, if tsc/tsgo throws error, the logic in apiExtractor.ts will not be called.

When bundle set to false, throwing error is reasonable since no other operations will be performed.

In other words, abortOnError is used to prevent a process from exiting with a non-zero exit code. If you want to ignore type errors, you should set noCheck to disable type checking.

const error = new Error(
`Failed to generate declaration files. ${color.dim(`(${name})`)}`,
);
// do not log the stack trace, diagnostic messages are enough
error.stack = '';
throw error;
}
}
}

Expand All @@ -99,6 +103,7 @@ export async function emitDtsTsc(
): Promise<void> {
const start = Date.now();
const {
abortOnError,
configPath,
tsConfigResult,
declarationDir,
Expand Down Expand Up @@ -252,6 +257,7 @@ export async function emitDtsTsc(
ts.sortAndDeduplicateDiagnostics(allDiagnostics);

await handleDiagnosticsAndProcessFiles(
abortOnError,
sortAndDeduplicateDiagnostics,
configPath,
bundle,
Expand Down Expand Up @@ -322,6 +328,7 @@ export async function emitDtsTsc(
ts.sortAndDeduplicateDiagnostics(allDiagnostics);

await handleDiagnosticsAndProcessFiles(
abortOnError,
sortAndDeduplicateDiagnostics,
configPath,
bundle,
Expand Down Expand Up @@ -370,7 +377,7 @@ export async function emitDtsTsc(
footer,
);

if (errorNumber > 0) {
if (errorNumber > 0 && abortOnError) {
const error = new Error(
`Failed to generate declaration files. ${color.dim(`(${name})`)}`,
);
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-dts/src/tsgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const generateTsgoArgs = (
};

async function handleDiagnosticsAndProcessFiles(
abortOnError: boolean | undefined,
isWatch: boolean,
hasErrors: boolean,
tsConfigResult: ts.ParsedCommandLine,
Expand Down Expand Up @@ -124,7 +125,7 @@ async function handleDiagnosticsAndProcessFiles(
footer,
);

if (hasErrors && !isWatch) {
if (hasErrors && !isWatch && abortOnError) {
const error = new Error(
`Failed to generate declaration files. ${color.dim(`(${name})`)}`,
);
Expand All @@ -143,6 +144,7 @@ export async function emitDtsTsgo(
): Promise<boolean> {
const start = Date.now();
const {
abortOnError,
configPath,
tsConfigResult,
declarationDir,
Expand Down Expand Up @@ -203,6 +205,7 @@ export async function emitDtsTsgo(
}

await handleDiagnosticsAndProcessFiles(
abortOnError,
isWatch,
hasErrors,
tsConfigResult,
Expand Down