Cannot set context at app level anymore after v1.9.0. This is because a Tree child is automatically created under the app and context cannot be set after children have been added.
Comes from #4194 (3cca03d).
const app = new cdk.App();
app.node.setContext('stage', 'dev');
Error: Cannot set context after children have been added: Tree
This is :bug: Bug Report
You can specify the context when you initialise the app:
new App({ context: { key: 'value' });
Closing for now.
How would you set the context if not already set by the CLI?
I was doing:
const app = new cdk.App();
// Get and set stage context
const stage = app.node.tryGetContext('stage') || process.env.STAGE || 'dev';
app.node.setContext('stage', stage);
This should work since the CLI context is applied after the initializer鈥檚:
new App({
context: { stage: process.env.STAGE || 'dev' }
})
Let me know if this works.
It works. Thanks 馃憤
(could be documented)
@eladb In the case of setting context when we initialize the app, how should the nested structures be handled? Context is expecting [key: string]: string which doesn't allow setting a key to a nested object. Trying to set the context for js unit test.
Most helpful comment
You can specify the context when you initialise the app:
Closing for now.