Skip to content

Commit

Permalink
fix(cli): 'deploy' and 'diff' silently does nothing when given unknow…
Browse files Browse the repository at this point in the history
…n stack name (#16073)

Currently, `cdk deploy` and `cdk diff` on stacks that do not exist return no output on the command line. This PR introduces a descriptive error message for those cases so it is easier to understand what happened.

Leaving the idea of fuzzy matching for stack name suggestions to a (potential) future PR, I can open a new issue for that if this one goes through.

For the linter, I'm not sure if anything of value can be added to the readme so I'm wondering if this would be a candidate for exemption.

closes #15866 while adding the same idea to `diff` as well as `deploy`.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc committed Aug 18, 2021
1 parent 7e0ad5d commit f35b032
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

await this.validateStacks(stacks);
await this.validateStacks(stacks, selector.patterns);

return stacks;
}
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f35b032

Please sign in to comment.