Skip to content

Commit a74aacf

Browse files
authored
fix(core): single-file bundling breaks due to left over temp dir (#28566)
This change fixes a bad behavior of the asset bundling if we use the SINGLE_FILE asset type with the OUTPUT hash type. Because only the created file is moved and the temporary bundle dir is left over, subsequent bundling runs fail and create empty asset files. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 27f27b0 commit a74aacf

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/aws-cdk-lib/core/lib/asset-staging.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,16 @@ export class AssetStaging extends Construct {
343343
this.stageAsset(bundledAsset.path, stagedPath, 'move');
344344

345345
// If bundling produced a single archive file we "touch" this file in the bundling
346-
// directory after it has been moved to the staging directory. This way if bundling
346+
// directory after it has been moved to the staging directory if the hash is known before bundling. This way if bundling
347347
// is skipped because the bundling directory already exists we can still determine
348348
// the correct packaging type.
349+
// If the hash is calculated after bundling we remove the temporary directory now.
349350
if (bundledAsset.packaging === FileAssetPackaging.FILE) {
350-
fs.closeSync(fs.openSync(bundledAsset.path, 'w'));
351+
if (this.hashType === AssetHashType.OUTPUT || this.hashType === AssetHashType.BUNDLE) {
352+
fs.removeSync(path.dirname(bundledAsset.path));
353+
} else {
354+
fs.closeSync(fs.openSync(bundledAsset.path, 'w'));
355+
}
351356
}
352357

353358
return {

packages/aws-cdk-lib/core/test/staging.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,37 @@ describe('staging', () => {
14191419
expect(staging.isArchive).toEqual(false);
14201420
});
14211421

1422+
test('bundling that produces a single file with SINGLE_FILE and hash type OUTPUT', () => {
1423+
// GIVEN
1424+
const app = new App({ context: { [cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT]: false } });
1425+
const stack = new Stack(app, 'stack');
1426+
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1', 'subdir');
1427+
1428+
// WHEN
1429+
const staging = new AssetStaging(stack, 'Asset', {
1430+
sourcePath: directory,
1431+
assetHashType: AssetHashType.OUTPUT,
1432+
bundling: {
1433+
image: DockerImage.fromRegistry('alpine'),
1434+
command: [DockerStubCommand.SINGLE_FILE],
1435+
outputType: BundlingOutput.SINGLE_FILE,
1436+
},
1437+
});
1438+
1439+
// THEN
1440+
const assembly = app.synth();
1441+
expect(fs.readdirSync(assembly.directory)).toEqual([
1442+
// 'bundling-temp-0e346bd27baa32f4f2d15d1d73c8972db3293080f6c2836328b7bf77747683db', this directory gets removed and does no longer exist
1443+
'asset.95c924c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.txt',
1444+
'cdk.out',
1445+
'manifest.json',
1446+
'stack.template.json',
1447+
'tree.json',
1448+
]);
1449+
expect(staging.packaging).toEqual(FileAssetPackaging.FILE);
1450+
expect(staging.isArchive).toEqual(false);
1451+
});
1452+
14221453
});
14231454

14241455
describe('staging with docker cp', () => {

0 commit comments

Comments
 (0)