apparentlymart / terraform-clean-syntax

A simple tool for Terraform language syntax cleanup, extending terraform fmt
Mozilla Public License 2.0
160 stars 24 forks source link

panic: didn't find any token of type TokenOBrack #5

Closed pdecat closed 4 years ago

pdecat commented 4 years ago

On the heels of #2, here's a variant with TokenOBrack:

# terraform-clean-syntax common/k8s/cluster0/monitoring.tf
panic: didn't find any token of type TokenOBrack

goroutine 1 [running]:
github.com/hashicorp/hcl/v2/hclwrite.inputTokens.PartitionType(0xc0000f7ce0, 0x2, 0x2, 0xc0000ba698, 0x2, 0x2, 0x7fff0000005b, 0x0, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:88 +0x580
github.com/hashicorp/hcl/v2/hclwrite.parseTraversalStep(0x709100, 0xc0000b0f60, 0xc0000f7ce0, 0x2, 0x2, 0xc0000ba698, 0x2, 0x2, 0xc0000f7ce0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:408 +0xdab
github.com/hashicorp/hcl/v2/hclwrite.parseTraversal(0xc0000f0100, 0x5, 0x8, 0xc0000f7b00, 0x9, 0x9, 0xc0000ba670, 0x9, 0x9, 0xc0000f7b00, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:377 +0x3da
github.com/hashicorp/hcl/v2/hclwrite.parseExpression(0x70a740, 0xc0000b0d80, 0xc0000f7b00, 0x9, 0x9, 0xc0000ba670, 0x9, 0x9, 0x2)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:356 +0x175
github.com/hashicorp/hcl/v2/hclwrite.parseAttribute(0xc0000d01c0, 0xc0000f7b00, 0x0, 0x0, 0xc0000ba670, 0x0, 0x0, 0xc0000f79e0, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:260 +0xaf4
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x707960, 0xc0000d01c0, 0xc0000f79e0, 0xd, 0xd, 0xc0000ba658, 0xd, 0xd, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:217 +0x632
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0000ba4d0, 0xc0000f79e0, 0xd, 0xd, 0xc0000ba658, 0xd, 0xd, 0x0, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parseBlock(0xc0000b67e0, 0xc0000f79e0, 0x0, 0x0, 0xc0000ba658, 0x0, 0x0, 0xc0000f7800, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:329 +0xf72
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x7079a0, 0xc0000b67e0, 0xc0000f7800, 0x14, 0x14, 0xc0000ba630, 0x14, 0x14, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:219 +0x4f7
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0000ba580, 0xc0000f7800, 0x15, 0x20, 0xc0000ba630, 0x15, 0x15, 0x0, 0x0, 0x0, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parse(0xc000104000, 0x4e, 0x24e, 0x7fff989f6e41, 0x21, 0x1, 0x1, 0x0, 0xffffffffffffff9c, 0xc0000a8210, ...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/parser.go:53 +0x2e4
github.com/hashicorp/hcl/v2/hclwrite.ParseConfig(...)
        /home/patrick/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191206020329-84e71e9393a0/hclwrite/public.go:27
main.processFile(0x7fff989f6e41, 0x21, 0x1a4)
        /home/patrick/go/src/github.com/apparentlymart/terraform-clean-syntax/main.go:78 +0xc8
main.processItem(0x7fff989f6e41, 0x21)
        /home/patrick/go/src/github.com/apparentlymart/terraform-clean-syntax/main.go:55 +0x2e7
main.main()
        /home/patrick/go/src/github.com/apparentlymart/terraform-clean-syntax/main.go:30 +0xec

Here's the culprit:

module "test" {
  test         = kubernetes_config_map.test.metadata.0.name
}
apparentlymart commented 4 years ago

Oh, interesting... this one seems to be hclwrite not understanding this legacy HIL-style index syntax .0 instead of [0].

If you don't have too many examples of this and want to work around this quickly for now, I think manually rewriting that as follows should fix it:

module "test" {
  test = kubernetes_config_map.test.metadata[0].name
}

I'll make an HCL issue for this later, but hopefully that helps as a workaround in the meantime.