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

feat(cli): warn of non-existent stacks in cdk destroy #32636

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 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 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 * as uuid from 'uuid';
import { DeploymentMethod, SuccessfulDeployStackResult } from './api';
import { SdkProvider } from './api/aws-auth';
Expand Down Expand Up @@ -53,6 +55,7 @@
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';

// Must use a require() otherwise esbuild complains about calling a namespace
Expand Down Expand Up @@ -802,6 +805,9 @@
stacks = stacks.reversed();

if (!options.force) {
if (stacks.stackArtifacts.length === 0) {
return;

Check warning on line 809 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L809

Added line #L809 was not covered by tests
}
// eslint-disable-next-line max-len
const confirmed = await promptly.confirm(
`Are you sure you want to delete: ${chalk.blue(stacks.stackArtifacts.map((s) => s.hierarchicalId).join(', '))} (y/n)?`,
Expand Down Expand Up @@ -1157,8 +1163,43 @@
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const selectorWithoutPatterns: StackSelector = {
...selector,
allTopLevel: true,
patterns: [],
};

Check warning on line 1170 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1170

Added line #L1170 was not covered by tests
const stacksWithoutPatterns = await assembly.selectStacks(selectorWithoutPatterns, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
defaultBehavior: DefaultSelection.OnlySingle,
});

const patterns = selector.patterns.map(pattern => {

Check warning on line 1176 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1175-L1176

Added lines #L1175 - L1176 were not covered by tests
const notExist = !stacks.stackArtifacts.find(stack =>
minimatch(stack.hierarchicalId, pattern) || (stack.id === pattern && semver.major(versionNumber()) < 2),
);

const closelyMatched = notExist ? stacksWithoutPatterns.stackArtifacts.map(stack => {
if (minimatch(stack.hierarchicalId.toLowerCase(), pattern.toLowerCase())) {

Check warning on line 1182 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1182

Added line #L1182 was not covered by tests
return stack.hierarchicalId;
}
if (stack.id.toLowerCase() === pattern.toLowerCase() && semver.major(versionNumber()) < 2) {

Check warning on line 1185 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1185

Added line #L1185 was not covered by tests
return stack.id;
}
return;
}).filter((stack): stack is string => stack !== undefined) : [];

Check warning on line 1189 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1187-L1189

Added lines #L1187 - L1189 were not covered by tests
return {
pattern,
notExist,
closelyMatched,
};
});

patterns.forEach(pattern => {
if (pattern.notExist) {
const closelyMatched = pattern.closelyMatched.length > 0 ? ` Do you mean ${pattern.closelyMatched.join(', ')}?` : '';

Check warning on line 1199 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1199

Added line #L1199 was not covered by tests
warning(`${pattern.pattern} does not exist.${closelyMatched}`);
}
});

Check warning on line 1202 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1202

Added line #L1202 was not covered by tests
return stacks;
}

Expand Down
Loading