From 61815929d7534e91d0659b3e0098ac2ab51b98ff Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Mon, 13 Jan 2025 10:20:50 -0700 Subject: [PATCH] fix(js): improve error handling during SWC compilation (#29605) Improves our error handling for swc when using `@nx/js:swc` executor ## Current Behavior The executor fails to build if the workspace has a newer versions of `@swc/cli` e.g.(`0.6.0`) is installed since it now uses stderr instead of message to log errors. RE: https://github.com/swc-project/pkgs/pull/53 ## Expected Behavior The executor should pass in this scenario. ## Related Issue(s) Fixes #29599 --- packages/js/src/utils/swc/compile-swc.ts | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index e8c83cb48b443..fbb4a4f70439d 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -86,17 +86,25 @@ export async function compileSwc( rmSync(normalizedOptions.outputPath, { recursive: true, force: true }); } - const swcCmdLog = execSync(getSwcCmd(normalizedOptions), { - encoding: 'utf8', - cwd: normalizedOptions.swcCliOptions.swcCwd, - windowsHide: false, - }); - logger.log(swcCmdLog.replace(/\n/, '')); - const isCompileSuccess = swcCmdLog.includes('Successfully compiled'); + try { + const swcCmdLog = execSync(getSwcCmd(normalizedOptions), { + encoding: 'utf8', + cwd: normalizedOptions.swcCliOptions.swcCwd, + windowsHide: false, + stdio: 'pipe', + }); + logger.log(swcCmdLog.replace(/\n/, '')); + } catch (error) { + logger.error('SWC compilation failed'); + if (error.stderr) { + logger.error(error.stderr.toString()); + } + return { success: false }; + } if (normalizedOptions.skipTypeCheck && !normalizedOptions.isTsSolutionSetup) { await postCompilationCallback(); - return { success: isCompileSuccess }; + return { success: true }; } const { errors, warnings } = await runTypeCheck( @@ -111,7 +119,7 @@ export async function compileSwc( await postCompilationCallback(); return { - success: !hasErrors && isCompileSuccess, + success: !hasErrors, outfile: normalizedOptions.mainOutputPath, }; }