Terraform: 'terraform fmt' removes all blank lines after lone full-line comments

Created on 1 Jul 2018  ยท  3Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.11.7

Terraform Configuration Files

locals {
  test1 = ""
  test2 = ""
  test3 = ""

  test4 = ""
  test5 = ""
  test6 = ""

  # Something regarding variables 7 to 9.

  test7 = [
    "",
  ]

  test8 = [
    "",
  ]

  test9 = [
    "",
  ]

  test10 = ""
  test11 = ""
  test12 = ""

  test13 = ""
  test14 = ""
  test15 = ""
}

Expected Behavior

After running terraform fmt, not much (if anything) should change in the above configuration.

Actual Behavior

After running terraform fmt, all the blank lines after the lone full-line comment get removed:

locals {
  test1 = ""
  test2 = ""
  test3 = ""

  test4 = ""
  test5 = ""
  test6 = ""

  # Something regarding variables 7 to 9.

  test7 = [
    "",
  ]
  test8 = [
    "",
  ]
  test9 = [
    "",
  ]
  test10 = ""
  test11 = ""
  test12 = ""
  test13 = ""
  test14 = ""
  test15 = ""
}

Note that if the comment is instead 'attached' to the top of test7, like this:

  # Something regarding variables 7 to 9.
  test7 = [
    "",
  ]

then the rest of the whitespace is _not_ removed. Conversely, with the comment 'attached', the whitespace between the lists is actually returned back to how it was when re-running fmt.

Steps to Reproduce

  1. Copy the example configuration at the top of this issue into an otherwise blank something.tf file.
  2. Run terraform fmt
  3. Observe that the blank lines after the comment get removed.

Additional Context

This is not a massive issue; it just gets annoying every now and then. I like to fmt my Terraform files, and this feels like a parsing bug of sorts because it only occurs when a comment is on it's own - i.e. not 'attached' to the next line. In addition, the issue results in a large file, with appropriate use of whitespace, becoming very messy after fmt'ing it.

Note that this occurs with // comments, # comments, and /* ..... */ comments (all whether multiline or not).

This isn't specific to locals {} blocks - it happens inside any block.

Also, I know from comments on one of the referenced issues below that there's a new language parser coming; this is just another test case to help with future and I realise it's not something that would be worked on separately at the moment!

References

I'm not sure if it's related, but the other fmt issues referring to comments I could find are #17436, #16199, and #14576.

bug config

Most helpful comment

Thanks for reporting this odd behavior, @tdmalone!

In our development branch for the next major release we have made significant changes to the configuration language parser, which includes a new version of the terraform fmt functionality. As it currently stands it's less "capable" than the current implementation, in that it makes fewer formatting changes, but as a result of that I expect that it will actually fix this bug since it no longer removes blank lines from a file in any case.

We'll use this issue as a reminder to test and verify my statement above once the development branch is merged down into master. Thanks again for reporting this!

All 3 comments

Thanks for reporting this odd behavior, @tdmalone!

In our development branch for the next major release we have made significant changes to the configuration language parser, which includes a new version of the terraform fmt functionality. As it currently stands it's less "capable" than the current implementation, in that it makes fewer formatting changes, but as a result of that I expect that it will actually fix this bug since it no longer removes blank lines from a file in any case.

We'll use this issue as a reminder to test and verify my statement above once the development branch is merged down into master. Thanks again for reporting this!

Hi @tdmalone!

I just verified that in v0.12.0-alpha2 terraform fmt isn't changing your input file at all, as expected. This fix is already in master and will be included in the forthcoming v0.12.0 release, so I'm going to close this.

Thanks again for reporting this!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

c4milo picture c4milo  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments

thebenwaters picture thebenwaters  ยท  3Comments

rjinski picture rjinski  ยท  3Comments