Skip to content

Commit 5556b60

Browse files
committed
fix(core): make bundlingRequired minimatch matching the same as aws-cdk
The original fix as per 5cff2d9, with the addition of bypassing minimatch when `*` is the given pattern (return true). This gives special meaning to `*`, since `*` in minimatch doesn't produce the desired selection (all stacks). fixes #19927
1 parent d7ac3bb commit 5556b60

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,10 +1168,13 @@ 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+
if (bundlingStacks.length === 1 && bundlingStacks[0] === '*') {
1172+
return true; // bundle all stacks if a stack pattern isn't supplied on the CLI
1173+
}
1174+
11721175
return bundlingStacks.some(pattern => minimatch(
1173-
this.stackName,
1174-
pattern.replace('/', '-'),
1176+
this.node.path, // use the same value for pattern matching as the aws-cdk CLI (displayName / hierarchicalId)
1177+
pattern,
11751178
));
11761179
}
11771180
}

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,81 @@ describe('staging', () => {
912912
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
913913
});
914914

915+
test('correctly skips bundling with stack under stage and custom stack name', () => {
916+
// GIVEN
917+
const app = new App();
918+
919+
const stage = new Stage(app, 'Stage');
920+
stage.node.setContext(cxapi.BUNDLING_STACKS, ['Stage/Stack1']);
921+
922+
const stack1 = new Stack(stage, 'Stack1', { stackName: 'unrelated-stack1-name' });
923+
const stack2 = new Stack(stage, 'Stack2', { stackName: 'unrelated-stack2-name' });
924+
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
925+
926+
// WHEN
927+
new AssetStaging(stack1, 'Asset', {
928+
sourcePath: directory,
929+
assetHashType: AssetHashType.OUTPUT,
930+
bundling: {
931+
image: DockerImage.fromRegistry('alpine'),
932+
command: [DockerStubCommand.SUCCESS],
933+
},
934+
});
935+
936+
new AssetStaging(stack2, 'Asset', {
937+
sourcePath: directory,
938+
assetHashType: AssetHashType.OUTPUT,
939+
bundling: {
940+
image: DockerImage.fromRegistry('alpine'),
941+
command: [DockerStubCommand.MULTIPLE_FILES],
942+
},
943+
});
944+
945+
// THEN
946+
const dockerStubInput = readDockerStubInputConcat();
947+
// Docker ran for the asset in Stack1
948+
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
949+
// Docker did not run for the asset in Stack2
950+
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
951+
});
952+
953+
test('correctly bundles with stack under stage and the default stack pattern', () => {
954+
// GIVEN
955+
const app = new App();
956+
957+
const stage = new Stage(app, 'Stage');
958+
959+
const stack1 = new Stack(stage, 'Stack1');
960+
const stack2 = new Stack(stage, 'Stack2');
961+
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
962+
963+
// WHEN
964+
new AssetStaging(stack1, 'Asset', {
965+
sourcePath: directory,
966+
assetHashType: AssetHashType.OUTPUT,
967+
bundling: {
968+
image: DockerImage.fromRegistry('alpine'),
969+
command: [DockerStubCommand.SUCCESS],
970+
},
971+
});
972+
973+
new AssetStaging(stack2, 'Asset', {
974+
sourcePath: directory,
975+
assetHashType: AssetHashType.OUTPUT,
976+
bundling: {
977+
image: DockerImage.fromRegistry('alpine'),
978+
command: [DockerStubCommand.MULTIPLE_FILES],
979+
},
980+
});
981+
982+
// THEN
983+
const dockerStubInput = readDockerStubInputConcat();
984+
// Docker ran for the asset in Stack1
985+
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
986+
// Docker ran for the asset in Stack2
987+
expect(dockerStubInput).toMatch(DockerStubCommand.MULTIPLE_FILES);
988+
});
989+
915990
test('bundling still occurs with partial wildcard', () => {
916991
// GIVEN
917992
const app = new App();

0 commit comments

Comments
 (0)