fix(dev-infra): formatting errors not reported with failure message (#42202)

Currently if formatting for a file fails due a formatter error,
the `ng-dev` tool reports that formatting failed, but no actual
error (or involved file) is printed out. This commit prints out
the failed files with their error message.

PR Close #42202
This commit is contained in:
Paul Gschwendtner 2021-05-20 20:47:38 +02:00 committed by Zach Arend
parent 01f6e429e3
commit 8c158babdc
3 changed files with 29 additions and 13 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {error, info, promptConfirm} from '../utils/console';
import {error, info, promptConfirm, red} from '../utils/console';
import {runFormatterInParallel} from './run-commands-parallel';
@ -24,7 +24,11 @@ export async function formatFiles(files: string[]) {
// The process should exit as a failure if any of the files failed to format.
if (failures.length !== 0) {
error(`Formatting failed, see errors above for more information.`);
error(red(`The following files could not be formatted:`));
failures.forEach(({filePath, message}) => {
info(`${filePath}: ${message}`);
});
error(red(`Formatting failed, see errors above for more information.`));
process.exit(1);
}
info(`√ Formatting complete.`);
@ -46,8 +50,8 @@ export async function checkFiles(files: string[]) {
if (failures.length) {
// Provide output expressing which files are failing formatting.
info.group('\nThe following files are out of format:');
for (const file of failures) {
info(` - ${file}`);
for (const {filePath} of failures) {
info(` ${filePath}`);
}
info.groupEnd();
info();
@ -60,7 +64,7 @@ export async function checkFiles(files: string[]) {
if (runFormatter) {
// Format the failing files as requested.
await formatFiles(failures);
await formatFiles(failures.map(f => f.filePath));
process.exit(0);
} else {
// Inform user how to format files in the future.

View File

@ -17,6 +17,14 @@ import {Formatter, FormatterAction, getActiveFormatters} from './formatters/inde
const AVAILABLE_THREADS = Math.max(cpus().length - 1, 1);
/** Interface describing a failure occurred during formatting of a file. */
export interface FormatFailure {
/** Path to the file that failed. */
filePath: string;
/** Error message reported by the formatter. */
message: string;
}
/**
* Run the provided commands in parallel for each provided file.
*
@ -30,9 +38,9 @@ const AVAILABLE_THREADS = Math.max(cpus().length - 1, 1);
* The promise resolves with a list of failures, or `false` if no formatters have matched.
*/
export function runFormatterInParallel(allFiles: string[], action: FormatterAction) {
return new Promise<false|string[]>((resolve) => {
return new Promise<false|FormatFailure[]>((resolve) => {
const formatters = getActiveFormatters();
const failures: string[] = [];
const failures: FormatFailure[] = [];
const pendingCommands: {formatter: Formatter, file: string}[] = [];
for (const formatter of formatters) {
@ -85,7 +93,7 @@ export function runFormatterInParallel(allFiles: string[], action: FormatterActi
// Run the provided callback function.
const failed = formatter.callbackFor(action)(file, code, stdout, stderr);
if (failed) {
failures.push(file);
failures.push({filePath: file, message: stderr});
}
// Note in the progress bar another file being completed.
progressBar.increment(1);

View File

@ -2591,7 +2591,7 @@ function runFormatterInParallel(allFiles, action) {
// Run the provided callback function.
const failed = formatter.callbackFor(action)(file, code, stdout, stderr);
if (failed) {
failures.push(file);
failures.push({ filePath: file, message: stderr });
}
// Note in the progress bar another file being completed.
progressBar.increment(1);
@ -2639,7 +2639,11 @@ function formatFiles(files) {
}
// The process should exit as a failure if any of the files failed to format.
if (failures.length !== 0) {
error(`Formatting failed, see errors above for more information.`);
error(red(`The following files could not be formatted:`));
failures.forEach(({ filePath, message }) => {
info(`${filePath}: ${message}`);
});
error(red(`Formatting failed, see errors above for more information.`));
process.exit(1);
}
info(`√ Formatting complete.`);
@ -2660,8 +2664,8 @@ function checkFiles(files) {
if (failures.length) {
// Provide output expressing which files are failing formatting.
info.group('\nThe following files are out of format:');
for (const file of failures) {
info(` - ${file}`);
for (const { filePath } of failures) {
info(` ${filePath}`);
}
info.groupEnd();
info();
@ -2672,7 +2676,7 @@ function checkFiles(files) {
}
if (runFormatter) {
// Format the failing files as requested.
yield formatFiles(failures);
yield formatFiles(failures.map(f => f.filePath));
process.exit(0);
}
else {