From 841cd6c4d78abdf8bee8fa64feb2f401db45a5d2 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Tue, 14 Apr 2020 10:25:24 -0400 Subject: [PATCH] Fix Situation Where Build Can Hang Indefinitely (#11881) --- .../terser-webpack-plugin/src/TaskRunner.js | 31 ++++++++++++++++--- .../terser-webpack-plugin/src/index.js | 9 ++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js index 17e2244a2e..7caa8cb33a 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js @@ -57,8 +57,16 @@ export default class TaskRunner { const done = () => step(index, result) if (cachePath) { writeFileP(cachePath, JSON.stringify(result), 'utf8') - .then(done) - .catch(done) + // This is important to stay as `.then(fn1, fn2)` and not + // `.then(fn1).catch(fn2)` so that we don't double-invoke `step` + // when `done` emits an error. + .then(done, done) + .catch(err => { + // Abort task on internal error (`done` failed). This can + // happen when users have a bad `package-lock.json` with an + // invalid webpack version, etc: + callback(err) + }) } } catch (error) { step(index, { error }) @@ -66,9 +74,22 @@ export default class TaskRunner { } if (this.cacheDir) { - readFileP(cachePath, 'utf8') - .then(data => step(index, JSON.parse(data))) - .catch(() => enqueue()) + // This is important to stay as `.then(fn1, fn2)` and not + // `.then(fn1).catch(fn2)` so that we don't double-invoke `step` when + // `step` emits an error. + readFileP(cachePath, 'utf8').then( + data => { + try { + step(index, JSON.parse(data)) + } catch (err) { + // Abort task on internal error (`done` failed). This can happen + // when users have a bad `package-lock.json` with an invalid + // webpack version, etc: + callback(err) + } + }, + () => enqueue() + ) } else { enqueue() } diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index 974a491286..e4416c33cc 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -4,7 +4,7 @@ import stringHash from 'next/dist/compiled/string-hash' import { SourceMapConsumer } from 'next/dist/compiled/source-map' import { SourceMapSource, RawSource } from 'webpack-sources' -import { RequestShortener } from 'webpack' +import RequestShortener from 'webpack/lib/RequestShortener' import TaskRunner from './TaskRunner' const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/ @@ -204,8 +204,11 @@ export class TerserPlugin { taskRunner.run(tasks, (tasksError, results) => { if (tasksError) { - compilation.errors.push(tasksError) - + try { + taskRunner.exit() + } finally { + callback(tasksError) + } return } -- GitLab