Skip to content

Commit

Permalink
fix(nextjs): Surface error codes when build is interrupted by signals…
Browse files Browse the repository at this point in the history
… SIGINT, SIGTERM etc... (#22190)

(cherry picked from commit 466debe)
  • Loading branch information
ndcunningham authored and FrozenPandaz committed Mar 8, 2024
1 parent bce494f commit 6790a2e
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/next/src/executors/build/build.impl.ts
Expand Up @@ -19,6 +19,7 @@ import { checkPublicDirectory } from './lib/check-project';
import { NextBuildBuilderOptions } from '../../utils/types';
import { ChildProcess, fork } from 'child_process';
import { createCliOptions } from '../../utils/create-cli-options';
import { signalToCode } from 'nx/src/utils/exit-codes';

let childProcess: ChildProcess;

Expand Down Expand Up @@ -51,9 +52,17 @@ export default async function buildExecutor(

try {
await runCliBuild(workspaceRoot, projectRoot, options);
} catch (error) {
logger.error(`Error occurred while trying to run the build command`);
logger.error(error);
} catch ({ error, code, signal }) {
if (code || signal) {
logger.error(
`Build process exited due to ${code ? 'code ' + code : ''} ${
code && signal ? 'and' : ''
} ${signal ? 'signal ' + signal : ''}`
);
} else {
logger.error(`Error occurred while trying to run the build command`);
logger.error(error);
}
return { success: false };
} finally {
if (childProcess) {
Expand Down Expand Up @@ -128,24 +137,30 @@ function runCliBuild(
['build', ...args],
{
cwd: pathResolve(workspaceRoot, projectRoot),
stdio: 'inherit',
stdio: ['pipe', 'inherit', 'ipc', 'inherit'],
env: process.env,
}
);

// Ensure the child process is killed when the parent exits
process.on('exit', () => childProcess.kill());
process.on('SIGTERM', () => childProcess.kill());

process.on('SIGTERM', (signal) => {
reject({ code: signalToCode(signal), signal });
});
process.on('SIGINT', (signal) => {
reject({ code: signalToCode(signal), signal });
});

childProcess.on('error', (err) => {
reject(err);
reject({ error: err });
});

childProcess.on('exit', (code) => {
childProcess.on('exit', (code, signal) => {
if (code === 0) {
resolve(code);
resolve({ code, signal });
} else {
reject(code);
reject({ code, signal });
}
});
});
Expand Down

0 comments on commit 6790a2e

Please sign in to comment.