fix(toolkit-lib): hotswap template cache ignores target environment - #1875
Open
Adityaj0 wants to merge 2 commits into
Open
fix(toolkit-lib): hotswap template cache ignores target environment#1875Adityaj0 wants to merge 2 commits into
Adityaj0 wants to merge 2 commits into
Conversation
The on-disk hotswap cache (.hotswap-cache/<stack>.json) was keyed only by assembly directory + stack name. Its payload stores the deployed template and physical resource names, which are meaningful only for the specific account/region they were captured against. If a stack's environment is resolved at deploy time (e.g. from CLI credentials) rather than fixed in the template, running `cdk watch` against one account, then switching credentials/profile and running `cdk watch` again for the same stack name against a different account/region, would silently reuse the first environment's cached deployedRootTemplate and physical resource names. This can produce a wrong hotswap diff, or hotswap operations issued against physical resource names/SDK clients for the wrong account. The only existing invalidation path (a full CloudFormation deploy) does not fire between two hotswap-only sessions, so the stale cache persists indefinitely. Fold the resolved environment (account/region) into the cache key so a cache entry can only ever be read back for the environment it was written for. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adityaj0
requested a deployment
to
integ-approval
August 22, 2026 06:30 — with
GitHub Actions
Waiting
aws-cdk-automation
enabled auto-merge
August 22, 2026 06:30
…environment parameter CI caught a tsc build failure: this test still called writeHotswapTemplateCache with 4 arguments after the prior commit added a required environment parameter. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
auto-merge was automatically disabled
August 23, 2026 03:10
Head branch was pushed to by a user without write access
Adityaj0
requested a deployment
to
integ-approval
August 23, 2026 03:10 — with
GitHub Actions
Waiting
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1875 +/- ##
==========================================
+ Coverage 91.10% 91.13% +0.03%
==========================================
Files 80 80
Lines 12205 12205
Branches 1742 1744 +2
==========================================
+ Hits 11119 11123 +4
+ Misses 1050 1046 -4
Partials 36 36
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Closes #1874
Reason for this change
The on-disk hotswap cache (
.hotswap-cache/<stackName>.json, inhotswap-template-cache.ts) was keyed only by cloud assembly directory + stack name. Its payload — the last-deployed root template and, for nested stacks, physical resource names — is only valid for the specific account/region it was captured against, but nothing in the key or the data records which environment produced it.hotswapDeployment()resolves the target environment and then unconditionally trusts the cache for that stack name, regardless of whether it was written for a different account/region. The only invalidation path is a full CloudFormation deployment for the same stack name — switching target environments between hotswap-only sessions (e.g.cdk watchruns against different accounts viaAWS_PROFILE, common for environments resolved from CLI credentials at deploy time) never invalidates it.Concretely: hotswap against Account A, then without an intervening full deploy, switch credentials and hotswap the same stack name against Account B. The stale Account-A cache is returned, so the diff is computed against the wrong deployed state and any hotswap operations that rely on cached physical resource names can target the wrong account's resources.
Description of changes
Fold the resolved environment (
account/region) into the hotswap cache key (cachePath), threading it throughreadHotswapTemplateCache,writeHotswapTemplateCache, andinvalidateHotswapTemplateCache, and updating all three call sites (hotswap-deployments.ts,deploy-stack.ts) to pass the stack's resolved environment. A cache entry can now only ever be read back for the environment it was written for; a different environment for the same stack name/assembly directory is treated as a cache miss (falls back to loading the template from CloudFormation), and invalidating one environment's cache no longer touches another's.Description of how you validated changes
Added a regression test (
cache from one environment is never returned for a different environment) tohotswap-template-cache.test.tsthat writes a cache entry for environment A and asserts reading it back for environment B returnsundefined, reading it back for environment A still returns the cached state, and invalidating environment B's cache doesn't remove environment A's. Verified this test fails against the pre-fix code (the stale cache is returned across environments) and passes after the fix. Updated the existing tests in the same file to pass an environment argument. Ran the fullhotswapanddeploymentsunit test suites (494 tests) — all pass. Confirmedtsc --noEmithas no new type errors from this change.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license