Skip to content

feat(cli): cannot pass objects and numbers as context arguments #20068

Merged
mergify[bot] merged 4 commits intoaws:mainfrom
corymhall:cli-future-flag-context
Jul 20, 2022
Merged

feat(cli): cannot pass objects and numbers as context arguments #20068
mergify[bot] merged 4 commits intoaws:mainfrom
corymhall:cli-future-flag-context

Conversation

@corymhall
Copy link
Copy Markdown
Contributor

@corymhall corymhall commented Apr 25, 2022

All context arguments passed to the cli are parsed as strings.

cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=false
cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=0

The values are interpreted as strings so the above two values get
interpreted as 'false' and '0' which are truthy (and not what we want).

This PR adds a function to parse context arguments and try to covert values
into their correct type.


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

All future flag context arguments are either `true` or `false` (enabled
or disabled). When passing future flags as command line arguments, i.e.

```
cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=false
cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=0
```

The values are interpreted as strings so the above two values get
interpreted as 'false' and '0' which are truthy (and not what we want).

This PR adds a function to parse context arguments and covert any future
flags to boolean values. 'false' and '0' will be evaluated as `false`.

This functionality could be beneficial to more than just future flags,
but with future flags we know that the value must be a boolean.
@gitpod-io
Copy link
Copy Markdown

gitpod-io bot commented Apr 25, 2022

@github-actions github-actions bot added the p2 label Apr 25, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team April 25, 2022 12:51
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Apr 25, 2022
Copy link
Copy Markdown
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Instead of special-casing feature flags, I was hoping to generalize the context parsing for all values in the CLI. Is that possible?

@corymhall corymhall changed the title feat(cli): parse future flag context arguments as boolean feat(cli): parse cli context arguments Apr 25, 2022
@corymhall
Copy link
Copy Markdown
Contributor Author

Instead of special-casing feature flags, I was hoping to generalize the context parsing for all values in the CLI. Is that possible?

Ok i've updated it to try and parse all values. Let me know what you think.

@corymhall corymhall requested a review from rix0rrr April 25, 2022 14:22
@corymhall corymhall added the pr-linter/exempt-readme The PR linter will not require README changes label Apr 25, 2022
@TheRealAmazonKendra TheRealAmazonKendra changed the base branch from v1-main to main July 13, 2022 00:07
@TheRealAmazonKendra TheRealAmazonKendra added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Jul 14, 2022
@rix0rrr rix0rrr changed the title feat(cli): parse cli context arguments feat(cli): cannot pass objects and numbers as context arguments Jul 18, 2022
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jul 18, 2022

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
Copy Markdown
Contributor

mergify bot commented Jul 20, 2022

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).

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: c8ee2eb
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit ec2d68a into aws:main Jul 20, 2022
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jul 20, 2022

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).

comcalvi pushed a commit to comcalvi/aws-cdk that referenced this pull request Jul 25, 2022
…20068)

All context arguments passed to the cli are parsed as strings.

```
cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=false
cdk deploy --context @aws-cdk/core:newStyleStackSynthesis=0
```

The values are interpreted as strings so the above two values get
interpreted as 'false' and '0' which are truthy (and not what we want).

This PR adds a function to parse context arguments and try to covert values
into their correct type.

----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
iliapolo added a commit that referenced this pull request Jul 29, 2022
iliapolo added a commit that referenced this pull request Jul 29, 2022
@oceanofmaya
Copy link
Copy Markdown

oceanofmaya commented Jul 30, 2022

Interestingly this fix broke things for me where I was expecting a string and evaluating to a boolean. With this change --context IsSomething=true evaluated to false where it previously evaluated to true as I was doing this in my code (stack.node.tryGetContext('IsSomething') as string) === 'true' .

I was able to resolve it on my end by doing one of the below (treat it as boolean or as a literal string)

  1. stack.node.tryGetContext('IsSomething') as boolean
  2. `${stack.node.tryGetContext('IsSomething') as string}` === 'true'

@nicolai0
Copy link
Copy Markdown

nicolai0 commented Aug 1, 2022

Isn't everything after the JSON.parse(...) line redundant? JSON.parse will already resolve non-object values:

> typeof JSON.parse('1')
'number'
> typeof JSON.parse('1.2')
'number'
> typeof JSON.parse('true')
'boolean'
> typeof JSON.parse('false')
'boolean'

@oceanofmaya
Copy link
Copy Markdown

I type cast in my code as stack.node.tryGetContext returns any type and my project settings enforce linting rules that disallow any type. The crux of the issue this fix caused on my end was 'true' === 'true' became true === 'true' as it now parsed to a boolean, so everything that evaluated to true when it was a string now evaluated to false as it is now a boolean.

I updated my code to accommodate this change, but was surprised when it occurred as suddenly all of my stacks changed when pipelines in my org ran post 7/28.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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 pr-linter/exempt-readme The PR linter will not require README changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants