Skip to content

Commit 7a04a5a

Browse files
authored
fix(synthetics): synth-time failure for canary assets in nested stages (#27167)
Fixes #27089. Canaries inside stages would fail at synth time after #26291 was introduced. This was due to a misunderstanding of `assetOutdir`. It turns out that `this.asset.assetPath` is a relative path of `Stage.of(scope).outdir`, not of `Stage.of(scope).assetOutdir`. These two out directories would be equivalent at the root level, but if there were nested stages, `assetOutdir` would equal `../${outdir}`. This diagram may help: ``` CLOUD ASSEMBLY ROOT +-- asset.12345abcdef/ +-- assembly-Stage +-- MyStack.template.json +-- MyStack.assets.json <- will contain { "path": "../asset.12345abcdef" } ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fe0955f commit 7a04a5a

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎packages/@aws-cdk/aws-synthetics-alpha/lib/code.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export class AssetCode extends Code {
132132
}
133133

134134
// Get the staged (or copied) asset path.
135-
const assetOutDir = Stage.of(scope)?.assetOutdir;
136-
const assetPath = assetOutDir ? path.join(assetOutDir, this.asset.assetPath): this.assetPath;
135+
// `this.asset.assetPath` is relative to the `outdir`, not the `assetOutDir`.
136+
const asmManifestDir = Stage.of(scope)?.outdir;
137+
const assetPath = asmManifestDir ? path.join(asmManifestDir, this.asset.assetPath): this.assetPath;
137138

138139
if (path.extname(assetPath) !== '.zip') {
139140
if (!fs.lstatSync(assetPath).isDirectory()) {

‎packages/@aws-cdk/aws-synthetics-alpha/test/code.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import { Template } from 'aws-cdk-lib/assertions';
44
import * as s3 from 'aws-cdk-lib/aws-s3';
5-
import { App, Stack, DockerImage } from 'aws-cdk-lib';
5+
import { App, Stage, Stack, DockerImage } from 'aws-cdk-lib';
66
import * as cxapi from 'aws-cdk-lib/cx-api';
77
import * as synthetics from '../lib';
88
import { RuntimeFamily } from '../lib';
@@ -123,6 +123,24 @@ describe(synthetics.Code.fromAsset, () => {
123123
expect(synthesized.assets.length).toEqual(1);
124124
});
125125

126+
test('works when stack is a part of a stage', () => {
127+
// GIVEN
128+
const app = new App();
129+
const stage1 = new Stage(app, 'Stage1');
130+
const stage2 = new Stage(stage1, 'Stage2');
131+
const stack = new Stack(stage2);
132+
133+
// WHEN
134+
const directoryAsset = synthetics.Code.fromAsset(path.join(__dirname, 'canaries'));
135+
new synthetics.Canary(stack, 'Canary1', {
136+
test: synthetics.Test.custom({
137+
handler: 'canary.handler',
138+
code: directoryAsset,
139+
}),
140+
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_3_8,
141+
});
142+
});
143+
126144
test('fails if path does not exist', () => {
127145
const assetPath = path.join(__dirname, 'does-not-exist');
128146
expect(() => synthetics.Code.fromAsset(assetPath))

‎packages/aws-cdk-lib/core/lib/stage.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ export class Stage extends Construct {
197197
* will return an empty string.
198198
*
199199
* Derived from the construct path.
200-
*
201200
*/
202201
public get artifactId() {
203202
if (!this.node.path) { return ''; }

0 commit comments

Comments
 (0)