Skip to main content
compact(list) returns a new list with all empty string and null elements removed.

Signature

compact(list list(string)) list(string)

Example

Build a list of Docker build arguments, where some are optional. Empty entries are filtered out before the list is joined into a command:
variable "build_arg_proxy" {
  description = "HTTP proxy for Docker builds (optional)"
  type        = string
  default     = ""
}

variable "build_arg_no_proxy" {
  description = "No-proxy list for Docker builds (optional)"
  type        = string
  default     = ""
}

computed "build_args" {
  description = "Non-empty Docker build arguments"
  expression  = compact([
    var.build_arg_proxy != "" ? "--build-arg HTTP_PROXY=${var.build_arg_proxy}" : "",
    var.build_arg_no_proxy != "" ? "--build-arg NO_PROXY=${var.build_arg_no_proxy}" : "",
  ])
}

task "build" {
  description = "Build the Docker image with optional proxy settings"
  commands    = ["docker build ${join(" ", computed.build_args)} ."]
}
When neither proxy variable is set, computed.build_args is empty and the command becomes docker build ..

Notes

  • compact only operates on list(string). It does not apply to lists of other types.
  • Use compact after producing a list where some elements may be conditionally empty.