From 989454f7e27f3cbf33180d8aab29d56472378126 Mon Sep 17 00:00:00 2001 From: Rico Hermans Date: Mon, 6 Feb 2023 11:20:34 +0100 Subject: [PATCH] fix(cdk-assets): asset concurrency leaves a corrupted archive (#24026) This is a re-roll of https://github.com/aws/aws-cdk/pull/23677 which was reverted in https://github.com/aws/aws-cdk/pull/23994 because the `randomUUID()` function from the original solution was not available in Node versions below 14.17 (and we advertise compatibility with Node 14.*). We didn't actually need a UUID, just any random string, so replace it with a function that generates a random string in a different way. ---------- Resolves #23290 A very simple fix for the issue where builds with `--concurrency` specified can lead to corrupt archives. Rather than use the outputFile as the basis for the temp file name we simply use a random string. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/cdk-assets/lib/private/archive.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/cdk-assets/lib/private/archive.ts b/packages/cdk-assets/lib/private/archive.ts index a3922d4d59dba..5f7be17232859 100644 --- a/packages/cdk-assets/lib/private/archive.ts +++ b/packages/cdk-assets/lib/private/archive.ts @@ -11,7 +11,7 @@ type Logger = (x: string) => void; export async function zipDirectory(directory: string, outputFile: string, logger: Logger): Promise { // We write to a temporary file and rename at the last moment. This is so that if we are // interrupted during this process, we don't leave a half-finished file in the target location. - const temporaryOutputFile = `${outputFile}._tmp`; + const temporaryOutputFile = `${outputFile}.${randomString()}._tmp`; await writeZipFile(directory, temporaryOutputFile); await moveIntoPlace(temporaryOutputFile, outputFile, logger); } @@ -96,4 +96,8 @@ async function pathExists(x: string) { } throw e; } +} + +function randomString() { + return Math.random().toString(36).replace(/[^a-z0-9]+/g, ''); } \ No newline at end of file