diff --git a/packages/@aws-cdk/core/lib/stack.ts b/packages/@aws-cdk/core/lib/stack.ts index 23097a90914f2..325863d2a92fd 100644 --- a/packages/@aws-cdk/core/lib/stack.ts +++ b/packages/@aws-cdk/core/lib/stack.ts @@ -1168,10 +1168,10 @@ export class Stack extends Construct implements ITaggable { public get bundlingRequired() { const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*']; - // bundlingStacks is of the form `Stage/Stack`, convert it to `Stage-Stack` before comparing to stack name + // bundlingStacks is of the form `Stage/Stack` return bundlingStacks.some(pattern => minimatch( - this.stackName, - pattern.replace('/', '-'), + this.node.path, // the same value used for pattern matching in aws-cdk CLI (displayName / hierarchicalId) + pattern, )); } } diff --git a/packages/@aws-cdk/core/test/staging.test.ts b/packages/@aws-cdk/core/test/staging.test.ts index f577ddf3b9024..a1cf81aec2eed 100644 --- a/packages/@aws-cdk/core/test/staging.test.ts +++ b/packages/@aws-cdk/core/test/staging.test.ts @@ -844,7 +844,45 @@ describe('staging', () => { const dockerStubInput = readDockerStubInputConcat(); // Docker ran for the asset in Stack1 expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS); - // DOcker did not run for the asset in Stack2 + // Docker did not run for the asset in Stack2 + expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES); + }); + + test('correctly skips bundling with stack under stage and custom stack name', () => { + // GIVEN + const app = new App(); + + const stage = new Stage(app, 'Stage'); + stage.node.setContext(cxapi.BUNDLING_STACKS, ['Stage/Stack1']); + + const stack1 = new Stack(stage, 'Stack1', { stackName: 'unrelated-stack1-name' }); + const stack2 = new Stack(stage, 'Stack2', { stackName: 'unrelated-stack2-name' }); + const directory = path.join(__dirname, 'fs', 'fixtures', 'test1'); + + // WHEN + new AssetStaging(stack1, 'Asset', { + sourcePath: directory, + assetHashType: AssetHashType.OUTPUT, + bundling: { + image: DockerImage.fromRegistry('alpine'), + command: [DockerStubCommand.SUCCESS], + }, + }); + + new AssetStaging(stack2, 'Asset', { + sourcePath: directory, + assetHashType: AssetHashType.OUTPUT, + bundling: { + image: DockerImage.fromRegistry('alpine'), + command: [DockerStubCommand.MULTIPLE_FILES], + }, + }); + + // THEN + const dockerStubInput = readDockerStubInputConcat(); + // Docker ran for the asset in Stack1 + expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS); + // Docker did not run for the asset in Stack2 expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES); });