fix(cli): cdk flags --set leaks synthesized cloud assembly directories - #1833
Merged
Merged
Conversation
aws-cdk-automation
enabled auto-merge
August 14, 2026 21:18
Adityaj0
requested a deployment
to
integ-approval
August 14, 2026 21:18 — with
GitHub Actions
Waiting
prototypeChanges() created two mkdtemp-based synthesis output directories
under os.tmpdir() (prefixed cdk-original-/cdk-temp-) but never stored their
paths anywhere. cleanupTempDirectories(), called after the user accepts or
cancels, instead removed `${cwd}/original` and `${cwd}/temp` - paths that
were never created by this code path - so it was a no-op and every `cdk
flags --set` invocation left two real synthesized-cloud-assembly
directories behind in the OS temp dir indefinitely.
Stores both paths as instance fields (originalTempDir/modifiedTempDir),
matching the existing baselineTempDir pattern already used elsewhere in
this class for the safety-check path, and has cleanupTempDirectories()
remove the actual paths. Also cleans up originalTempDir on the early
"nothing to update" return in prototypeChanges(), which previously
skipped handleUserResponse() (and therefore cleanup) entirely.
Fixes aws#1832
mrgrain
force-pushed
the
fix/flags-temp-dir-cleanup
branch
from
August 19, 2026 11:04
b28b174 to
e97a627
Compare
mrgrain
approved these changes
Aug 19, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1833 +/- ##
=======================================
Coverage 90.92% 90.93%
=======================================
Files 80 80
Lines 12226 12237 +11
Branches 1750 1752 +2
=======================================
+ Hits 11117 11128 +11
Misses 1073 1073
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.
fixes #1832
Reason for this change
FlagOperations.prototypeChanges()(packages/aws-cdk/lib/commands/flags/operations.ts) creates two synthesis output directories viafs.mkdtempSync(path.join(os.tmpdir(), 'cdk-original-'))and'cdk-temp-', but never stores the returned paths anywhere.cleanupTempDirectories(), called after the user accepts or declines the confirmation prompt, instead removespath.join(process.cwd(), 'original')andpath.join(process.cwd(), 'temp')— paths that were never created by this code path at all. It is effectively a no-op, and everycdk flags --set(or--recommended/--default/--all) invocation leaves two full synthesized cloud-assembly directories behind in the OS temp directory, indefinitely.Description of changes
originalTempDir/modifiedTempDirinstance fields, following the same pattern this class already uses forbaselineTempDiron thesetSafeFlagscode path.prototypeChanges()now stores eachmkdtemppath onthisinstead of discarding it.cleanupTempDirectories()now removes the actual stored paths (and resets the fields), instead of the unrelated${cwd}/original/${cwd}/temppaths.cleanupTempDirectories()on the early "nothing to update" return insideprototypeChanges()(e.g. "flag is already set to the specified value") — that path returnsfalsebeforehandleUserResponse()(the only other place cleanup was wired up) is ever called, sooriginalTempDirwould otherwise still leak even after fixing the main paths.Description of how you validated changes
Added two regression tests to
test/commands/flag-operations.test.ts('cleans up the real temp directories created during prototyping when the user cancels'/'...when the user accepts') that snapshotos.tmpdir()before and after a fullcdk flags --setinvocation and assert nocdk-original-*/cdk-temp-*entries remain.fs.mkdtempSyncis not mocked in this suite, so these tests exercise real filesystem directory creation/cleanup, not just the mocked toolkit calls.Verified both new tests fail against the pre-fix code (real leaked directories detected in
os.tmpdir()) and pass with the fix. Ran the fullflag-operations.test.tssuite 3x with jest's randomized test order (58 tests each run) plus a scan ofos.tmpdir()after each run — zero leaked entries across all runs.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license