diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 55ca1c6e24903..8edefe66fc333 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -397,7 +397,7 @@ export class CdkToolkit { defaultBehavior: DefaultSelection.OnlySingle, }); - await this.validateStacks(stacks); + await this.validateStacks(stacks, selector.patterns); return stacks; } @@ -415,7 +415,7 @@ export class CdkToolkit { ? allStacks.filter(art => art.validateOnSynth ?? false) : new StackCollection(assembly, []); - await this.validateStacks(selectedForDiff.concat(autoValidateStacks)); + await this.validateStacks(selectedForDiff.concat(autoValidateStacks), stackNames); return selectedForDiff; } @@ -435,7 +435,11 @@ export class CdkToolkit { /** * Validate the stacks for errors and warnings according to the CLI's current settings */ - private async validateStacks(stacks: StackCollection) { + private async validateStacks(stacks: StackCollection, stackNames: string[]) { + if (stacks.stackCount == 0) { + throw new Error(`No stacks match the name(s) ${stackNames}`); + } + stacks.processMetadataMessages({ ignoreErrors: this.props.ignoreErrors, strict: this.props.strict, diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index 8f53405b749e4..f4573637ee5f4 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -39,6 +39,14 @@ function defaultToolkitSetup() { } describe('deploy', () => { + test('fails when no valid stack names are given', async () => { + // GIVEN + const toolkit = defaultToolkitSetup(); + + // WHEN + await expect(() => toolkit.deploy({ selector: { patterns: ['Test-Stack-D'] } })).rejects.toThrow('No stacks match the name(s) Test-Stack-D'); + }); + describe('makes correct CloudFormation calls', () => { test('without options', async () => { // GIVEN diff --git a/packages/aws-cdk/test/diff.test.ts b/packages/aws-cdk/test/diff.test.ts index c1bcd11c78caa..e14ebe9bd9779 100644 --- a/packages/aws-cdk/test/diff.test.ts +++ b/packages/aws-cdk/test/diff.test.ts @@ -121,6 +121,16 @@ test('throws an error during diffs on stack with error metadata', async () => { })).rejects.toThrow(/Found errors/); }); +test('throws an error if no valid stack names given', async () => { + const buffer = new StringWritable(); + + // WHEN + await expect(() => toolkit.diff({ + stackNames: ['X', 'Y', 'Z'], + stream: buffer, + })).rejects.toThrow('No stacks match the name(s) X,Y,Z'); +}); + class StringWritable extends Writable { public data: string; private readonly _decoder: NodeStringDecoder;