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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ class LambdaStack extends cdk.Stack {
constructor(parent, id, props) {
// sometimes we need to specify the custom bootstrap bucket to use
// see the 'upgrade legacy bootstrap stack' test
const synthesizer = parent.node.tryGetContext('legacySynth') === 'true' ?
const useLegacy = [true, 'true'].includes(parent.node.tryGetContext('legacySynth'));
const synthesizer = useLegacy ?
new LegacyStackSynthesizer({
fileAssetsBucketName: parent.node.tryGetContext('bootstrapBucket'),
})
Expand All @@ -477,7 +478,8 @@ class LambdaStack extends cdk.Stack {

class DriftableStack extends cdk.Stack {
constructor(parent, id, props) {
const synthesizer = parent.node.tryGetContext('legacySynth') === 'true' ?
const useLegacy = [true, 'true'].includes(parent.node.tryGetContext('legacySynth'));
const synthesizer = useLegacy ?
new LegacyStackSynthesizer({
fileAssetsBucketName: parent.node.tryGetContext('bootstrapBucket'),
})
Expand Down
8 changes: 7 additions & 1 deletion packages/aws-cdk/lib/cli/user-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,13 @@ function parseStringContextListToObject(argv: Arguments): any {
`User-provided context cannot use keys prefixed with 'aws:', but ${parts[0]} was provided.`,
);
}
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

} catch {
debug('Non-JSON context value for %s, leaving as string: %s', parts[0], parts[1]);
}
Comment on lines +345 to +349
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

context[parts[0]] = parsedValue;
} else {
warning(
'Context argument is not an assignment (key=value): %s',
Expand Down
20 changes: 20 additions & 0 deletions packages/aws-cdk/test/cli/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ test('providing a build arg', () => {
// THEN
expect(settings.get(['build'])).toEqual('mvn package');
});

test('can parse boolean context from command line arguments', () => {
// GIVEN
const settings1 = commandLineArgumentsToSettings({ context: ['foo=true'], _: [Command.DEPLOY] });
const settings2 = commandLineArgumentsToSettings({ context: ['foo=false'], _: [Command.DEPLOY] });

// THEN
expect(settings1.get(['context']).foo).toEqual( true );
expect(settings2.get(['context']).foo).toEqual( false );
});

test('can parse number context from command line arguments', () => {
// GIVEN
const settings1 = commandLineArgumentsToSettings({ context: ['foo=5'], _: [Command.DEPLOY] });
const settings2 = commandLineArgumentsToSettings({ context: ['foo=3.14'], _: [Command.DEPLOY] });

// THEN
expect(settings1.get(['context']).foo).toEqual( 5 );
expect(settings2.get(['context']).foo).toEqual( 3.14 );
});
Loading