Terraform: "terraform console" incorrectly omits object and map elements whose values are null

Created on 21 Nov 2019  ยท  6Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.16

Steps to Reproduce

$ terraform console
> {a=null}
{}
> keys({a=null})
[
  "a",
]
> keys({})
[]

Expected Behavior

Either {a=null} != {}, or keys({a=null}) == [].

bug cli v0.12

All 6 comments

Somewhat also suprisingly (maybe I should open a separate issue?)

$ terraform console
> {a=null}["a"]
null
> {}["a"]
Error: Invalid index

I have a feeling that the bug here is in the object/map rendering code in terraform console: null values _are_ allowed in objects and maps, and are distinct from the attribute/element being absent, because Terraform's type system is a superset of JSON which makes a similar distinction.

In other words, the first step in the reproduction is the incorrect one. Should be:

> {a=null}
{
  a = null
}

It looks like this is just a cosmetic bug in the terraform console UI rather than an inconsistency in the type system itself, but still definitely worth a fix because indeed it's pretty confusing and misleading.

The reason for this inconsistency might be because the terraform console printing logic is similar to the terraform plan diff rendering. In the case of resource arguments null _is_ the same as unset, so this behavior makes sense in that context but not in the terraform console context where we're working with normal values.

It looks like this is just a cosmetic bug in the terraform console UI rather than an inconsistency in the type system itself, but still definitely worth a fix because indeed it's pretty confusing and misleading.

_Definitely_ worth fixing. I threw out my entire plan for handling certain module inputs that I was working on when I filed #22316 because I thought that merge() was just broken due to how the console behaved.

I _can_ see now that it was clearly a console issue though:

> merge({"a"=1,"b"=2},{"a"=null,"c"=3}) 
{
  "b" = 2
  "c" = 3
}
> keys(merge({"a"=1,"b"=2},{"a"=null,"c"=3}))
[
  "a",
  "b",
  "c",
]

This is fixed in #26189, which will be part of the Terraform 0.14.0 release.

Sample session with a build from today:

$ terraform console
> {a=null}
{
  "a" = null
}
> merge({"a"=1,"b"=2},{"a"=null,"c"=3})
{
  "a" = null
  "b" = 2
  "c" = 3
}

:tada:

Thanks!!!

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.

Was this page helpful?
0 / 5 - 0 ratings