diff --git a/packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts b/packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts index 104cc7ec1..3f551710d 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts @@ -1889,7 +1889,9 @@ export class Toolkit extends CloudAssemblySourceBuilder { stackExisted: result.stackArn !== undefined, }); - await ioHelper.notify(IO.CDK_TOOLKIT_I7900.msg(chalk.green(`\n✅ ${chalk.blue(stack.displayName)}: ${action}ed`), stack)); + // `destroyStack` returns no ARN when CloudFormation has no such stack: nothing was deleted + const outcome = result.stackArn === undefined ? 'not deployed, nothing to destroy' : `${action}ed`; + await ioHelper.notify(IO.CDK_TOOLKIT_I7900.msg(chalk.green(`\n✅ ${chalk.blue(stack.displayName)}: ${outcome}`), stack)); if (options.express) { const warning = formatExpressStabilizationWarning(result.stabilizingResources, 'destroy'); diff --git a/packages/@aws-cdk/toolkit-lib/test/actions/destroy.test.ts b/packages/@aws-cdk/toolkit-lib/test/actions/destroy.test.ts index 5abcfa440..3890b8801 100644 --- a/packages/@aws-cdk/toolkit-lib/test/actions/destroy.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/actions/destroy.test.ts @@ -17,7 +17,10 @@ beforeEach(() => { ioHost.requestSpy.mockClear(); jest.clearAllMocks(); - mockDestroyStack = jest.spyOn(deployments.Deployments.prototype, 'destroyStack').mockResolvedValue({ stabilizingResources: [] }); + mockDestroyStack = jest.spyOn(deployments.Deployments.prototype, 'destroyStack').mockResolvedValue({ + stackArn: 'arn:aws:cloudformation:region:account:stack/test-stack', + stabilizingResources: [], + }); }); describe('destroy', () => { @@ -44,6 +47,27 @@ describe('destroy', () => { })); }); + test('reports a stack that is not deployed instead of claiming it was destroyed', async () => { + // GIVEN: CloudFormation has no such stack, so destroyStack returns no ARN + mockDestroyStack.mockResolvedValue({ stabilizingResources: [] }); + + // WHEN + const cx = await builderFixture(toolkit, 'stack-with-role'); + await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); + + // THEN + expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ + action: 'destroy', + level: 'result', + code: 'CDK_TOOLKIT_I7900', + message: expect.stringContaining('not deployed, nothing to destroy'), + })); + expect(ioHost.notifySpy).not.toHaveBeenCalledWith(expect.objectContaining({ + code: 'CDK_TOOLKIT_I7900', + message: expect.stringContaining(': destroyed'), + })); + }); + test('multiple stacks', async () => { // WHEN const cx = await builderFixture(toolkit, 'two-empty-stacks');