Skip to content

Commit 5cff2d9

Browse files
fix(core): use node.path in skip bundling check for consistency with cdk deploy CLI (aws#19950)
The pattern given to aws-cdk on the CLI (`cdk deploy $pattern`) is matched against `stack.hierarchicalId` (i.e. displayName, node.path) [1] [2] [3]. The same pattern is given to the CDK app (as `bundlingStacks`) but is 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 match against the same value as `cdk deploy $pattern` (node.path/hierarchicalId/displayName) when deciding whether to bundle an asset. Making them consistent ensures necessary stacks are bundled, avoiding the issue of uploading the entire project directory since the asset wasn't bundled. fixes aws#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 ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 69d25a6 commit 5cff2d9

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)