Skip to content

Commit

Permalink
fix(cli): empty non top-level stack does not get deleted (#21624)
Browse files Browse the repository at this point in the history
Fixed selector pattern for the empty stack which must be deleted during the deployment. I set stack selector pattern as 
`stack.hierarchicalId` because hierarchicalId is used in `selectMatchingStacks` method which is called when destroy logic is looking for the stack to be deleted.

https://github.com/aws/aws-cdk/blob/92d6d58029595735df6902db5f820b1182dfb27b/packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts#L138

There is also existing integration test which covers destroy logic and it works now without additional modifications:

https://github.com/aws/aws-cdk/blob/92d6d58029595735df6902db5f820b1182dfb27b/packages/aws-cdk/test/integ/cli/cli.integtest.ts#L685

**How I tested it locally?**

- Prepared a package with fix with `yarn package` and installed with `npm install -g dist/js/aws-cdk-0.0.0.tgz`
- Reproduced steps from the bug #20822
- Ensured that the issue is fixed

closes #20822 

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
arewa authored Aug 17, 2022
1 parent f5a1057 commit a6757b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class CdkToolkit {
} else {
warning('%s: stack has no resources, deleting existing stack.', chalk.bold(stack.displayName));
await this.destroy({
selector: { patterns: [stack.stackName] },
selector: { patterns: [stack.hierarchicalId] },
exclusively: true,
force: true,
roleArn: options.roleArn,
Expand Down
22 changes: 21 additions & 1 deletion packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jest.mock('../lib/logging', () => ({
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { Bootstrapper } from '../lib/api/bootstrap';
import { CloudFormationDeployments, DeployStackOptions } from '../lib/api/cloudformation-deployments';
import { CloudFormationDeployments, DeployStackOptions, DestroyStackOptions } from '../lib/api/cloudformation-deployments';
import { DeployStackResult } from '../lib/api/deploy-stack';
import { Template } from '../lib/api/util/cloudformation';
import { CdkToolkit, Tag } from '../lib/cdk-toolkit';
Expand Down Expand Up @@ -558,6 +558,21 @@ describe('deploy', () => {
});
});

describe('destroy', () => {
test('destroy correct stack', async () => {
const toolkit = defaultToolkitSetup();

await expect(() => {
return toolkit.destroy({
selector: { patterns: ['Test-Stack-A/Test-Stack-C'] },
exclusively: true,
force: true,
fromDeploy: true,
});
}).resolves;
});
});

describe('watch', () => {
test("fails when no 'watch' settings are found", async () => {
const toolkit = defaultToolkitSetup();
Expand Down Expand Up @@ -931,6 +946,11 @@ class FakeCloudFormation extends CloudFormationDeployments {
});
}

public destroyStack(options: DestroyStackOptions): Promise<void> {
expect(options.stack).toBeDefined();
return Promise.resolve();
}

public readCurrentTemplate(stack: cxapi.CloudFormationStackArtifact): Promise<Template> {
switch (stack.stackName) {
case MockStack.MOCK_STACK_A.stackName:
Expand Down

0 comments on commit a6757b0

Please sign in to comment.