Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter should handle multiline function call with closing parenthesis on same line as last argument #22558

Open
tomasaschan opened this issue Aug 22, 2019 · 2 comments
Labels
cli enhancement fmt Bug reports and feature requests related to Terraform's auto-formatter command "terraform fmt" v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@tomasaschan
Copy link

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 😄

@teamterraform
Copy link
Contributor

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.

@hashibot hashibot added the v0.12 Issues (primarily bugs) reported against v0.12 releases label Aug 22, 2019
@tomasaschan
Copy link
Author

@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.

@tomasaschan tomasaschan changed the title Formatter should insert newline before ending parenthesis in multiline function call Formatter should handle multiline function call with closing parenthesis on same line as last argument Aug 23, 2019
@apparentlymart apparentlymart added the fmt Bug reports and feature requests related to Terraform's auto-formatter command "terraform fmt" label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli enhancement fmt Bug reports and feature requests related to Terraform's auto-formatter command "terraform fmt" v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

4 participants