terramate-io / terramate

Terramate CLI is an open-source Infrastructure as Code (IaC) Orchestration and Code Generation tool for Terraform, OpenTofu and Terragrunt.
https://terramate.io
Mozilla Public License 2.0
3.12k stars 86 forks source link

[BUG] Panic error when using tm_hcl_expression inside a for loop #1689

Closed crileroro closed 1 month ago

crileroro commented 2 months ago

Describe the bug I get a panic error when using tm_hcl_expression inside a for loop. Since it's not possible to use terramate variables inside a for loop, I have to use tm_merge as explained in this issue https://github.com/terramate-io/terramate/issues/1676.

To Reproduce

  1. Run command terramate generate in the following example (I have made it as simple as possible for the purpose of showing the error):
    
    globals {
    role = [
    {
      foo = "bar"
    }
    ]
    }

generate_hcl "_main.tf" {

content { main_role = tm_merge({}, { for entry in global.role : entry.foo => { arn = tm_hcl_expression("data.aws_iam_role.${entry.foo}.arn") } }) } }


**Log Output**

panic: cannot produce tokens for customdecode.ExpressionVal(&hclsyntax.ScopeTraversalExpr{Traversal:hcl.Traversal{hcl.TraverseRoot{isTraverser:hcl.isTraverser{}, Name:"data", SrcRange:hcl.Range{Filename:"", Start:hcl.Pos{Line:1, Column:1, Byte:0}, End:hcl.Pos{Line:1, Column:5, Byte:4}}}, hcl.TraverseAttr{isTraverser:hcl.isTraverser{}, Name:"aws_iam_role", SrcRange:hcl.Range{Filename:"", Start:hcl.Pos{Line:1, Column:5, Byte:4}, End:hcl.Pos{Line:1, Column:18, Byte:17}}}, hcl.TraverseAttr{isTraverser:hcl.isTraverser{}, Name:"bar", SrcRange:hcl.Range{Filename:"", Start:hcl.Pos{Line:1, Column:18, Byte:17}, End:hcl.Pos{Line:1, Column:22, Byte:21}}}, hcl.TraverseAttr{isTraverser:hcl.isTraverser{}, Name:"arn", SrcRange:hcl.Range{Filename:"", Start:hcl.Pos{Line:1, Column:22, Byte:21}, End:hcl.Pos{Line:1, Column:26, Byte:25}}}}, SrcRange:hcl.Range{Filename:"", Start:hcl.Pos{Line:1, Column:1, Byte:0}, End:hcl.Pos{Line:1, Column:26, Byte:25}}})

goroutine 1 [running]: github.com/hashicorp/hcl/v2/hclwrite.appendTokensForValue({{{0x101c1b640, 0x140004e59b0}}, {0x1018e0160, 0x1400080f250}}, {0x1400088db00, 0x8, 0x8}) github.com/hashicorp/hcl/v2@v2.20.1/hclwrite/generate.go:301 +0xde8 github.com/hashicorp/hcl/v2/hclwrite.appendTokensForValue({{{0x101c1b100, 0x1400080f260}}, {0x1019bc9c0, 0x14000b97c50}}, {0x140008b6840, 0x4, 0x4}) github.com/hashicorp/hcl/v2@v2.20.1/hclwrite/generate.go:287 +0xc14 github.com/hashicorp/hcl/v2/hclwrite.appendTokensForValue({{{0x101c1b100, 0x1400080f340}}, {0x1019bc9c0, 0x1400067b020}}, {0x0, 0x0, 0x0}) github.com/hashicorp/hcl/v2@v2.20.1/hclwrite/generate.go:287 +0xc14 github.com/hashicorp/hcl/v2/hclwrite.TokensForValue({{{0x101c1b100?, 0x1400080f340?}}, {0x1019bc9c0?, 0x1400067b020?}}) github.com/hashicorp/hcl/v2@v2.20.1/hclwrite/generate.go:27 +0x38 github.com/terramate-io/terramate/hcl/ast.TokensForValue({{{0x101c1b100?, 0x1400080f340?}}, {0x1019bc9c0?, 0x1400067b020?}}) github.com/terramate-io/terramate/hcl/ast/expr.go:42 +0x110 github.com/terramate-io/terramate/hcl/ast.(tokenBuilder).literalTokens(...) github.com/terramate-io/terramate/hcl/ast/expr.go:101 github.com/terramate-io/terramate/hcl/ast.(tokenBuilder).build(0x140007c8e30, {0x101c1b2f8?, 0x140001b2c60?}) github.com/terramate-io/terramate/hcl/ast/expr.go:62 +0xdc github.com/terramate-io/terramate/hcl/ast.tokensForExpression(...) github.com/terramate-io/terramate/hcl/ast/expr.go:47 github.com/terramate-io/terramate/hcl/ast.TokensForExpression({0x101c1b2f8?, 0x140001b2c60?}) github.com/terramate-io/terramate/hcl/ast/expr.go:28 +0x40 github.com/terramate-io/terramate/generate/genhcl.copyBody(0x140008b53e0, 0x14000b94d10, {0x101c1b8c0, 0x1400080ef60}) github.com/terramate-io/terramate/generate/genhcl/genhcl.go:429 +0x240 github.com/terramate-io/terramate/generate/genhcl.Load(0x140000d24c8, 0x140008ae1c0, 0x14000ac7e90, {{0x101206805?, 0x10120320f?}}, 0x14000112de0) github.com/terramate-io/terramate/generate/genhcl/genhcl.go:340 +0xe40 github.com/terramate-io/terramate/generate.loadStackCodeCfgs(0x140000d24c8, 0x140008ae1c0, 0x14000ac7e90, {{0x101206805?, 0x0?}}, 0x14000112de0) github.com/terramate-io/terramate/generate/generate.go:1198 +0x94 github.com/terramate-io/terramate/generate.doStackGeneration(0x140000d24c8, {{0x101206805?, 0x0?}}, 0x14000112de0) github.com/terramate-io/terramate/generate/generate.go:216 +0x3e0 github.com/terramate-io/terramate/generate.Do(0x140000d24c8, {{0x101206805?, 0x0?}}, 0x1400055e660?) github.com/terramate-io/terramate/generate/generate.go:184 +0x44 github.com/terramate-io/terramate/cmd/terramate/cli.(cli).gencodeWithVendor(0x140000d2408) github.com/terramate-io/terramate/cmd/terramate/cli/cli.go:1059 +0x114 github.com/terramate-io/terramate/cmd/terramate/cli.(cli).generate(0x140000d2408) github.com/terramate-io/terramate/cmd/terramate/cli/cli.go:1016 +0x60 github.com/terramate-io/terramate/cmd/terramate/cli.(*cli).run(0x140000d2408) github.com/terramate-io/terramate/cmd/terramate/cli/cli.go:654 +0x4c0 github.com/terramate-io/terramate/cmd/terramate/cli.Exec({0x101202fb7, 0x5}, {0x14000148010, 0x1, 0x1}, {0x101c091a8, 0x14000128040}, {0x101c091c8, 0x14000128048}, {0x101c091c8, ...}) github.com/terramate-io/terramate/cmd/terramate/cli/cli.go:340 +0xb0 main.main() github.com/terramate-io/terramate/cmd/terramate/main.go:20 +0x94


**Environment (please complete the following information):**
 - OS:  Mac 14.4.1
 - Git version: 2.39.3 (Apple Git-146)
 - Terramate Version: 0.5.3 <-> 0.8.1
i4ki commented 1 month ago

Hi @crileroro

Thanks for reporting this issue. It will be resolved by #1714. I will let you know once released.