Skip to content

Conversation

@go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Dec 10, 2025

Fixes aws/aws-cdk#32836, aws/aws-cdk#32545, aws/aws-cdk#27179, aws/aws-cdk#22240

Reason for this change

This PR implements the feature that warns users when non-existent stacks are specified in cdk destroy.

  • It does not display the message Are you sure you want to delete: if there is no matching stack.
  • Even if the stack does not exist, cdk destroy will not fail, it will just print a warning.

For examples (that have Stacka, StackA, StackX):

destroy2
destroy3
destroy4

Difference from previous PR

The previous PR was reverted in aws/aws-cdk#32839 due to a regression with only nested stage stacks. So this version addresses that regression with comprehensive tests.

Description of changes

The original implementation added warnings for non-existent stacks in cdk destroy, but it failed when applications had only nested stage stacks (no top-level stacks). This happened because the code used allTopLevel: true, which only searched for stacks directly under the App, ignoring stacks within nested Stages.

Fixed the regression by changing suggestStacks method to use DefaultSelection.AllStacks instead of allTopLevel: true, ensuring the warning feature works for all stack configurations (top-level only, nested only, or both). Added regression tests to verify the fix for the nested stage scenario.

  private async suggestStacks(props: {
    selector: StackSelector;
    stacks: StackCollection;
    exclusively?: boolean;
  }) {
    const assembly = await this.assembly();
    const selectorWithoutPatterns: StackSelector = {
-      ...props.selector,
-      allTopLevel: true,
      patterns: [],
    };
    const stacksWithoutPatterns = await assembly.selectStacks(selectorWithoutPatterns, {
      extend: props.exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
-      defaultBehavior: DefaultSelection.OnlySingle,
+      defaultBehavior: DefaultSelection.AllStacks,
    });

Additional Information

When running cdk destroy --all or cdk deploy --all against a configuration with no top-level stacks (nested stages only), the following error occurs.

However, this behavior existed before this PR and is unrelated to the changes made here. Since it is outside the scope of this PR, no fix has been implemented for this behavior.

const app = new cdk.App();

new MyStage(app, 'MyStage', {
  env: {
    region: 'us-east-1',
    account: process.env.AWS_ACCOUNT_ID,
  },
});
> cdk destroy --all
...
No stack found in the main cloud assembly. Use "list" to print manifest

> cdk deploy --all
...
No stack found in the main cloud assembly. Use "list" to print manifest

P.S.

I have submitted a PR about this behavior, but I will close the PR if it meets the specifications. Otherwise, the above implementation with defaultBehavior: DefaultSelection.AllStacks instead of allTopLevel: true would not be needed.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

auto-merge was automatically disabled December 10, 2025 12:18

Head branch was pushed to by a user without write access

@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.72%. Comparing base (2a6f8d3) to head (c4203ad).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #984      +/-   ##
==========================================
+ Coverage   87.38%   87.72%   +0.34%     
==========================================
  Files          71       72       +1     
  Lines       10010    10084      +74     
  Branches     1311     1341      +30     
==========================================
+ Hits         8747     8846      +99     
+ Misses       1240     1214      -26     
- Partials       23       24       +1     
Flag Coverage Δ
suite.unit 87.72% <100.00%> (+0.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

integ

integ

integ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cdk destroy: No stack found in the main cloud assembly

2 participants