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

Terraform 0.13-beta2 panic: Call to function "setintersection" failed: panic in function implementation: value is not known #25550

Closed mariux closed 4 years ago

mariux commented 4 years ago

We are facing an issue where terraform 0.13.x beta fails with an Error followed by a panic. terraform 0.12 works fine with the same code.

Error: Error in function call

  on x.tf line 6, in locals:
   6:   intersection = setintersection(local.set)

Call to function "setintersection" failed: panic in function implementation:
value is not known

Terraform Version

$ terraform-0.13.0-beta2 version
Terraform v0.13.0-beta2
+ provider registry.terraform.io/hashicorp/aws v2.70.0

$ terraform version
Terraform v0.12.26
+ provider.aws v2.70.0

Terraform Configuration Files

data "aws_region" "region" {}

locals {
  set = distinct(["${data.aws_region.region}a", "eu-west-1b"])

  intersection = setintersection(local.set)
}

provider "aws" {
  region  = "us-east-1"
}

Debug Output

https://gist.github.com/mariux/5c5edcdec6b43ec59b1127c89791895f#file-terraform-0-13-intersection-disctinct-debug-txt

Crash Output

https://gist.github.com/mariux/5c5edcdec6b43ec59b1127c89791895f#file-crash-log

Expected Behavior

Actual Behavior

Steps to Reproduce

  1. terraform init
  2. terraform plan
  3. see the crash with 0.13.x, see no crash with 0.12.x

Additional Context

This is a stripped-down version of the fail to just include the needed data as a minimal test case. We are maintaining a module where this fails in bigger context.

References

Workaround

Happy to provide more information if needed. thank you a lot for this great tooling.. ;)

apparentlymart commented 4 years ago

Hi @mariux! Sorry for this crash, and thanks for reporting it.

I tried to reproduce this with the following simplified configuration, just because it's always easier to iterate if we can find a reproduction case that doesn't require any remote API credentials:


resource "null_resource" "example" {
}

locals {
  set = distinct([null_resource.example.id, "foo"])
}

output "intersection" {
  value = setintersection(local.set)
}

Since the error message refers to unknown values, I used the id of null_resource here because I knew that would be unknown at plan time: the null_resource resource type assigns itself an id only at apply time.

I ran terraform plan with Terraform 0.13.0-beta3 and saw it work as expected:

$ terraform0.13.0-beta3 plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.example will be created
  + resource "null_resource" "example" {
      + id = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

However, when I tried with -beta2 instead, I was able to reproduce it with the same result as you:

$ terraform plan

Error: Error in function call

  on setintersection-panic.tf line 10, in output "intersection":
  10:   value = setintersection(local.set)

Call to function "setintersection" failed: panic in function implementation:
value is not known
goroutine 63 [running]:
runtime/debug.Stack(0xc00034b9f8, 0x2208940, 0x2c2be40)
    /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues.func1(0xc00034bea8,
0xc00034beb8)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/function.go:217
+0x78
panic(0x2208940, 0x2c2be40)
    /usr/local/go/src/runtime/panic.go:969 +0x166
github.com/zclconf/go-cty/cty.Value.LengthInt(0x2cd5000, 0xc000196000,
0x22c5d80, 0x40fd800, 0x0)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/value_ops.go:994
+0x285
github.com/zclconf/go-cty/cty/function/stdlib.setOperationReturnType(0xc0006b5a80,
0x1, 0x1, 0xc000196000, 0xc00034bce0, 0x0, 0x0)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go:170
+0x109
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues(0xc000194870,
0xc0006b5a80, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/function.go:221
+0x433
github.com/zclconf/go-cty/cty/function.Function.Call(0xc000194870,
0xc0006b5a80, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /home/circleci/project/project/vendor/github.com/zclconf/go-cty/cty/function/function.go:228
+0xb5
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc00057a0f0,
0xc0006b5a60, 0x0, 0xc00034d600, 0x1, 0x1, 0x0, 0x0, 0x0)
    /home/circleci/project/project/vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go:412
+0x109d
github.com/hashicorp/terraform/lang.(*Scope).EvalExpr(0xc00054cff0, 0x2cd3d00,
0xc00057a0f0, 0x2cd4f00, 0x40fdc48, 0x200000003, 0xc0003a0a80, 0xc000063800,
0xc000870798, 0x4ba01b, ...)
    /home/circleci/project/project/lang/eval.go:93 +0x1b3
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateExpr(0xc00045f520,
0x2cd3d00, 0xc00057a0f0, 0x2cd4f00, 0x40fdc48, 0x0, 0x0, 0x40cf2e0, 0x3d, 0x0,
...)
    /home/circleci/project/project/terraform/eval_context_builtin.go:286 +0x1e7
github.com/hashicorp/terraform/terraform.(*EvalWriteOutput).Eval(0xc0006b5560,
0x2d16820, 0xc00045f520, 0x1, 0x1, 0x264a060, 0xc00011a000)
    /home/circleci/project/project/terraform/eval_output.go:47 +0x159
github.com/hashicorp/terraform/terraform.EvalRaw(0x2c6d100, 0xc0006b5560,
0x2d16820, 0xc00045f520, 0x25, 0x0, 0x0, 0x25)
    /home/circleci/project/project/terraform/eval.go:49 +0xc7
github.com/hashicorp/terraform/terraform.(*EvalOpFilter).Eval(0xc0005852c0,
0x2d16820, 0xc00045f520, 0x1, 0x1, 0x1dcceaf6181a4, 0xc000870b88)
    /home/circleci/project/project/terraform/eval_filter_operation.go:37 +0x4c
github.com/hashicorp/terraform/terraform.EvalRaw(0x2c6cee0, 0xc0005852c0,
0x2d16820, 0xc00045f520, 0x129cbabe, 0x80, 0xc0005ea000, 0xc00013e030)
    /home/circleci/project/project/terraform/eval.go:49 +0xc7
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc0006b5580,
0x2d16820, 0xc00045f520, 0x1, 0x1, 0x1, 0xc000581150)
    /home/circleci/project/project/terraform/eval_sequence.go:20 +0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x2c6d000, 0xc0006b5580,
0x2d16820, 0xc00045f520, 0x22a8a20, 0x3f8d685, 0x2208940, 0xc000581170)
    /home/circleci/project/project/terraform/eval.go:49 +0xc7
github.com/hashicorp/terraform/terraform.Eval(0x2c6d000, 0xc0006b5580,
0x2d16820, 0xc00045f520, 0xc0006b5580, 0x2c6d000, 0xc0006b5580, 0x0)
    /home/circleci/project/project/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x2534f80,
0xc0005845d0, 0x0, 0x0, 0x0)
    /home/circleci/project/project/terraform/graph.go:73 +0xc77
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc000546b40,
0x2534f80, 0xc0005845d0, 0xc00009ac80)
    /home/circleci/project/project/dag/walk.go:387 +0x357
created by github.com/hashicorp/terraform/dag.(*Walker).Update
    /home/circleci/project/project/dag/walk.go:309 +0x11d7
.

It looks like this bug was fixed in #25400, which was merged into our main branch just before releasing v0.12.0-beta3. For that reason, I'm going to close this one. However, I do appreciate you taking the time to report it! I expect that if you try again with v0.12.0-beta3 then you will also see the setintersection function succeed as expected.

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.