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

core: "You're using escape sequences (${...}) with CDKTF Built-in functions" - false positive?

Open elruwen opened this issue 11 months ago • 2 comments

Expected Behavior

No error message. I am not using escape sequences anywhere explicitly. I believe this is a false positive.

Actual Behavior

Given the following example application:

import {Construct} from "constructs";
import {App, Fn, TerraformStack, TerraformVariable} from "cdktf";
import {File} from "@cdktf/provider-local/lib/file";
import {LocalProvider} from "@cdktf/provider-local/lib/provider";

class MyStack extends TerraformStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        new LocalProvider(this, "p")

        const myvar = new TerraformVariable(this, "prefix", {
            type: "string",
            default: "rwn-",
            description: "prefix",
        })

        const resources = [
            {
                foo: myvar.stringValue + "bar",
            },
            {
                bar: myvar.stringValue + "foo"
            }
        ]

        new File(this, "ruwen", {
            filename: `ruwen.yaml`,
            content: Fn.yamlencode(resources)
        })
    }
}

const app = new App();
new MyStack(app, "cdktf-playground");
app.synth();

This generates successfully the following yaml file without warnings:

- "foo": "rwn-bar"
- "bar": "rwn-foo"

But if I use the result from the Fn.yamlencode in another function, I am getting an error.

import {Construct} from "constructs";
import {App, Fn, TerraformStack, TerraformVariable} from "cdktf";
import {File} from "@cdktf/provider-local/lib/file";
import {LocalProvider} from "@cdktf/provider-local/lib/provider";

class MyStack extends TerraformStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        new LocalProvider(this, "p")

        const myvar = new TerraformVariable(this, "prefix", {
            type: "string",
            default: "rwn-",
            description: "prefix",
        })

        const resources = [
            {
                foo: myvar.stringValue + "bar",
            },
            {
                bar: myvar.stringValue + "foo"
            }
        ]

        new File(this, "ruwen", {
            filename: `ruwen.yaml`,
            content: Fn.join("", [Fn.yamlencode(resources)])
        })
    }
}

const app = new App();
new MyStack(app, "cdktf-playground");
app.synth();

[2024-03-22T11:35:57.800] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.

[2024-03-22T11:35:57.800] [ERROR] default - yamlencode([{"foo" = "${${TfToken[TOKEN.0]}}bar"}, {"bar" = "${${TfToken[TOKEN.1]}}foo"}])
You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.
yamlencode([{"foo" = "${${TfToken[TOKEN.0]}}bar"}, {"bar" = "${${TfToken[TOKEN.1]}}foo"}])

[2024-03-22T11:35:57.801] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.
yamlencode([{"foo" = "${${TfToken[TOKEN.0]}}bar"}, {"bar" = "${${TfToken[TOKEN.1]}}foo"}])

[2024-03-22T11:35:57.803] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.
yamlencode([{"foo" = "${${TfToken[TOKEN.0]}}bar"}, {"bar" = "${${TfToken[TOKEN.1]}}foo"}])

[2024-03-22T11:35:57.803] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.
yamlencode([{"foo" = "${${TfToken[TOKEN.0]}}bar"}, {"bar" = "${${TfToken[TOKEN.1]}}foo"}])

[2024-03-22T11:35:57.803] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.

The file still gets generated correctly.

If I do something wrong, then the error message should point to the spot where I use Fn.join. Because the yamlencode without the join works. If it is a false positive, then the warning shouldn't be there.

Steps to Reproduce

See above

Versions

cdktf debug language: typescript cdktf-cli: 0.20.4 node: v18.15.0 cdktf: 0.20.5 constructs: 10.3.0 jsii: null terraform: 1.7.5 arch: x64 os: linux 6.1.0-17-amd64 providers @cdktf/provider-local (PREBUILT) terraform provider version: 2.5.1 prebuilt provider version: 10.1.0 cdktf version: ^0.20.0

Providers

┌───────────────┬──────────────────┬─────────┬────────────┬───────────────────────┬─────────────────┐ │ Provider Name │ Provider Version │ CDKTF │ Constraint │ Package Name │ Package Version │ ├───────────────┼──────────────────┼─────────┼────────────┼───────────────────────┼─────────────────┤ │ local │ 2.5.1 │ ^0.20.0 │ │ @cdktf/provider-local │ 10.1.0 │ └───────────────┴──────────────────┴─────────┴────────────┴───────────────────────┴─────────────────┘

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

  • [ ] I'm interested in contributing a fix myself

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

elruwen avatar Mar 22 '24 01:03 elruwen

I think the root of the problem might lie in this code:

                foo: myvar.stringValue + "bar",

If you were to replace this with Fn.join("", [myvar.stringValue, "bar"]), and try again, does the error go away?

mutahhir avatar Mar 25 '24 09:03 mutahhir

That works. But I think it is a bit tedious to use. Especially when it comes to format strings. And the generated code looks good. Do you have currently cases where it doesn't work?

elruwen avatar Mar 26 '24 00:03 elruwen