Skip to content

fix(toolkit-lib)!: fromAssemblyBuilder mutates globally shared process.env #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented May 23, 2025

The Toolkit communicates with the CDK app passing environment variables (containing context, and others).

If the CDK app is executed as a subprocess it is trivial to set those environment variables when executing the subprocess; but for an in-memory synthesis as allowed by fromAssemblyBuilder mutating process.env is not the correct approach: it is clobbering global state, and multiple async assembly builders might fight with each other over the single shared global object.

In fromAssemblyBuilder(), we now pass an additional env prop containing the environment variables we didn't apply. There were already parametes for outdir and context; those must now be passed to an App instantiated inside the builder (whereas previously the App would pick up on the globally mutated process.env changes).

The rest of this PR is reorganizing the code to make the code that touches the environment return values instead of directly applying them.

  • Remove withEnv() and withContext().
  • Move logging of the final context object out to the call site instead of the builder function.

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

…uilder`

The Toolkit communicates with the CDK app passing environment variables
(containing context, and others).

If the CDK app is executed as a subprocess it is trivial to set those
environment variables when executing the subprocess; but for an
in-memory synthesis as allowed by `fromAssemblyBuilder` mutating
`process.env` is not the correct approach: it is clobbering global
state, and multiple async assembly builders might fight with
each other over the single shared global object.

In `fromAssemblyBuilder()`, we now pass an additional `env` prop
containing the environment variables we didn't apply. There were
already parametes for `outdir` and `context`; those *must* now be
passed to an `App` instantiated inside the builder (whereas previously
the `App` would pick up on the globally mutated `process.env`
changes).

The rest of this PR is reorganizing the code to make the code
that touches the environment return values instead of directly
applying them.
@github-actions github-actions bot added the p2 label May 23, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team May 23, 2025 13:11
@rix0rrr rix0rrr changed the title fix(toolkit-lib)!: don't mutate environment when using fromAssemblyBuilder fix(toolkit-lib)!: fromAssemblyBuilder mutates globally shared process.env May 23, 2025
@codecov-commenter
Copy link

codecov-commenter commented May 23, 2025

Codecov Report

Attention: Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.20%. Comparing base (2b92bd5) to head (9edc071).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cxapp/exec.ts 96.42% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #528      +/-   ##
==========================================
+ Coverage   78.92%   79.20%   +0.27%     
==========================================
  Files          46       46              
  Lines        6980     6957      -23     
  Branches      777      777              
==========================================
+ Hits         5509     5510       +1     
+ Misses       1452     1427      -25     
- Partials       19       20       +1     
Flag Coverage Δ
suite.unit 79.20% <96.42%> (+0.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rix0rrr rix0rrr temporarily deployed to integ-approval May 23, 2025 14:04 — with GitHub Actions Inactive
@rix0rrr
Copy link
Contributor Author

rix0rrr commented May 23, 2025

TODO: this pessimizes the default API for an edge case.

Retain all the code changes, but make it an option; leave the legacy behavior by default. This will require capturing the OUTDIR and CONTEXT envvars in the env bundle to fromAssemblyBuilder() as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants