From f5d43f200f71c12cfe413078f2a7407165dd1461 Mon Sep 17 00:00:00 2001 From: Neta Nir Date: Fri, 3 Jan 2020 12:48:17 -0800 Subject: [PATCH] fix(core): nested stacks does not report missing context #5594 --- .../test/test.nested-stack.ts | 31 +++++++++++++++++-- packages/@aws-cdk/core/lib/stack.ts | 11 +++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudformation/test/test.nested-stack.ts b/packages/@aws-cdk/aws-cloudformation/test/test.nested-stack.ts index d4437d05265aa..f2748ad57d607 100644 --- a/packages/@aws-cdk/aws-cloudformation/test/test.nested-stack.ts +++ b/packages/@aws-cdk/aws-cloudformation/test/test.nested-stack.ts @@ -1,7 +1,7 @@ import { expect, haveResource, SynthUtils } from '@aws-cdk/assert'; import * as s3_assets from '@aws-cdk/aws-s3-assets'; import * as sns from '@aws-cdk/aws-sns'; -import { App, CfnParameter, CfnResource, Construct, Stack } from '@aws-cdk/core'; +import { App, CfnParameter, CfnResource, Construct, ContextProvider, Stack } from '@aws-cdk/core'; import * as fs from 'fs'; import { Test } from 'nodeunit'; import * as path from 'path'; @@ -872,5 +872,32 @@ export = { })); test.done(); - } + }, + + 'missing context in nested stack is reported if the context is not available'(test: Test) { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'ParentStack', { env: { account: '1234account', region: 'us-east-44' } }); + const nestedStack = new NestedStack(stack, 'nested'); + const provider = 'dummyProvider'; + const expectedKey = ContextProvider.getKey(nestedStack, { + provider + }).key; + + // WHEN + ContextProvider.getValue(nestedStack, { + provider, + dummyValue: ['dummy1a', 'dummy1b', 'dummy1c'], + }); + + // THEN: missing context is reported in the cloud assembly + const asm = app.synth(); + const missing = asm.manifest.missing; + + test.ok(missing && missing.find(m => { + return (m.key === expectedKey); + })); + + test.done(); + }, }; diff --git a/packages/@aws-cdk/core/lib/stack.ts b/packages/@aws-cdk/core/lib/stack.ts index 171896df671be..9431ae9eb9287 100644 --- a/packages/@aws-cdk/core/lib/stack.ts +++ b/packages/@aws-cdk/core/lib/stack.ts @@ -791,6 +791,10 @@ export class Stack extends Construct implements ITaggable { const text = JSON.stringify(this._toCloudFormation(), undefined, 2); fs.writeFileSync(outPath, text); + for (const ctx of this._missingContext) { + builder.addMissing(ctx); + } + // if this is a nested stack, do not emit it as a cloud assembly artifact (it will be registered as an s3 asset instead) if (this.nested) { return; @@ -798,7 +802,6 @@ export class Stack extends Construct implements ITaggable { const deps = this.dependencies.map(s => s.artifactId); const meta = this.collectMetadata(); - // backwards compatibility since originally artifact ID was always equal to // stack name the stackName attribute is optional and if it is not specified // the CLI will use the artifact ID as the stack name. we *could have* @@ -823,10 +826,6 @@ export class Stack extends Construct implements ITaggable { dependencies: deps.length > 0 ? deps : undefined, metadata: Object.keys(meta).length > 0 ? meta : undefined, }); - - for (const ctx of this._missingContext) { - builder.addMissing(ctx); - } } /** @@ -975,12 +974,10 @@ export class Stack extends Construct implements ITaggable { if (parent !== stack) { return; } - if (node.node.metadata.length > 0) { // Make the path absolute output[ConstructNode.PATH_SEP + node.node.path] = node.node.metadata.map(md => stack.resolve(md) as cxapi.MetadataEntry); } - for (const child of node.node.children) { visit(child); }