fix(toolkit-lib): downstream stack selection uses wrong key for nested stacks - #1835
Open
Adityaj0 wants to merge 1 commit into
Open
fix(toolkit-lib): downstream stack selection uses wrong key for nested stacks#1835Adityaj0 wants to merge 1 commit into
Adityaj0 wants to merge 1 commit into
Conversation
…d stacks includeDownstreamStacks looked up dependencies with `selectedStacks.has(dep.id)`, but selectedStacks/allStacks are keyed by hierarchicalId (manifest.displayName ?? id) everywhere else in this file. For a stack whose displayName differs from its raw id - i.e. stacks inside nested assemblies/stages - this lookup never matches, so a stack that depends on a selected stack is silently dropped from a DOWNSTREAM-expanded selection (the default behavior for `cdk deploy`/`cdk destroy` without `--exclusively`). The symmetric includeUpstreamStacks already does this correctly via `x.manifest.displayName ?? x.id`. This switches includeDownstreamStacks to use the existing `dep.hierarchicalId` getter instead, matching that pattern. Fixes aws#1834
Adityaj0
requested a deployment
to
integ-approval
August 14, 2026 21:30 — with
GitHub Actions
Waiting
aws-cdk-automation
enabled auto-merge
August 14, 2026 21:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #1834
Reason for this change
`includeDownstreamStacks` (`packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/stack-assembly.ts`) computes the transitive closure of stacks that depend on the selected set — this is what powers `expand: ExpandStackSelection.DOWNSTREAM`, the default expansion behavior for `cdk deploy`/`cdk destroy` when `--exclusively` is not passed.
`selectedStacks`/`allStacks` are both keyed by `hierarchicalId` (`manifest.displayName ?? id`) — see `indexByHierarchicalId` and `extendStacks` just above, which build these maps via `stack.hierarchicalId`. But the lookup here checks the dependency's raw `.id`, not its `.hierarchicalId`. For a stack whose `displayName` differs from its `id` (stacks inside nested assemblies/stages, e.g. CDK Pipelines), this lookup never matches, so a stack that genuinely depends on a selected stack is silently dropped from the expanded selection.
This was masked for ordinary flat apps, where `displayName` is unset and `id === hierarchicalId`.
The symmetric `includeUpstreamStacks` a few lines below already does this correctly:
Description of changes
Switches `dep.id` to `dep.hierarchicalId` (the existing getter on `CloudArtifact`, equivalent to `includeUpstreamStacks`'s inline `manifest.displayName ?? id` expression) in `includeDownstreamStacks`.
Description of how you validated changes
Added `test/api/cloud-assembly/stack-selection-expand.test.ts` with two regression tests, using `Toolkit.list()` end-to-end through a `TestCloudAssemblySource` fixture: a dependency stack with an explicit `displayName` distinct from its `stackName`/id, and a second stack that depends on it.
Ran the full `test/api/cloud-assembly/`, `test/actions/list.test.ts`, and the rest of `test/actions/` (deploy, diff, synth, drift, rollback, hotswap) — all pass except `bootstrap.test.ts`, which fails identically with or without this change due to a missing `bootstrap-template.yaml` asset that's only copied by a full `yarn build`, not a bare `tsc --build` compile — unrelated to this change.
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license