hcl icon indicating copy to clipboard operation
hcl copied to clipboard

Feature Request: Multiline condition support like in bash or python

Open dylanf9797 opened this issue 5 months ago • 2 comments

Hi

Currently, multiline condition is not supported and i think it can be good to have this feature.

Let's take an example

this works using Bash test.sh

[ -f /tmp/a ] && \
  [ -f /tmp/b ] && { echo yes; } \
  || echo no

and this works in python: test.py

t = 1
s = "hellos"

if 2 > t and \
        s == "hello":
    print("ok")
else:
    print("no")

But this not works in terraform for example:

# variables.tf

variable "hello" {
  type = string
  validation {
    condition = length(var.hello) > 0 && \
      length(var.hello) < 128

    error_message = "NO"
  }
}

dylanf9797 avatar Sep 17 '25 17:09 dylanf9797

Hi @dylanf9797,

In HCL, the syntax for splitting an expression over multiple lines is to write it in some kind of brackets, which HCL then uses to understand where the multi-line expression ends.

While various kinds of bracket-shaped language features can work, the simplest answer is to use parentheses because they don't cause any other effect other than allowing the expression to wrap:

variable "hello" {
  type = string

  validation {
    condition = (
      length(var.hello) > 0 &&
      length(var.hello) < 128
    )
    error_message = "NO"
  }
}

When HCL finds the opening ( it expects the expression to continue until the matching ), ignoring any newline characters that appear in between.

I showed this as a validation condition example because that was what you asked about, but note that this is a general-purpose mechanism that works for expressions anywhere in HCL; it's not actually specific to validation conditions.

apparentlymart avatar Sep 17 '25 18:09 apparentlymart

Yes, we can use this syntax but maybe the another syntax can be added if it's considered useful for most people, otherwise, this issue can be closed.

dylanf9797 avatar Sep 17 '25 19:09 dylanf9797