terraform
terraform copied to clipboard
Formatter should handle multiline function call with closing parenthesis on same line as last argument
Given this code:
locals {
example = format("hello, %s! this is an %s of %s doing weird stuff",
"terraform",
"example",
"format")
}
I would have expected terraform fmt to indent "format") to the same level as "example",, but the snippet above is the formatted output.
If I add a newline before the ending parenthesis, the rationale behind this becomes a little more obvious - the formatted document now looks like this:
locals {
example = format("hello, %s! this is an %s of %s doing weird stuff",
"terraform",
"example",
"format"
)
}
It would be nice if the formatter knew to insert that newline, so I don't have to. It took me a while to figure out what I had to do to make this code format nicely 😄
Hi @tomasaschan! Thanks for sharing this use-case.
terraform fmt is currently only makes changes to horizontal spacing (indentation and alignment) and leaves vertical spacing (newlines) untouched, so it's working as expected here. We don't have any short-term plans to introduce more opinionated formatting rules into terraform fmt because there's a lot more scope for those rules to make things worse rather than better (as was often true in the Terraform 0.11 terraform fmt)
With that said, we'll keep this issue here to record the use-case in case we reconsider that position in a future version of Terraform. That is likely to be only after the language has been stable for a while and thus there's a chance for formatting idioms to have formed around the new Terraform 0.12 language features.
@teamterraform Ah, go it! Then, may I suggest that this snippet should format like this instead?
locals {
example = format("hello, %s! this is an %s of %s doing weird stuff",
"terraform",
"example",
"format")
}
In other words, arguments in a multi-line function call should be aligned, including the last one, even if it has the function call's parenthesis on the last line instead of on a new one.
Another related case to watch out for if/when implementing this:
locals {
example = format("hello, %s! this is an %s of %s doing weird stuff",
"terraform",
"example",
some_func("format")
)
}
i.e. the fact that the last argument line ends with ) is not enough to know that the function call ends there.