Skip to content

Commit 60e5bbd

Browse files
committed
fix(core): use node.path in skip bundling check for consistency with CLI
The pattern given to aws-cdk on the CLI is matched against `hierarchicalId` (i.e. displayName, node.path) [1] [2] [3]. The same pattern is given to the CDK app but was matching against `stackName`, causing potential for bundling to be skipped under certain scenarios (i.e. using --exclusively with custom stack names). To make them consistent, the skip bundling check has been changed to use the same value (node.path/hierarchicalId/displayName) as aws-cdk when deciding whether to bundle an asset. fixes #19927 [1] https://github.com/aws/aws-cdk/blob/1d0270446b3effa6b8518de3c7d76f0c14e626c5/packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts#L138 [2] https://github.com/aws/aws-cdk/blob/6cca62db88866479f2b1d4f52b30beab3d78aeb2/packages/@aws-cdk/cx-api/lib/cloud-artifact.ts#L143-L145 [3] https://github.com/aws/aws-cdk/blob/6cca62db88866479f2b1d4f52b30beab3d78aeb2/packages/@aws-cdk/core/lib/stack-synthesizers/_shared.ts#L66
1 parent bf35048 commit 60e5bbd

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

packages/@aws-cdk/core/lib/stack.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,10 +1168,10 @@ export class Stack extends Construct implements ITaggable {
11681168
public get bundlingRequired() {
11691169
const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*'];
11701170

1171-
// bundlingStacks is of the form `Stage/Stack`, convert it to `Stage-Stack` before comparing to stack name
1171+
// bundlingStacks is of the form `Stage/Stack`
11721172
return bundlingStacks.some(pattern => minimatch(
1173-
this.stackName,
1174-
pattern.replace('/', '-'),
1173+
this.node.path, // the same value used for pattern matching in aws-cdk CLI (displayName / hierarchicalId)
1174+
pattern,
11751175
));
11761176
}
11771177
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,45 @@ describe('staging', () => {
844844
const dockerStubInput = readDockerStubInputConcat();
845845
// Docker ran for the asset in Stack1
846846
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
847-
// DOcker did not run for the asset in Stack2
847+
// Docker did not run for the asset in Stack2
848+
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
849+
});
850+
851+
test('correctly skips bundling with stack under stage and custom stack name', () => {
852+
// GIVEN
853+
const app = new App();
854+
855+
const stage = new Stage(app, 'Stage');
856+
stage.node.setContext(cxapi.BUNDLING_STACKS, ['Stage/Stack1']);
857+
858+
const stack1 = new Stack(stage, 'Stack1', { stackName: 'unrelated-stack1-name' });
859+
const stack2 = new Stack(stage, 'Stack2', { stackName: 'unrelated-stack2-name' });
860+
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
861+
862+
// WHEN
863+
new AssetStaging(stack1, 'Asset', {
864+
sourcePath: directory,
865+
assetHashType: AssetHashType.OUTPUT,
866+
bundling: {
867+
image: DockerImage.fromRegistry('alpine'),
868+
command: [DockerStubCommand.SUCCESS],
869+
},
870+
});
871+
872+
new AssetStaging(stack2, 'Asset', {
873+
sourcePath: directory,
874+
assetHashType: AssetHashType.OUTPUT,
875+
bundling: {
876+
image: DockerImage.fromRegistry('alpine'),
877+
command: [DockerStubCommand.MULTIPLE_FILES],
878+
},
879+
});
880+
881+
// THEN
882+
const dockerStubInput = readDockerStubInputConcat();
883+
// Docker ran for the asset in Stack1
884+
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
885+
// Docker did not run for the asset in Stack2
848886
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
849887
});
850888

0 commit comments

Comments
 (0)