minamijoyo / tfupdate

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

Failed to parse file if a map index is a variable reference #12

Closed minamijoyo closed 4 years ago

minamijoyo commented 4 years ago

Summary:

Failed to parse file if a map index is a variable reference

Version:

$ tfupdate --version
0.3.2

Expected behavior:

no error

Actual behavior:

panic

Step to reproduce:

  1. Given the following file:

tmp/test.tf

variable "a" {
  default = {
    dev  = 1
    prod = 2
  }
}

variable "env" {
  default = "dev"
}

resource "hoge" "fuga" {
  b = a[var.env]
}
  1. Run tfupdate
$ tfupdate terraform ./tmp/test.tf
failed to parse input: ./tmp/test.tf
panic: didn't find any token of type TokenIdent
This may be caused by a bug in the hclwrite parser.
As a workaround, you can ignore this file with --ignore-path option
minamijoyo commented 4 years ago

debug log

$ TFUPDATE_LOG=TRACE tfupdate terraform ./tmp/test.tf
2020/01/09 11:24:27 [INFO] CLI args: []string{"tfupdate", "terraform", "./tmp/test.tf"}
2020/01/09 11:24:28 [INFO] Update terraform to 0.12.19
2020/01/09 11:24:28 [DEBUG] check file: ./tmp/test.tf
2020/01/09 11:24:28 [DEBUG] failed to parse input: ./tmp/test.tf
stacktrace: goroutine 1 [running]:
runtime/debug.Stack(0xc0004e47b8, 0x16354a0, 0xc0002f49b0)
        /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/minamijoyo/tfupdate/tfupdate.safeParseConfig.func1(0x7ffeefbfef03, 0xd, 0xc0004e7a98)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:76 +0x6e
panic(0x16354a0, 0xc0002f49b0)
        /usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/hashicorp/hcl/v2/hclwrite.inputTokens.PartitionType(0xc000455ac0, 0x0, 0x0, 0xc00000a370, 0x0, 0x0, 0x49, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:88 +0x580
github.com/hashicorp/hcl/v2/hclwrite.inputTokens.PartitionTypeSingle(0xc000455ac0, 0x0, 0x0, 0xc00000a370, 0x0, 0x0, 0x7ffe00000049, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:92 +0xe3
github.com/hashicorp/hcl/v2/hclwrite.parseTraversalStep(0x1887320, 0xc00030c190, 0xc000455ac0, 0x0, 0x0, 0xc00000a370, 0x0, 0x0, 0xc000455ac0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:395 +0x364
github.com/hashicorp/hcl/v2/hclwrite.parseTraversal(0xc0002f47e0, 0x1, 0x1, 0xc000455ac0, 0x0, 0x0, 0xc00000a370, 0x0, 0x0, 0xc000455ac0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:377 +0x3da
github.com/hashicorp/hcl/v2/hclwrite.parseExpression(0x1889f60, 0xc00045e0a0, 0xc000455ac0, 0x5, 0x5, 0xc00000a370, 0x5, 0x5, 0xd)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:356 +0x175
github.com/hashicorp/hcl/v2/hclwrite.parseAttribute(0xc000418380, 0xc000455a60, 0x0, 0x0, 0xc00000a368, 0x0, 0x0, 0xc000455940, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:260 +0xaf4
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x1881f60, 0xc000418380, 0xc000455940, 0xa, 0xa, 0xc00000a350, 0xa, 0xa, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:217 +0x632
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0002fe2c0, 0xc000455940, 0xa, 0xa, 0xc00000a350, 0xa, 0xa, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parseBlock(0xc0000b70e0, 0xc000455940, 0x0, 0x0, 0xc00000a350, 0x0, 0x0, 0xc0004555e0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:329 +0xf72
github.com/hashicorp/hcl/v2/hclwrite.parseBodyItem(0x1881fa0, 0xc0000b70e0, 0xc0004555e0, 0x15, 0x15, 0xc00000a308, 0x15, 0x15, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:219 +0x4f7
github.com/hashicorp/hcl/v2/hclwrite.parseBody(0xc0002fe370, 0xc000454800, 0x3b, 0x40, 0xc00000a1e0, 0x3b, 0x3b, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:193 +0x68d
github.com/hashicorp/hcl/v2/hclwrite.parse(0xc0003d6600, 0x90, 0x600, 0x7ffeefbfef03, 0xd, 0x1, 0x1, 0x0, 0xc0004d7a80, 0x187c3c0, ...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/parser.go:53 +0x2e4
github.com/hashicorp/hcl/v2/hclwrite.ParseConfig(...)
        /go/pkg/mod/github.com/hashicorp/hcl/v2@v2.1.1-0.20191120012119-7f9aa845c107/hclwrite/public.go:27
github.com/minamijoyo/tfupdate/tfupdate.safeParseConfig(0xc0003d6600, 0x90, 0x600, 0x7ffeefbfef03, 0xd, 0x1, 0x1, 0x0, 0x0, 0x0, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:85 +0x10f
github.com/minamijoyo/tfupdate/tfupdate.UpdateHCL(0x187c3c0, 0xc00000e1d8, 0x187b8e0, 0xc0002ea930, 0x7ffeefbfef03, 0xd, 0x176f86b, 0x9, 0x0, 0x0, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/update.go:48 +0x15b
github.com/minamijoyo/tfupdate/tfupdate.UpdateFile(0x188ed60, 0x1cdb680, 0x7ffeefbfef03, 0xd, 0x176f86b, 0x9, 0x0, 0x0, 0xc0004121b9, 0x7, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/file.go:26 +0x290
github.com/minamijoyo/tfupdate/tfupdate.UpdateFileOrDir(0x188ed60, 0x1cdb680, 0x7ffeefbfef03, 0xd, 0x176f86b, 0x9, 0x0, 0x0, 0xc0004121b9, 0x7, ...)
        /go/src/github.com/minamijoyo/tfupdate/tfupdate/file.go:114 +0x1eb
github.com/minamijoyo/tfupdate/command.(*TerraformCommand).Run(0xc0000aa5a0, 0xc00008c1a0, 0x1, 0x1, 0xc000116ae0)
        /go/src/github.com/minamijoyo/tfupdate/command/terraform.go:64 +0x49a
github.com/mitchellh/cli.(*CLI).Run(0xc0000d08c0, 0xc0000d08c0, 0xc0000cbf40, 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 TokenIdent
This may be caused by a bug in the hclwrite parser.
As a workaround, you can ignore this file with --ignore-path option
minamijoyo commented 4 years ago

This maybe releated to https://github.com/hashicorp/hcl/issues/327

and it seems to be fixed in hcl v2.3.0

https://github.com/hashicorp/hcl/pull/328

minamijoyo commented 4 years ago

Fixed in tfupdate v0.3.3