From 4c4e90f83208d777ae1ca344349fa766d25e969b Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Thu, 24 Sep 2020 12:20:12 +0200 Subject: [PATCH] fix(core): bundling with staging disabled returns a relative path The change introduced in #9576 did not handle the "staging disabled" case. As a consequence, when bundling the staged path was always relative. Revert to the behavior that was present before this change: when staging is disabled the staged path is absolute (whether bundling or not). Closes #10367 --- packages/@aws-cdk/core/lib/asset-staging.ts | 14 +++++++------- packages/@aws-cdk/core/test/test.staging.ts | 12 ++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/@aws-cdk/core/lib/asset-staging.ts b/packages/@aws-cdk/core/lib/asset-staging.ts index e5878c2a31365..53f607f9588a0 100644 --- a/packages/@aws-cdk/core/lib/asset-staging.ts +++ b/packages/@aws-cdk/core/lib/asset-staging.ts @@ -120,14 +120,14 @@ export class AssetStaging extends Construct { } } else { this.assetHash = this.calculateHash(hashType, props.assetHash); + this.relativePath = renderAssetFilename(this.assetHash, path.extname(this.sourcePath)); + this.stagedPath = this.relativePath; + } - const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT); - if (stagingDisabled) { - this.stagedPath = this.sourcePath; - } else { - this.relativePath = renderAssetFilename(this.assetHash, path.extname(this.sourcePath)); - this.stagedPath = this.relativePath; - } + const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT); + if (stagingDisabled) { + this.relativePath = undefined; + this.stagedPath = this.bundleDir ?? this.sourcePath; } this.sourceHash = this.assetHash; diff --git a/packages/@aws-cdk/core/test/test.staging.ts b/packages/@aws-cdk/core/test/test.staging.ts index e652c884935f4..996f56d840c05 100644 --- a/packages/@aws-cdk/core/test/test.staging.ts +++ b/packages/@aws-cdk/core/test/test.staging.ts @@ -144,11 +144,11 @@ export = { // GIVEN const app = new App(); const stack = new Stack(app, 'stack'); - stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, true); + stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, false); const directory = path.join(__dirname, 'fs', 'fixtures', 'test1'); // WHEN - new AssetStaging(stack, 'Asset', { + const asset = new AssetStaging(stack, 'Asset', { sourcePath: directory, bundling: { image: BundlingDockerImage.fromRegistry('alpine'), @@ -167,6 +167,14 @@ export = { 'tree.json', ]); + test.equal(asset.sourceHash, 'b1e32e86b3523f2fa512eb99180ee2975a50a4439e63e8badd153f2a68d61aa4'); + test.equal(asset.sourcePath, directory); + + const resolvedStagePath = stack.resolve(asset.stagedPath); + // absolute path ending with bundling dir + test.ok(path.isAbsolute(resolvedStagePath)); + test.ok(new RegExp('asset.b1e32e86b3523f2fa512eb99180ee2975a50a4439e63e8badd153f2a68d61aa4$').test(resolvedStagePath)); + test.done(); },