Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using merge on a variable that is null results in a panic #21695

Closed
pselle opened this issue Jun 11, 2019 · 4 comments
Closed

Using merge on a variable that is null results in a panic #21695

pselle opened this issue Jun 11, 2019 · 4 comments

Comments

@pselle
Copy link
Contributor

pselle commented Jun 11, 2019

Relocating from this comment: #5471 (comment)

Terraform Version

...

Terraform Configuration Files

...

Debug Output

Crash Output

Error: Error in function call

  on ../aws-iam-role/main.tf line 20, in locals:
  20:   iam_role_tags                       = "${merge(var.iam_role_tags, map("Name", format("%s", local.iam_role_name)), map("terraform", "true"))}"
    |----------------
    | local.iam_role_name is "rds-config-role"
    | var.iam_role_tags is null

Call to function "merge" failed: panic in function implementation: can't use
ElementIterator on null value
goroutine 325 [running]:
runtime/debug.Stack(0xc00066dfa8, 0x1856a20, 0x1f7f5d0)

Expected Behavior

Omit the null values

Actual Behavior

A panic!

Steps to Reproduce

Additional Context

References

@apparentlymart
Copy link
Contributor

In this case it looks like the map itself is what is null, so I think it is expected that this would return an error, but of course it should be a user-friendly error rather than a panic like this.

Curiously, I see that this function is explicitly opting in to receiving nulls, but then failing to handle them. The easiest fix here would be to remove AllowNull: true and thus accept the standard behavior of having the language runtime itself detect and reject the null value.

While in principle ignoring the null values altogether could be a reasonable behavior, elsewhere in the language we've tended towards being explicit about treating null as distinct from an empty value because it tends to make it easier to catch mistakes where something is mistakenly unset. I suggest that we stick with that principle here and make this an error; in line with our usual "explicit is better than implicit" principle, there is an explicit way to indicate the behavior of treating a null as if it were an empty map:

coalesce(var.iam_role_tags, {})

@mikalai-t
Copy link

Just filling the gaps

Terraform version

./terraform-0.12.1 --version
Terraform v0.12.1
+ provider.null v2.1.2

Terraform Configuration Files

tree ./ -L 1
./
├── terraform-0.12.1
└── test.tf

test.tf

variable "empty_map" {
  type = "map"
  default = {}
}

locals {
  modify_var_before_use  = merge(var.empty_map, map("some_key", "some_value"))
}

data "null_data_source" "values" {
  inputs = {
    test_local_var = local.modify_var_before_use["some_key"]
  }
}

output "value_from_null_data_source" {
  value = data.null_data_source.values.outputs["test_local_var"]
}

Default:

./terraform-0.12.1 apply
data.null_data_source.values: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

value_from_null_data_source = some_value

With null value:

./terraform-0.12.1 apply -var=empty_map=null

Error: Error in function call

  on test.tf line 7, in locals:
   7:   modify_var_before_use  = merge(var.empty_map, map("some_key", "some_value"))
    |----------------
    | var.empty_map is null

Call to function "merge" failed: panic in function implementation: can't use
ElementIterator on null value
goroutine 46 [running]:
runtime/debug.Stack(0xc000146000, 0x1856a20, 0x1f7f5d0)
	/opt/go/src/runtime/debug/stack.go:24 +0x9d
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
...

@pselle
Copy link
Contributor Author

pselle commented Jun 17, 2019

closed by #21734

@pselle pselle closed this as completed Jun 17, 2019
@ghost
Copy link

ghost commented Jul 25, 2019

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.

@ghost ghost locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants