hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.89k stars 456 forks source link

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

Open ansgarm opened 2 years ago

ansgarm commented 2 years ago

Community Note

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

DanielMSchmidt commented 2 years ago

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