fix(cli): deploy and destroy with --all option fail on apps with no top-level stacks - #985
fix(cli): deploy and destroy with --all option fail on apps with no top-level stacks#985go-to-k wants to merge 10 commits into
--all option fail on apps with no top-level stacks#985Conversation
…top-level stacks fix
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #985 +/- ##
==========================================
+ Coverage 87.72% 87.98% +0.26%
==========================================
Files 72 72
Lines 10077 10082 +5
Branches 1327 1335 +8
==========================================
+ Hits 8840 8871 +31
+ Misses 1212 1186 -26
Partials 25 25
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Head branch was pushed to by a user without write access
807c8bc to
9550003
Compare
…l-on-stage-only
dd751ed to
989cb78
Compare
--all option fails on apps with no top-level stacks--all option fail on apps with no top-level stacks
There was a problem hiding this comment.
I don't think this behavior is desirable or intuitive.
The constraints are:
--allwas introduced before Stages existed. When Stages were introduced, it would continue to select stacks only in the top-level assembly.- Stages are intended to be deployed using a pipeline in the same application. For that use case, you don't really want to select them using
cdk deploy. - I think having a "sometimes
cdk deploydeploys stacks in stages and sometimes not" kind of behavior will be extremely confusing.
I understand this might be desirable for your specific use case, but is your use case represenative?
As written on your issue,
$ cdk deploy '**'
Should solve your particular problem exactly, and it's not that much more to type than --all.
Don't get me wrong: I'm not against making changes here altogether. But I think it should be a co-operation between the CDK app and the CLI. Perhaps a Stack gets a new option that interplays with a new selection mechanism:
new Stack(this, 'SomeStack', {
deployUsingCli: WITH_ALL_FLAG | ONLY_IF_EXPLICITLY_SELECTED | NEVER,
});And then we set appropriate defaults to retain existing behavior.
I'm also not opposed to changing the documentation to better reflect actual behavior.
But as-is, I think changing this behavior in the CLI only will get too confusing.
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`): <img width="355" height="56" alt="destroy2" src="https://github.com/user-attachments/assets/0ac39df5-7ec7-46a4-8572-f61bb7f26ec6" /> <br> <img width="414" height="85" alt="destroy3" src="https://github.com/user-attachments/assets/9c897485-71a9-4c93-9d00-4ed5a5f8df15" /> <br> <img width="315" height="56" alt="destroy4" src="https://github.com/user-attachments/assets/d208e590-f3c8-4228-87df-0797202a6224" /> ## Difference from previous PR The [previous PR](aws/aws-cdk#32636) 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. ```diff 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. FYI: I have submitted an [issue](aws#1003) and a [PR](aws#985) about this behavior. ```ts const app = new cdk.App(); new MyStage(app, 'MyStage'); // This has CdkSampleStack ``` ``` > cdk deploy Since this app includes more than a single stack, specify which stacks to use (wildcards are supported) or specify `--all` Stacks: MyStage/CdkSampleStack > cdk deploy --all ... No stack found in the main cloud assembly. Use "list" to print manifest > cdk destroy --all ... No stack found in the main cloud assembly. Use "list" to print manifest ``` --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Momo Kornher <kornherm@amazon.co.uk>
Fixes: #1003
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license