Skip to content

Commit 01fc188

Browse files
committed
fix(@angular-devkit/build-angular): limit concurrent output file writes with application builder
When using localization with the application builder for a large amount of locales, the number of files that may need to be written to disk can become large. This may be problematic on certain operating systems depending on the open file process limits. To avoid approaching the limit, the number of concurrent writes is now limited to 64.
1 parent c12f98f commit 01fc188

File tree

1 file changed

+27
-13
lines changed
  • packages/angular_devkit/build_angular/src/tools/esbuild

1 file changed

+27
-13
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,39 @@ export function getFeatureSupport(target: string[]): BuildOptions['supported'] {
175175
return supported;
176176
}
177177

178+
const MAX_CONCURRENT_WRITES = 64;
179+
178180
export async function writeResultFiles(
179181
outputFiles: BuildOutputFile[],
180182
assetFiles: { source: string; destination: string }[] | undefined,
181183
outputPath: string,
182184
) {
183185
const directoryExists = new Set<string>();
184-
await Promise.all(
185-
outputFiles.map(async (file) => {
186-
const fullOutputPath = file.fullOutputPath;
187-
// Ensure output subdirectories exist
188-
const basePath = path.dirname(fullOutputPath);
189-
if (basePath && !directoryExists.has(basePath)) {
190-
await fs.mkdir(path.join(outputPath, basePath), { recursive: true });
191-
directoryExists.add(basePath);
192-
}
193-
// Write file contents
194-
await fs.writeFile(path.join(outputPath, fullOutputPath), file.contents);
195-
}),
196-
);
186+
187+
// Writes the output file to disk and ensures the containing directories are present
188+
const writeOutputFile = async (file: BuildOutputFile) => {
189+
const fullOutputPath = file.fullOutputPath;
190+
// Ensure output subdirectories exist
191+
const basePath = path.dirname(fullOutputPath);
192+
if (basePath && !directoryExists.has(basePath)) {
193+
await fs.mkdir(path.join(outputPath, basePath), { recursive: true });
194+
directoryExists.add(basePath);
195+
}
196+
// Write file contents
197+
await fs.writeFile(path.join(outputPath, fullOutputPath), file.contents);
198+
};
199+
200+
// Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
201+
for (let fileIndex = 0; fileIndex < outputFiles.length; ) {
202+
const groupMax = Math.min(fileIndex + MAX_CONCURRENT_WRITES, outputFiles.length);
203+
204+
const actions = [];
205+
while (fileIndex < groupMax) {
206+
actions.push(writeOutputFile(outputFiles[fileIndex++]));
207+
}
208+
209+
await Promise.all(actions);
210+
}
197211

198212
if (assetFiles?.length) {
199213
await Promise.all(

0 commit comments

Comments
 (0)