From efb3f3038c9c2ba9ac4c41625f74fb06f02e366e Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Fri, 16 Dec 2022 16:15:04 +0100 Subject: [PATCH 1/5] Add failing test case for the given issue --- internal/terraform/eval_variable_test.go | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/terraform/eval_variable_test.go b/internal/terraform/eval_variable_test.go index 702c7a8a31b0..e6e152ba259b 100644 --- a/internal/terraform/eval_variable_test.go +++ b/internal/terraform/eval_variable_test.go @@ -180,6 +180,18 @@ func TestPrepareFinalInputVariableValue(t *testing.T) { } ] } + // https://github.com/hashicorp/terraform/issues/32396 + // This variable was originally introduced to test the behaviour of the + // dynamic type constraint. You should be able to set primitive types in + // the list consistently. + variable "list_with_nested_collections_dynamic_with_default" { + type = list( + object({ + name = optional(string, "default") + taints = optional(list(map(any)), []) + }) + ) + } ` cfg := testModuleInline(t, map[string]string{ "main.tf": cfgSrc, @@ -714,6 +726,39 @@ func TestPrepareFinalInputVariableValue(t *testing.T) { }), ``, }, + { + "list_with_nested_collections_dynamic_with_default", + cty.TupleVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("default"), + }), + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("complex"), + "taints": cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "key": cty.StringVal("my_key"), + "value": cty.StringVal("my_value"), + }), + }), + }), + }), + cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("default"), + "taints": cty.ListValEmpty(cty.Map(cty.String)), + }), + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("complex"), + "taints": cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "key": cty.StringVal("my_key"), + "value": cty.StringVal("my_value"), + }), + }), + }), + }), + ``, + }, // sensitive { From c9b9b22cf71b65179f558f2b28ea68edc8c40edc Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 20 Dec 2022 11:36:10 +0100 Subject: [PATCH 2/5] pause --- go.mod | 2 ++ internal/terraform/eval_variable.go | 16 ++++++------ internal/terraform/eval_variable_test.go | 33 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index f464a0118995..111405bed679 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/hashicorp/terraform +replace github.com/hashicorp/hcl/v2 => ../../liamcervante/hcl + require ( cloud.google.com/go/kms v1.6.0 cloud.google.com/go/storage v1.28.0 diff --git a/internal/terraform/eval_variable.go b/internal/terraform/eval_variable.go index c489e4f3bea6..8878886383e4 100644 --- a/internal/terraform/eval_variable.go +++ b/internal/terraform/eval_variable.go @@ -90,6 +90,14 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In given = defaultVal // must be set, because we checked above that the variable isn't required } + // Apply defaults from the variable's type constraint to the converted value, + // unless the converted value is null. We do not apply defaults to top-level + // null values, as doing so could prevent assigning null to a nullable + // variable. + if cfg.TypeDefaults != nil && !given.IsNull() { + given = cfg.TypeDefaults.Apply(given) + } + val, err := convert.Convert(given, convertTy) if err != nil { log.Printf("[ERROR] prepareFinalInputVariableValue: %s has unsuitable type\n got: %s\n want: %s", addr, given.Type(), convertTy) @@ -132,14 +140,6 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In return cty.UnknownVal(cfg.Type), diags } - // Apply defaults from the variable's type constraint to the converted value, - // unless the converted value is null. We do not apply defaults to top-level - // null values, as doing so could prevent assigning null to a nullable - // variable. - if cfg.TypeDefaults != nil && !val.IsNull() { - val = cfg.TypeDefaults.Apply(val) - } - // By the time we get here, we know: // - val matches the variable's type constraint // - val is definitely not cty.NilVal, but might be a null value if the given was already null. diff --git a/internal/terraform/eval_variable_test.go b/internal/terraform/eval_variable_test.go index e6e152ba259b..d2b4eba2128e 100644 --- a/internal/terraform/eval_variable_test.go +++ b/internal/terraform/eval_variable_test.go @@ -522,6 +522,39 @@ func TestPrepareFinalInputVariableValue(t *testing.T) { cty.UnknownVal(cty.String), ``, }, + { + "list_with_nested_collections_dynamic_with_default", + cty.TupleVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("default"), + }), + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("complex"), + "taints": cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "key": cty.StringVal("my_key"), + "value": cty.StringVal("my_value"), + }), + }), + }), + }), + cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("default"), + "taints": cty.ListValEmpty(cty.Map(cty.String)), + }), + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("complex"), + "taints": cty.ListVal([]cty.Value{ + cty.MapVal(map[string]cty.Value{ + "key": cty.StringVal("my_key"), + "value": cty.StringVal("my_value"), + }), + }), + }), + }), + ``, + }, // complex types From 5642ba1835fbddedfdf755417980134b1b638ff4 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Wed, 4 Jan 2023 13:21:10 +0100 Subject: [PATCH 3/5] don't use local when sending PR for review --- go.mod | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 111405bed679..19b0e27cc801 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/hashicorp/terraform -replace github.com/hashicorp/hcl/v2 => ../../liamcervante/hcl +// replace github.com/hashicorp/hcl/v2 => ../../liamcervante/hcl +// TODO(liamcervante): Update HCL before merging this PR, that will make the +// tests pass again. require ( cloud.google.com/go/kms v1.6.0 From cf57bdf404962568377870554525353ee6a7edb4 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Mon, 30 Jan 2023 11:09:00 -0500 Subject: [PATCH 4/5] go get github.com/hashicorp/hcl/v2@v2.16.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 19b0e27cc801..26ed41712ace 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( github.com/hashicorp/go-uuid v1.0.3 github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f - github.com/hashicorp/hcl/v2 v2.15.0 + github.com/hashicorp/hcl/v2 v2.16.0 github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2 github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 diff --git a/go.sum b/go.sum index 106c3630e5e7..a90159e1d23f 100644 --- a/go.sum +++ b/go.sum @@ -395,8 +395,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws= github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90= -github.com/hashicorp/hcl/v2 v2.15.0 h1:CPDXO6+uORPjKflkWCCwoWc9uRp+zSIPcCQ+BrxV7m8= -github.com/hashicorp/hcl/v2 v2.15.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng= +github.com/hashicorp/hcl/v2 v2.16.0 h1:MPq1q615H+9wBAdE3EbwEd6imSohElrIguuasbQruB0= +github.com/hashicorp/hcl/v2 v2.16.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng= github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0= github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= From 262b8742424c2ded97705ffa6fb40dbc21e4d685 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Mon, 30 Jan 2023 11:24:21 -0500 Subject: [PATCH 5/5] Update go.mod --- go.mod | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 26ed41712ace..17f5983c6bc4 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,5 @@ module github.com/hashicorp/terraform -// replace github.com/hashicorp/hcl/v2 => ../../liamcervante/hcl -// TODO(liamcervante): Update HCL before merging this PR, that will make the -// tests pass again. - require ( cloud.google.com/go/kms v1.6.0 cloud.google.com/go/storage v1.28.0