Skip to content

core: incorrect asset bundling skip for assets in stacks inside stages inside stacks #21925

@plumdog

Description

@plumdog

Describe the bug

Eg when you have:

  • PipelineStack
    • DeploymentStage
      • DeploymentStack
        • SomeLambda

If I try to deploy just the DeploymentStack with npm run -- cdk deploy -e pipeline-stack/deployment-stage/deployment-stack, the asset for SomeLambda is not bundled. Instead, I just get the whole repo, and Lambda complains about the zip being too big.

I think this might be because of this line here: https://github.com/aws/aws-cdk/blob/v1-main/packages/@aws-cdk/core/lib/stack.ts#L1179

Perhaps that .replace should instead act on the last occurrence in the string, not the first. This is half right. this.stackName is deployment-stage-deployment-stack, but bundlingStacks from cxapi.BUNDLING_STACKS is pipeline-stack/deployment-stage/deployment-stack. So to get the correct behaviour need to munge pattern as follows:

  • replace the last / with a -
  • then remove everything up-to-and-including any remaining /

But this feels - in general - unsafe. Why are cxapi.BUNDLING_STACKS and this.stackName in different formats and what is a valid way of comparing them?

The above would match with some crude debugging I bodged into my project where I encountered this, and with the comment there "bundlingStacks is of the form Stage/Stack, convert it to Stage-Stack before comparing to stack name": if I override bundlingRequired in my stack and print out what this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*'] is, I get ['pipeline-stack/deployment-stage/deployment-stack'], but this.stackName is 'deployment-stage-deployment-stack'.

So when the first / is replaced and the minimatch comparison occurs, the match fails, because it was the first / that was replaced. If it was the last, then it would match. (false, see above)

Expected Behavior

The asset for the stack I am deployed to be bundled.

Current Behavior

The project root is zipped as the asset.

Reproduction Steps

I'm in the process of trying to write a failing test, but am fighting buildup atm.

The test I'm trying to write looks just like the test added in cda6601 but with an extra layer of nesting, like Stack1/Stage/Stack2.

Possible Solution

In Stack.bundlingRequired, replace the last / in pattern, not the first.

Additional Information/Context

I think this is related to #15346

CDK CLI Version

2.35.0

Framework Version

No response

Node.js Version

16

OS

Linux

Language

Typescript

Language Version

No response

Other information

My workaround is to put:

public override get bundlingRequired() {
    return true;
}

in the Stack that contains the asset. This results in the expected behaviour.

Metadata

Metadata

Assignees

Labels

@aws-cdk/coreRelated to core CDK functionalitybugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions