Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

app.getAllContext() returns empty object #2239

Closed
piurafunk opened this issue Apr 25, 2024 · 1 comment · Fixed by #2483, grommir/cdk-test#8 or gentksb/splitwise_automation#15 · May be fixed by Opetushallitus/heratepalvelu#293 or ithunter0428/FastAPI-AWS-Todo-App#65

Comments

@piurafunk
Copy link

I am attempting to parse some context values from the CDK app. The feature was introduced by #1751

My code snippet is:

const app = new cdk.App();
console.log(app.node.getAllContext());

Which outputs:

{}

However, when I toss a console.log() in Node.getAllContext(), it prints my context variables:

console.log(this._context)

Outputs:

...
  '@aws-cdk/aws-ec2:restrictDefaultSecurityGroup': true,
  '@aws-cdk/aws-apigateway:requestValidatorUniqueId': true,
  longTermStorageBucketArn: 'backups-690e',
  'aws:cdk:enable-path-metadata': true,
  'aws:cdk:enable-asset-metadata': true,
...

Where longTermStorageBucketArn is the context I have passed on command line:

npm run cdk -- -c longTermStorageBucketArn=backups-690e synth

I also tested it in the constructor of a Stack object:

console.log('all context', this.node.getAllContext())
// as well as
console.log('all context', scope.node.getAllContext())

both of which produce:

all context {}

Is this the intended behavior? I don't seem to understand how to get all context from the app object, whereas app.node.getContext(...) works as expected.

@matt9ucci
Copy link

I noticed that the getAllContext method returns the value of defaults arg or {} instead of contexts when the construct is a root node (the scope is undefined).

constructs/src/construct.ts

Lines 248 to 252 in d5eb5ff

if (typeof defaults === 'undefined') {
defaults = {};
}
if (this.scope === undefined) { return defaults; }

The cdk.App() is a root node. Therefore, its getAllContext method never returns its contexts.

nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 4, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 5, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 6, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 7, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 8, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 8, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 9, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
nvinson added a commit to nvinson/aws_constructs that referenced this issue Oct 10, 2024
Correct getAllContext() to return the root node's context in addition to
specified defaults.

Closes: aws#2239
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
@mergify mergify bot closed this as completed in #2483 Oct 10, 2024
mergify bot pushed a commit that referenced this issue Oct 10, 2024
Correct getAllContext() to return the root node's context in addition to specified defaults.

Fixes: #2239
# for free to join this conversation on GitHub. Already have an account? # to comment