Skip to content

Commit 36f28ca

Browse files
committed
fix(@angular-devkit/build-angular): ensure output directory is present before writing stats JSON
If an error occurs during a build, it is possible that the output path directory may not be present and then cause the stats JSON file write to fail. Fixes: #20349 (cherry picked from commit ac4c109)
1 parent cc3cc8d commit 36f28ca

File tree

1 file changed

+17
-7
lines changed
  • packages/angular_devkit/build_angular/src/webpack/configs

1 file changed

+17
-7
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
buildOptimizerLoaderPath,
1111
} from '@angular-devkit/build-optimizer';
1212
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
13-
import { createWriteStream, existsSync } from 'fs';
13+
import { createWriteStream, existsSync, promises as fsPromises } from 'fs';
1414
import * as path from 'path';
1515
import { RollupOptions } from 'rollup';
1616
import { ScriptTarget } from 'typescript';
@@ -36,6 +36,7 @@ import {
3636
} from '../../utils/environment-options';
3737
import { findAllNodeModules } from '../../utils/find-up';
3838
import { Spinner } from '../../utils/spinner';
39+
import { addError } from '../../utils/webpack-diagnostics';
3940
import { isWebpackFiveOrHigher, withWebpackFourOrFive } from '../../utils/webpack-version';
4041
import {
4142
DedupeModuleResolvePlugin,
@@ -343,12 +344,21 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
343344
const data = stats.toJson('verbose');
344345
const statsOutputPath = path.join(stats.compilation.outputOptions.path, 'stats.json');
345346

346-
return new Promise<void>((resolve, reject) =>
347-
stringifyStream(data)
348-
.pipe(createWriteStream(statsOutputPath))
349-
.on('close', resolve)
350-
.on('error', reject),
351-
);
347+
try {
348+
await fsPromises.mkdir(path.dirname(statsOutputPath), { recursive: true });
349+
350+
await new Promise<void>((resolve, reject) =>
351+
stringifyStream(data)
352+
.pipe(createWriteStream(statsOutputPath))
353+
.on('close', resolve)
354+
.on('error', reject),
355+
);
356+
} catch (error) {
357+
addError(
358+
stats.compilation,
359+
`Unable to write stats file: ${error.message || 'unknown error'}`,
360+
);
361+
}
352362
});
353363
}
354364
})(),

0 commit comments

Comments
 (0)