Error when using compact function on a list

I have defined a list which has a string and null element in it. Here it is in the output.

Changes to Outputs:
  + rnd = {
      + debug = {
          + policies_for_this_account = [
              + "DeveloperAccess",
              + null,
            ]
        }
      + props = {}
    }

For reasons I cannot figure out (I’ve been at this for quite some time), if I try to pass that list to compact(), I get an error:

╷
│ Error: Invalid function argument
│
│   on ../../../infra-as-code/modules/access-member-accounts/aws/output.tf line 16, in output "debug":
│   16:     policies_for_this_account = compact(local.policies_for_this_account)
│     ├────────────────
│     │ while calling compact(list)
│     │ local.policies_for_this_account is tuple with 2 elements
│
│ Invalid value for "list" parameter: element 1: string required.
╵

I’ve even tried putting a tolist() in front of it compact(tolist(local…)).

What am I missing?

FWIW, My work around is to do this:

locals {
  xx = [for i in local.policies_for_this_account : i if i != null]
}

which works - but I really am curious why I can’t use compact() here.

Whats your terraform version?
I saw on the compact function page that the null value started being handled from version 1.5.x.

If your version is higher, could you share your local.policies_for_this_account?

I’m assuming it’s:

policies_for_this_account = [
  "DeveloperAccess",
  null
]

I’m running 1.9. You are correct in the value of local.policies.

This probably won’t mean much but here’s the local definition…

  permission_sets_for_this_account = distinct(compact(flatten([for g in keys(var.permission_assignments) : [for ps in keys(coalesce(var.permission_assignments[g].custom, {})) : contains(var.permission_assignments[g].custom[ps], var.account_name) ? ps : null]])))
  policies_for_this_account0 = flatten([for ps in local.permission_sets_for_this_account : var.custom_permission_sets[ps].custom_policy_templates])
  # I've no idea why I cannot use the compact() function here, but evidently I can't
  policies_for_this_account = [for i in local.policies_for_this_account0 : i if i != null]

Without seeing the code, the only way I reproduced the error was by setting policies_for_this_account to [ "DeveloperAccess", [ null ] ].

Even so, this setup doesn’t match the if you sent: [for i in local.policies_for_this_account : i if i != null]. Weird.

Please, open the terraform console and run type(local.policies_for_this_account) to check how Terraform is interpreting this variable.