hashicorp / hashicorp/web-unified-docs

Coalesce Default Behavior with Nulls, returns error.

Open
#804 2 comments 19 reactions 0 assignees View on GitHub
documentation Terraform CE
Dominant language
MDX
Stars
86
Forks
275
Avg merge
3d 18h
Merged PRs (30d)
199

Description

### Current Terraform Version
Terraform version: 0.12.25

```
...
```

### Use-cases
When evaluating a set of variables to determine if all are null or empty strings, coalesce returns an error if all values presented to it are null or empty strings, rather than returning null.

I believe it would be beneficial to have coalesce on receiving all nulls or empty strings, to return null, rather than an error that it cannot work on all null or empty strings.

### Attempted Solutions

Current code I am using to work around this, by presenting the last string as "Disabled" and hoping, expecting that if all the others are null (my variables file sets this as the default value), this would return "Disabled":

`local {
site_config_enabled = coalesce(var.site_config_always_on,var.http2_enabled,"Disabled") == "Disabled" ? null : "Enabled"
}`

When I call this later in a dynamic block for site_config (this is for AzureRM Function App) my for_each line is:
`for_each = local.site_config_enabled == "Disabled" ? [] : list("foo")`

This can be easily seen and reproduced from the Terraform Console.
`coalesce(null,null,"")`

Returns:
`Call to function "coalesce" failed: no non-null, non-empty-string arguments.`

### Proposal

What I would like:

coalesce(null,null,"")

Returns:
null

This would allow me to write code like this:
`local {
site_config_enabled = coalesce(var.site_config_always_on,var.site_config_http2_enabled) == null ? null : "enabled"
}`

### References

Contributor guide

Open the contributing guide

Research direction

Start in Terraform Console by reproducing coalesce(null,null,"") and compare the current error with the proposed null result. Review how coalesce handles null and empty-string arguments, then define and verify the behavior for all-null and all-empty inputs. Done means the requested result is implemented consistently and covered by appropriate regression checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
terraform
Domain
devops, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.