Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): cdk destroy does not throw error for a non-existent stack #27293

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,13 @@ integTest('hotswap deployment supports Fn::ImportValue intrinsic', withDefaultFi
}
}));

integTest('cdk destroy fails when the stacks do not exist', withDefaultFixture(async (fixture) => {
const nonExistingStackName1 = 'non-existing-stack-1';
const nonExistingStackName2 = 'non-existing-stack-2';

await expect(fixture.cdkDestroy([nonExistingStackName1, nonExistingStackName2])).rejects.toThrow('exited with error');
}));

async function listChildren(parent: string, pred: (x: string) => Promise<boolean>) {
const ret = new Array<string>();
for (const child of await fs.readdir(parent, { encoding: 'utf-8' })) {
Expand Down
10 changes: 9 additions & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
import { minimatch } from 'minimatch';
import * as promptly from 'promptly';
import * as semver from 'semver';
import { DeploymentMethod } from './api';
import { SdkProvider } from './api/aws-auth';
import { Bootstrapper, BootstrapEnvironmentOptions } from './api/bootstrap';
Expand All @@ -26,6 +28,7 @@ import { validateSnsTopicArn } from './util/validate-notification-arn';
import { Concurrency, WorkGraph } from './util/work-graph';
import { WorkGraphBuilder } from './util/work-graph-builder';
import { AssetBuildNode, AssetPublishNode, StackNode } from './util/work-graph-types';
import { versionNumber } from './version';
import { environmentsFromDescriptors, globEnvironmentsFromStacks, looksLikeGlob } from '../lib/api/cxapp/environments';

export interface CdkToolkitProps {
Expand Down Expand Up @@ -768,7 +771,12 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const notExistPatterns = selector.patterns.filter(pattern => !stacks.stackArtifacts.find(stack =>
minimatch(stack.hierarchicalId, pattern) || (stack.id === pattern && semver.major(versionNumber()) < 2),
));
if (notExistPatterns.length > 0) {
throw new Error(`Cannot run cdk destroy on stack(s) ${selector.patterns.join(', ')}. ${notExistPatterns.join(', ')} not exist.`);
}

return stacks;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,19 @@ describe('destroy', () => {
});
}).resolves;
});

test('fail on non-existent stack', async () => {
const toolkit = defaultToolkitSetup();

await expect(() => {
return toolkit.destroy({
selector: { patterns: ['Test-Stack-A/Test-Stack-C', 'Test-Stack-X', 'Test-Stack-Y'] },
exclusively: true,
force: true,
fromDeploy: true,
});
}).rejects.toThrowError('Cannot run cdk destroy on stack(s) Test-Stack-A/Test-Stack-C, Test-Stack-X, Test-Stack-Y. Test-Stack-X, Test-Stack-Y not exist.');
});
});

describe('watch', () => {
Expand Down
Loading