Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): single-file bundling breaks due to left over temp dir #28566

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/aws-cdk-lib/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 temporary directory 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 {
Expand Down
31 changes: 31 additions & 0 deletions packages/aws-cdk-lib/core/test/staging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down