From 281598105708d1db80ab6d77cfd4f99806d329fe Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Tue, 17 Jan 2023 16:18:34 -0800 Subject: [PATCH] Make build error cause more clear by prepending with module name Simplify the diagnosis of build failures by showing the module name encountering the issue right before the error itself is printed. Also apply the fix to warnings. Fixes #15815 Signed-off-by: Mitchell Hentges --- .changeset/eight-hotels-sparkle.md | 5 +++++ packages/cli/src/lib/bundler/bundle.ts | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/eight-hotels-sparkle.md diff --git a/.changeset/eight-hotels-sparkle.md b/.changeset/eight-hotels-sparkle.md new file mode 100644 index 0000000000..6846630e02 --- /dev/null +++ b/.changeset/eight-hotels-sparkle.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Show module name causing error during build diff --git a/packages/cli/src/lib/bundler/bundle.ts b/packages/cli/src/lib/bundler/bundle.ts index 9b146aa115..ba553b3b76 100644 --- a/packages/cli/src/lib/bundler/bundle.ts +++ b/packages/cli/src/lib/bundler/bundle.ts @@ -32,6 +32,10 @@ import chalk from 'chalk'; const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; +function applyContextToError(error: string, moduleName: string): string { + return `Failed to compile '${moduleName}':\n ${error}`; +} + export async function buildBundle(options: BuildOptions) { const { statsJsonEnabled, schema: configSchema } = options; @@ -127,15 +131,25 @@ async function build(config: webpack.Configuration, isCi: boolean) { if (errors.length) { // Only keep the first error. Others are often indicative // of the same problem, but confuse the reader with noise. - throw new Error(`Failed to compile.\n${errors[0]}`); + const errorWithContext = applyContextToError( + errors[0], + serializedStats.errors?.[0].moduleName || '', + ); + throw new Error(errorWithContext); } if (isCi && warnings.length) { + const warningsWithContext = warnings.map((warning, i) => { + return applyContextToError( + warning, + serializedStats.warnings?.[i].moduleName || '', + ); + }); console.log( chalk.yellow( '\nTreating warnings as errors because process.env.CI = true.\n', ), ); - throw new Error(`Failed to compile.\n${warnings.join('\n\n')}`); + throw new Error(warningsWithContext.join('\n\n')); } return { stats };