Skip to content

Commit 10ae444

Browse files
committed
Upgrade HCL to fix optional attr default crash
Also add regression test coverage of the crash. This would occur when objects with optional attributes had default values of different type from the attribute type, and the objects were members of a collection. For example: list(object({ a = optional(set(string), []) })) If this type constraint is applied to a variable value where one object has a set(string) value for a, and the other object applies the empty tuple default, Terraform would crash.
1 parent 1e6f091 commit 10ae444

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ require (
4242
github.com/hashicorp/go-uuid v1.0.3
4343
github.com/hashicorp/go-version v1.6.0
4444
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
45-
github.com/hashicorp/hcl/v2 v2.14.0
45+
github.com/hashicorp/hcl/v2 v2.14.1
4646
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
4747
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
4848
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
387387
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
388388
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
389389
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
390-
github.com/hashicorp/hcl/v2 v2.14.0 h1:jX6+Q38Ly9zaAJlAjnFVyeNSNCKKW8D0wvyg7vij5Wc=
391-
github.com/hashicorp/hcl/v2 v2.14.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
390+
github.com/hashicorp/hcl/v2 v2.14.1 h1:x0BpjfZ+CYdbiz+8yZTQ+gdLO7IXvOut7Da+XJayx34=
391+
github.com/hashicorp/hcl/v2 v2.14.1/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
392392
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0=
393393
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
394394
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=

internal/terraform/context_apply_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12165,6 +12165,70 @@ output "out" {
1216512165
}
1216612166
}
1216712167

12168+
func TestContext2Apply_moduleVariableOptionalAttributesDefaultChild(t *testing.T) {
12169+
m := testModuleInline(t, map[string]string{
12170+
"main.tf": `
12171+
variable "in" {
12172+
type = list(object({
12173+
a = optional(set(string))
12174+
}))
12175+
default = [
12176+
{ a = [ "foo" ] },
12177+
{ },
12178+
]
12179+
}
12180+
12181+
module "child" {
12182+
source = "./child"
12183+
in = var.in
12184+
}
12185+
12186+
output "out" {
12187+
value = module.child.out
12188+
}
12189+
`,
12190+
"child/main.tf": `
12191+
variable "in" {
12192+
type = list(object({
12193+
a = optional(set(string), [])
12194+
}))
12195+
default = []
12196+
}
12197+
12198+
output "out" {
12199+
value = var.in
12200+
}
12201+
`,
12202+
})
12203+
12204+
ctx := testContext2(t, &ContextOpts{})
12205+
12206+
// We don't specify a value for the variable here, relying on its defined
12207+
// default.
12208+
plan, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
12209+
if diags.HasErrors() {
12210+
t.Fatal(diags.ErrWithWarnings())
12211+
}
12212+
12213+
state, diags := ctx.Apply(plan, m)
12214+
if diags.HasErrors() {
12215+
t.Fatal(diags.ErrWithWarnings())
12216+
}
12217+
12218+
got := state.RootModule().OutputValues["out"].Value
12219+
want := cty.ListVal([]cty.Value{
12220+
cty.ObjectVal(map[string]cty.Value{
12221+
"a": cty.SetVal([]cty.Value{cty.StringVal("foo")}),
12222+
}),
12223+
cty.ObjectVal(map[string]cty.Value{
12224+
"a": cty.SetValEmpty(cty.String),
12225+
}),
12226+
})
12227+
if !want.RawEquals(got) {
12228+
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
12229+
}
12230+
}
12231+
1216812232
func TestContext2Apply_provisionerSensitive(t *testing.T) {
1216912233
m := testModule(t, "apply-provisioner-sensitive")
1217012234
p := testProvider("aws")

0 commit comments

Comments
 (0)