Skip to content

Commit c22b3bf

Browse files
committed
refactor(@angular/build): collect and log build errors in library builder
Instead of throwing errors inside `buildAction` and `compileLibrary` during progress spinner execution, collect formatted diagnostic errors, warnings, and informational logs into `BuildActionResult` and log them via `context.logger` after `withProgress` completes, matching the behavior of the `application` builder.
1 parent 4951994 commit c22b3bf

6 files changed

Lines changed: 317 additions & 164 deletions

File tree

‎packages/angular/build/src/builders/library/builder.ts‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { getSupportedBrowsers } from '../../utils/supported-browsers';
2424
import { assertCompatibleAngularVersion } from '../../utils/version';
2525
import type { BuildWatcher } from '../../utils/watcher';
2626
import { normalizeLibraryOptions } from './options';
27+
import type { BuildActionResult } from './pipeline/build-action';
2728
import { updateWatchedEntryPoints } from './pipeline/entry-points';
2829
import type { createComponentStylesheetBundlerForLibrary } from './pipeline/stylesheet-bundler';
2930
import { loadPackageJson } from './pipeline/utils';
@@ -185,6 +186,17 @@ export async function* executeLibraryBuilder(
185186
}
186187
}
187188

189+
/**
190+
* Executes a single build iteration with progress reporting, logs any resulting
191+
* messages, and registers newly discovered files with the watcher.
192+
*
193+
* @param message Progress spinner message to display while building.
194+
* @param actionContext The build action context and state.
195+
* @param withProgress Progress wrapper function (`withSpinner` or `withNoProgress`).
196+
* @param watcher Optional file watcher for watch mode.
197+
* @param buildAction The library build action function to execute.
198+
* @returns A promise resolving to the Architect `BuilderOutput`.
199+
*/
188200
async function executeBuild(
189201
message: string,
190202
actionContext: import('./pipeline/build-action').BuildActionContext,
@@ -196,13 +208,16 @@ async function executeBuild(
196208
const { context, watchedCompilationFiles, isWatchMode, options } = actionContext;
197209

198210
try {
199-
await withProgress(message, () => buildAction(actionContext));
200-
logBuildResult(context.logger, startTime, true, options.outputPath);
201-
if (isWatchMode) {
211+
const result = await withProgress(message, () => buildAction(actionContext));
212+
logMessages(context.logger, result);
213+
214+
const success = result.errors.length === 0;
215+
logBuildResult(context.logger, startTime, success, options.outputPath);
216+
if (success && isWatchMode) {
202217
logCumulativeDurations();
203218
}
204219

205-
return { success: true };
220+
return { success };
206221
} catch (error) {
207222
assertIsError(error);
208223
logBuildResult(context.logger, startTime, false);
@@ -300,6 +315,7 @@ async function* runWatchLoop(
300315
assertIsError(error);
301316
await buildState.singleProgramCache?.compilationInstance.update?.(changedFiles);
302317
buildState.hasEmittedManifests = false;
318+
303319
yield {
304320
success: false,
305321
error: `Failed to reload 'package.json': ${error.message}`,
@@ -343,8 +359,36 @@ async function* runWatchLoop(
343359
}
344360
}
345361

362+
/**
363+
* Logs informational, warning, and error messages produced during a build iteration.
364+
*
365+
* @param logger The Architect logger API.
366+
* @param result The build action result containing logs, warnings, and errors.
367+
*/
368+
function logMessages(
369+
logger: logging.LoggerApi,
370+
{ logs, warnings, errors }: BuildActionResult,
371+
): void {
372+
if (logs.length) {
373+
logger.info(logs.join('\n'));
374+
}
375+
376+
if (warnings.length) {
377+
logger.warn(warnings.join('\n'));
378+
}
379+
380+
if (errors.length) {
381+
logger.error(errors.join('\n'));
382+
}
383+
}
384+
346385
/**
347386
* Logs the build completion time and status.
387+
*
388+
* @param logger The Architect logger API.
389+
* @param startTime High-resolution timestamp recorded when the build iteration started.
390+
* @param success Whether the build iteration succeeded without errors.
391+
* @param outputPath Optional output directory path to log on successful builds.
348392
*/
349393
function logBuildResult(
350394
logger: logging.LoggerApi,

0 commit comments

Comments
 (0)