Skip to content

Conversation

@rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Nov 13, 2025

The CDK framework creates a temporary directory in /tmp for every unit test, and never cleans those up.

In this PR, register all temporary assembly directories created by doing new Stack() or new App() without an outdir, and delete them when the Node process exits.

This will only affect unit tests: if the outdir property is set explicitly, or the CDK App is being synthesized by the CLI (and $CDK_OUTDIR is set), the assembly directory will not be cleaned. For users: if you set outdir you are reponsible for cleaning up the directory. If you don't set outdir, it will be automatically removed at some point.

This will benefit both aws-cdk development itself, as well as users writing unit tests against CDK.

Relates to #802.

Also in this PR: cleanup of other temporary directories we create during tests. These will only benefit development on aws-cdk itself.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@rix0rrr rix0rrr requested a review from a team November 13, 2025 15:18
@aws-cdk-automation aws-cdk-automation requested a review from a team November 13, 2025 15:18
@rix0rrr rix0rrr requested review from mrgrain and removed request for a team November 13, 2025 15:18
@github-actions github-actions bot added the p2 label Nov 13, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 13, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@rix0rrr rix0rrr force-pushed the huijbers/cleanup-temp branch from e1ee38c to 177173e Compare November 13, 2025 15:34
The CDK framework creates a temporary directory in `/tmp` for every unit
test, and never cleans those up.

In this PR, register all temporary assembly directories created by doing
`new Stack()` or `new App()` without an `outdir`, and delete them when
the Node process exits.

This will only affect unit tests: if the `outdir` property is set
explicitly, or the CDK App is being synthesized by the CLI (and
`$CDK_OUTDIR` is set), the assembly directory will not be cleaned.
@rix0rrr rix0rrr force-pushed the huijbers/cleanup-temp branch from 177173e to 408e4a8 Compare November 13, 2025 15:38
@rix0rrr rix0rrr added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Nov 13, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review November 13, 2025 15:48

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 14, 2025
Comment on lines +499 to +505
// On process exit, delete all temporary assembly directories
const TEMPORARY_ASSEMBLY_DIRS: string[] = [];
process.on('exit', () => {
for (const dir of TEMPORARY_ASSEMBLY_DIRS) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: This scares me a little. But I guess it's safe because the only way that an outside caller of the app would know about the outdir, is by providing it explicitly to the app. So our rule is now:

  • Need the CloudAssembly? Provide an outdir.
  • Otherwise, we synth but the result is lost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my thought. If you didn't provide a path, are you going to dig the assembly directory out of /tmp, amongst the 3000 other assemblies there? I don't think so.

@mrgrain mrgrain added the pr/do-not-merge This PR should not be merged at this time. label Nov 14, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 14, 2025
@rix0rrr rix0rrr changed the title fix: automatically clean up unit test assemblies fix(aws-cdk-lib): temporary Cloud Assemblies are not cleaned up Nov 14, 2025
@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Nov 14, 2025
@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 1ace1ef into main Nov 14, 2025
18 of 19 checks passed
@mergify mergify bot deleted the huijbers/cleanup-temp branch November 14, 2025 10:02
@github-actions
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants