minamijoyo / tfupdate

Update version constraints in your Terraform configurations
MIT License
542 stars 23 forks source link

panic: didn't find any token of type TokenOBrack with legacy dot access of numeric indexes #18

Closed minamijoyo closed 4 years ago

minamijoyo commented 4 years ago

Version

$ tfupdate --version
0.3.5

Configuration

$ cat tmp/test.tf

locals {
  name = kubernetes_namespace.my_conditional_namespaces.metadata.0.name
}

Expected behavior

$ tfupdate terraform tmp/test.tf

Should parse with no errors

Actual behavior

Panic

$ tfupdate terraform tmp/test.tf
failed to parse input: tmp/test.tf
panic: didn't find any token of type TokenOBrack
This may be caused by a bug in the hclwrite parser.
As a workaround, you can ignore this file with --ignore-path option

Debug log

$ TFUPDATE_LOG=DEBUG tfupdate terraform tmp/test.tf
2020/06/11 09:55:22 [INFO] CLI args: []string{"tfupdate", "terraform", "tmp/test.tf"}
2020/06/11 09:55:22 [INFO] Update terraform to 0.12.26
2020/06/11 09:55:22 [DEBUG] check file: tmp/test.tf
2020/06/11 09:55:22 [DEBUG] failed to parse input: tmp/test.tf
stacktrace: goroutine 1 [running]:
runtime/debug.Stack(0xc0004ea9b0, 0x1640e80, 0xc000304820)
        /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/minamijoyo/tfupdate/tfupdate.safeParseConfig.func1(0x7ffeefbff2eb, 0xb, 0xc0004eda98)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:76 +0x6e
panic(0x1640e80, 0xc000304820)
        /usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/hashicorp/hcl/v2/hclwrite.inputTokens.PartitionType(0xc000115bc0, 0x2, 0x2, 0xc0002e0830, 0x2, 0x2, 0x7ffe0000005b, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:88 +0x580
github.com/hashicorp/hcl/v2/hclwrite.parseTraversalStep(0x1897120, 0xc0000a52c0, 0xc000115bc0, 0x2, 0x2, 0xc0002e0830, 0x2, 0x2, 0xc000115bc0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:408 +0xdab
github.com/hashicorp/hcl/v2/hclwrite.parseTraversal(0xc000110180, 0x5, 0x8, 0xc0001159e0, 0x9, 0x9, 0xc0002e0808, 0x9, 0x9, 0xc0001159e0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:377 +0x3da
github.com/hashicorp/hcl/v2/hclwrite.parseExpression(0x189a020, 0xc0000a50e0, 0xc0001159e0, 0x9, 0x9, 0xc0002e0808, 0x9, 0x9, 0x2)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:356 +0x175
github.com/hashicorp/hcl/v2/hclwrite.parseAttribute(0xc0003022a0, 0xc0001159e0, 0x0, 0x0, 0xc0002e0808, 0x0, 0x0, 0xc0001158c0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:260 +0xaf4
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x1891c20, 0xc0003022a0, 0xc0001158c0, 0xd, 0xd, 0xc0002e07f0, 0xd, 0xd, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:217 +0x632
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0003d2000, 0xc0001158c0, 0xd, 0xd, 0xc0002e07f0, 0xd, 0xd, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parseBlock(0xc0000b2a20, 0xc0001158c0, 0x0, 0x0, 0xc0002e07f0, 0x0, 0x0, 0xc000115800, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:329 +0xf72
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x1891c60, 0xc0000b2a20, 0xc000115800, 0x11, 0x11, 0xc0002e07e0, 0x11, 0x11, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:219 +0x4f7
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0003d20b0, 0xc000115800, 0x12, 0x20, 0xc0002e07e0, 0x12, 0x12, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parse(0xc000306600, 0x53, 0x600, 0x7ffeefbff2eb, 0xb, 0x1, 0x1, 0x0, 0xc000497a80, 0x188bc80, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/parser.go:53 +0x2e4
github.com/hashicorp/hcl/v2/hclwrite.ParseConfig(...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.1-0.20200103191330-7990d6e9a2c9/hclwrite/public.go:27
github.com/minamijoyo/tfupdate/tfupdate.safeParseConfig(0xc000306600, 0x53, 0x600, 0x7ffeefbff2eb, 0xb, 0x1, 0x1, 0x0, 0x0, 0x0, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:85 +0x10f
github.com/minamijoyo/tfupdate/tfupdate.UpdateHCL(0x188bc80, 0xc00000e1f0, 0x188b200, 0xc0002e26f0, 0x7ffeefbff2eb, 0xb, 0x177d26c, 0x9, 0x0, 0x0, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:48 +0x15b
github.com/minamijoyo/tfupdate/tfupdate.UpdateFile(0x189ed60, 0x1cf0860, 0x7ffeefbff2eb, 0xb, 0x177d26c, 0x9, 0x0, 0x0, 0xc000275a61, 0x7, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/file.go:26 +0x290
github.com/minamijoyo/tfupdate/tfupdate.UpdateFileOrDir(0x189ed60, 0x1cf0860, 0x7ffeefbff2eb, 0xb, 0x177d26c, 0x9, 0x0, 0x0, 0xc000275a61, 0x7, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/file.go:114 +0x1eb
github.com/minamijoyo/tfupdate/command.(*TerraformCommand).Run(0xc0000a45a0, 0xc0000861a0, 0x1, 0x1, 0xc000112ba0)
        /go/src/github.com/minamijoyo/tfupdate/command/terraform.go:64 +0x49a
github.com/mitchellh/cli.(*CLI).Run(0xc0000cc8c0, 0xc0000cc8c0, 0xc0000c7f40, 0x1)
        /go/pkg/mod/github.com/mitchellh/cli@v1.0.0/cli.go:255 +0x1da
main.main()
        /go/src/github.com/minamijoyo/tfupdate/main.go:47 +0x1f7
failed to parse input: tmp/test.tf
panic: didn't find any token of type TokenOBrack
This may be caused by a bug in the hclwrite parser.
As a workaround, you can ignore this file with --ignore-path option

Related issue

https://github.com/minamijoyo/tfupdate/issues/4#issuecomment-638284704 https://github.com/hashicorp/hcl/issues/327 https://github.com/hashicorp/hcl/pull/369

This issue seems to be fixed in upstream hcl@v2.5.1