fix(toolkit-lib): hotswap template cache ignores target environment - #1875
fix(toolkit-lib): hotswap template cache ignores target environment#1875Adityaj0 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>
…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>
Head branch was pushed to by a user without write access
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:
|
|
@Adityaj0 Can you update the integration tests for hotswap? This is the file you will want to update: https://github.com/aws/aws-cdk-cli/blob/main/packages/%40aws-cdk-testing/cli-integ/tests/cli-integ-tests/hotswap/cdk-hotswap-deployment-caches-template.integtest.ts |
ShadowCat567
left a comment
There was a problem hiding this comment.
@Adityaj0 can you update the hotswap cache integration tests? See my previous comment for the file you should be updating
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