terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Using `JSON.stringify()` falsely triggers `scalar list token string in a scalar string context` error

Open ansgarm opened this issue 3 years ago • 1 comments

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

cdktf & Language Versions

0.10.4

Affected Resource(s)

Debug Output

Expected Behavior

Should work if possible or print a more useful warning (e.g. recommend Fn.jsonencode()).

Actual Behavior

Fails with

Error: Found an encoded list token string in a scalar string context. Use 'Fn.element(list, 0)' (not 'list[0]') to extract elements from token lists

Steps to Reproduce

const arr = ["a", "b", "c"];
const jsonString = JSON.stringify({
  "test:key": Fn.concat([arr, arr]),
});
new TerraformLocal(this, "fails", jsonString);

Important Factoids

Using Fn.jsonencode() works as a workaround.

References

ansgarm avatar May 12 '22 12:05 ansgarm

I think it should print a better error message, but it should not work. The token could have any value and the serialization to JSON happens with the token as a string, so there could potentially be extra things rendered at execution time that don't match what a user would expect. But yeah we should try to make the error message clearer

DanielMSchmidt avatar May 12 '22 13:05 DanielMSchmidt