Skip to content

fix(cli): retain type of context values and not convert them to string #595

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

Merged
merged 7 commits into from
Jun 13, 2025

Conversation

kumsmrit
Copy link
Contributor

@kumsmrit kumsmrit commented Jun 11, 2025

Fixes #30583

CLI’s context-passing mechanism relied on serialising every entry to a "KEY=VALUE" string, leading CLI to only allows passing string context, so even JSON booleans (true/false) and numbers became plain strings.
This resulted in losing the typed context, like boolean feature-flags are always being passed as string "true"/"false". This incorrectly passed feature flags caused certain integration tests snapshot in the aws-cdk library to be updated with incorrect values causing the integration tests to fail.

This fix retains the type of context values when passed via CLI, by correctly JSON parsing the context while building user configuration. Values that aren’t valid JSON will fall back to plain strings, so existing workflows should continue working as is. This will restore correct typing for feature flags without disrupting existing tests.


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

@github-actions github-actions bot added the p2 label Jun 11, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team June 11, 2025 15:03
@codecov-commenter
Copy link

codecov-commenter commented Jun 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.02%. Comparing base (e272c5e) to head (8ea3b40).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #595      +/-   ##
==========================================
+ Coverage   79.01%   79.02%   +0.01%     
==========================================
  Files          46       46              
  Lines        7085     7091       +6     
  Branches      791      791              
==========================================
+ Hits         5598     5604       +6     
  Misses       1468     1468              
  Partials       19       19              
Flag Coverage Δ
suite.unit 79.02% <100.00%> (+0.01%) ⬆️

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.

@kumsmrit kumsmrit marked this pull request as ready for review June 12, 2025 13:20
@mrgrain mrgrain changed the title refactor(cli): retain type of context values and not convert them to string fix(cli): retain type of context values and not convert them to string Jun 12, 2025
@mrgrain mrgrain temporarily deployed to integ-approval June 12, 2025 16:51 — with GitHub Actions Inactive
context[parts[0]] = parts[1];
let parsedValue: any = parts[1];
try {
parsedValue = JSON.parse(parts[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

note to myself, we might want to limit this to booleans and numbers

Comment on lines +345 to +349
try {
parsedValue = JSON.parse(parts[1]);
} catch {
debug('Non-JSON context value for %s, leaving as string: %s', parts[0], parts[1]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to be more clever here. JSON.parse(parts[1]) will fail if the value is a regular string like test. This will print debug logs for perfectly fine values:

cdk deploy --context foobar=bar

@mrgrain mrgrain enabled auto-merge June 13, 2025 18:30
@mrgrain mrgrain added this pull request to the merge queue Jun 13, 2025
@mrgrain
Copy link
Contributor

mrgrain commented Jun 13, 2025

Discussed with the team and this is a great fix!

@mrgrain mrgrain removed this pull request from the merge queue due to a manual request Jun 13, 2025
Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

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

Too soon. Team is still discussing some open questions. But the general direction is okay.

@mrgrain mrgrain added this pull request to the merge queue Jun 13, 2025
Merged via the queue into aws:main with commit 6250b39 Jun 13, 2025
27 checks passed
@yhu02
Copy link

yhu02 commented Jun 18, 2025

I am getting errors when parsing now

image image

@yhu02
Copy link

yhu02 commented Jun 18, 2025

@mrgrain @kumsmrit

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.

(integ-tests-alpha,custom-resources): snapshots are always outdated
4 participants