From edc72609173a85352b90e807d6552898abefc674 Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Wed, 3 Jan 2024 21:11:41 +0000 Subject: [PATCH 1/3] fix(core): single-file bundling breaks due to left over temp dir --- .../aws-cdk-lib/core/lib/asset-staging.ts | 9 ++++-- .../aws-cdk-lib/core/test/staging.test.ts | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/core/lib/asset-staging.ts b/packages/aws-cdk-lib/core/lib/asset-staging.ts index 91ec93914a2f0..dce13f7d16ba9 100644 --- a/packages/aws-cdk-lib/core/lib/asset-staging.ts +++ b/packages/aws-cdk-lib/core/lib/asset-staging.ts @@ -343,11 +343,16 @@ export class AssetStaging extends Construct { this.stageAsset(bundledAsset.path, stagedPath, 'move'); // If bundling produced a single archive file we "touch" this file in the bundling - // directory after it has been moved to the staging directory. This way if bundling + // directory after it has been moved to the staging directory if the hash is known before bundling. This way if bundling // is skipped because the bundling directory already exists we can still determine // the correct packaging type. + // If the hash is calculated after bundling we remove the temp dir now if (bundledAsset.packaging === FileAssetPackaging.FILE) { - fs.closeSync(fs.openSync(bundledAsset.path, 'w')); + if (this.hashType === AssetHashType.OUTPUT || this.hashType === AssetHashType.BUNDLE) { + fs.removeSync(path.dirname(bundledAsset.path)); + } else { + fs.closeSync(fs.openSync(bundledAsset.path, 'w')); + } } return { diff --git a/packages/aws-cdk-lib/core/test/staging.test.ts b/packages/aws-cdk-lib/core/test/staging.test.ts index 6f3d904b4ed6e..dff63c09a76f8 100644 --- a/packages/aws-cdk-lib/core/test/staging.test.ts +++ b/packages/aws-cdk-lib/core/test/staging.test.ts @@ -1419,6 +1419,37 @@ describe('staging', () => { expect(staging.isArchive).toEqual(false); }); + test('bundling that produces a single file with SINGLE_FILE and hash type OUTPUT', () => { + // GIVEN + const app = new App({ context: { [cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT]: false } }); + const stack = new Stack(app, 'stack'); + const directory = path.join(__dirname, 'fs', 'fixtures', 'test1', 'subdir'); + + // WHEN + const staging = new AssetStaging(stack, 'Asset', { + sourcePath: directory, + assetHashType: AssetHashType.OUTPUT, + bundling: { + image: DockerImage.fromRegistry('alpine'), + command: [DockerStubCommand.SINGLE_FILE], + outputType: BundlingOutput.SINGLE_FILE, + }, + }); + + // THEN + const assembly = app.synth(); + expect(fs.readdirSync(assembly.directory)).toEqual([ + // 'bundling-temp-0e346bd27baa32f4f2d15d1d73c8972db3293080f6c2836328b7bf77747683db', this directory gets removed and does no longer exist + 'asset.95c924c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.txt', + 'cdk.out', + 'manifest.json', + 'stack.template.json', + 'tree.json', + ]); + expect(staging.packaging).toEqual(FileAssetPackaging.FILE); + expect(staging.isArchive).toEqual(false); + }); + }); describe('staging with docker cp', () => { From 868ce74f81605aff784547e01554934ccfde52d8 Mon Sep 17 00:00:00 2001 From: paulhcsun <47882901+paulhcsun@users.noreply.github.com> Date: Wed, 3 Jan 2024 17:21:02 -0800 Subject: [PATCH 2/3] minor grammar change --- packages/aws-cdk-lib/core/lib/asset-staging.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/core/lib/asset-staging.ts b/packages/aws-cdk-lib/core/lib/asset-staging.ts index dce13f7d16ba9..e17a0f2c5422e 100644 --- a/packages/aws-cdk-lib/core/lib/asset-staging.ts +++ b/packages/aws-cdk-lib/core/lib/asset-staging.ts @@ -346,7 +346,7 @@ export class AssetStaging extends Construct { // directory after it has been moved to the staging directory if the hash is known before bundling. This way if bundling // is skipped because the bundling directory already exists we can still determine // the correct packaging type. - // If the hash is calculated after bundling we remove the temp dir now + // If the hash is calculated after bundling we remove the temp dir now. if (bundledAsset.packaging === FileAssetPackaging.FILE) { if (this.hashType === AssetHashType.OUTPUT || this.hashType === AssetHashType.BUNDLE) { fs.removeSync(path.dirname(bundledAsset.path)); From aa4fc251e31d0652d6bb69c12272afb679cb9f6c Mon Sep 17 00:00:00 2001 From: paulhcsun <47882901+paulhcsun@users.noreply.github.com> Date: Wed, 3 Jan 2024 17:23:49 -0800 Subject: [PATCH 3/3] minor wording change --- packages/aws-cdk-lib/core/lib/asset-staging.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/core/lib/asset-staging.ts b/packages/aws-cdk-lib/core/lib/asset-staging.ts index e17a0f2c5422e..8991dfbe18634 100644 --- a/packages/aws-cdk-lib/core/lib/asset-staging.ts +++ b/packages/aws-cdk-lib/core/lib/asset-staging.ts @@ -346,7 +346,7 @@ export class AssetStaging extends Construct { // directory after it has been moved to the staging directory if the hash is known before bundling. This way if bundling // is skipped because the bundling directory already exists we can still determine // the correct packaging type. - // If the hash is calculated after bundling we remove the temp dir now. + // If the hash is calculated after bundling we remove the temporary directory now. if (bundledAsset.packaging === FileAssetPackaging.FILE) { if (this.hashType === AssetHashType.OUTPUT || this.hashType === AssetHashType.BUNDLE) { fs.removeSync(path.dirname(bundledAsset.path));