hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.35k stars 9.49k forks source link

Panic with setunion() on variable input #25318

Closed bondsbw closed 4 years ago

bondsbw commented 4 years ago

Terraform Version

Terraform v0.13.0-beta2

Terraform Configuration Files

variable "x" {
  default = []
}

locals {
  a = setunion(var.x, [])
}

Debug Output

https://gist.github.com/bondsbw/8526cd1ed6ff1df0f4aadc2532b45bf2

Expected Behavior

No error.

Actual Behavior

Error: Error in function call

  on main.tf line 6, in locals:
   6:   a = setunion(var.x, [])

Call to function "setunion" failed: panic in function implementation: value is
not known
goroutine 84 [running]:
runtime/debug.Stack(0xc000223b08, 0x225ee00, 0x2c7ff20)
        /usr/local/go/src/runtime/debug/stack.go:24 +0xa4
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
        /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/error.go:44

...

Steps to Reproduce

  1. terraform init
  2. terraform plan

    Additional Context

Running on Windows 10 version 10.0.17763.0.

apparentlymart commented 4 years ago

Thanks for reporting this, @bondsbw!

I've both reproduced this on Terraform 0.13.0 and seen that it's a regression compared to Terraform 0.12, where this doesn't panic.

I think this is caused by the upstream change zclconf/go-cty#52, which addressed some other misbehavior in these set functions but has introduced this bug because it's calling LengthInt on a value without checking if it's known first. Unknown collections don't have a known length, so it'll need to do something special in that case.


In case it's useful in some way, here's my version of the stack trace from my local reproduction on a build from current Terraform v0.13 (the master branch) at the time I'm posting this:

Error: Error in function call

  on setunion-panic.tf line 6, in locals:
   6:   a = setunion(var.x, [])

Call to function "setunion" failed: panic in function implementation: value is
not known
goroutine 56 [running]:
runtime/debug.Stack(0xc000361b08, 0x2210780, 0x2c36b80)
    /home/user/.goenv/versions/1.14.2/src/runtime/debug/stack.go:24 +0x9d
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues.func1(0xc000361fb8,
0xc000361fc8)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:217
+0x78
panic(0x2210780, 0x2c36b80)
    /home/user/.goenv/versions/1.14.2/src/runtime/panic.go:969 +0x166
github.com/zclconf/go-cty/cty.Value.LengthInt(0x2ce0340, 0xc000081fe0,
0x22cdc20, 0x4115aa0, 0xc00033e180)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/value_ops.go:994
+0x285
github.com/zclconf/go-cty/cty/function/stdlib.setOperationReturnType(0xc00034a440,
0x2, 0x2, 0xc000081ff0, 0xc000361df0, 0x0, 0x0)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go:170
+0x109
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues(0xc0001927e0,
0xc00034a440, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:221
+0x433
github.com/zclconf/go-cty/cty/function.Function.Call(0xc0001927e0,
0xc00034a440, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /home/user/dev/terraform/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:228
+0xb5
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc000312000,
0xc000346b60, 0x0, 0xc000363700, 0x1, 0x1, 0x0, 0x0, 0x0)
    /home/user/dev/terraform/terraform/vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go:412
+0x109d
github.com/hashicorp/terraform/lang.(*Scope).EvalExpr(0xc0001ec870, 0x2cdf040,
0xc000312000, 0x2ce0240, 0x4115ee8, 0x0, 0x0, 0xc0003638d0, 0x1, 0x1, ...)
    /home/user/dev/terraform/terraform/lang/eval.go:93 +0x1b3
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateExpr(0xc000350340,
0x2cdf040, 0xc000312000, 0x2ce0240, 0x4115ee8, 0x0, 0x0, 0x0, 0x2,
0xc0001b2a00, ...)
    /home/user/dev/terraform/terraform/terraform/eval_context_builtin.go:286
+0x1e7
github.com/hashicorp/terraform/terraform.(*EvalLocal).Eval(0xc000346540,
0x2d21b60, 0xc000350340, 0x1, 0x1, 0xc00032dc00, 0x500000000000000)
    /home/user/dev/terraform/terraform/terraform/eval_local.go:43 +0x6d7
github.com/hashicorp/terraform/terraform.EvalRaw(0x2c77d00, 0xc000346540,
0x2d21b60, 0xc000350340, 0x22b08c0, 0x3fa58e5, 0x2210780, 0xc00034c260)
    /home/user/dev/terraform/terraform/terraform/eval.go:49 +0xc7
github.com/hashicorp/terraform/terraform.Eval(0x2c77d00, 0xc000346540,
0x2d21b60, 0xc000350340, 0xc000346540, 0x2c77d00, 0xc000346540, 0x0)
    /home/user/dev/terraform/terraform/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x24f69e0,
0xc00033d170, 0x0, 0x0, 0x0)
    /home/user/dev/terraform/terraform/terraform/graph.go:73 +0xc77
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc00035a660,
0x24f69e0, 0xc00033d170, 0xc00034a380)
    /home/user/dev/terraform/terraform/dag/walk.go:387 +0x357
created by github.com/hashicorp/terraform/dag.(*Walker).Update
    /home/user/dev/terraform/terraform/dag/walk.go:309 +0x11d7
.
ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.