Skip to content

(aws-cdk-lib): App.of() returns incorrect values when falling through to Stage.of() #36484

@alias-rahil

Description

@alias-rahil

Describe the bug

App.of() falls through to Stage.of(), which finds the first Stage in scopes (excluding itself), causing:

  1. Returns Stage instead of App when both exist in hierarchy
  2. Returns undefined when called on App itself

Fix: Direct root check using construct.node.root and App.isApp() validation.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

App.of() should return the root App.

Current Behavior

App.of() can sometimes return a Stage (which is not an App) or undefined.

Reproduction Steps

const app = new App();
const stage = new Stage(app, 'TestStage');
const stack = new Stack(stage, 'TestStack');
App.of(stack); // Returns 'stage' instead of 'app' ❌

const app2 = new App();
App.of(app2); // Returns undefined instead of 'app2' ❌

Possible Solution

#36475

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

ALL

AWS CDK CLI version

N/A

Node.js Version

N/A

OS

N/A

Language

N/A

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    aws-cdk-libRelated to the aws-cdk-lib packagebugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions