Terraform v0.11.7
locals {
test1 = ""
test2 = ""
test3 = ""
test4 = ""
test5 = ""
test6 = ""
# Something regarding variables 7 to 9.
test7 = [
"",
]
test8 = [
"",
]
test9 = [
"",
]
test10 = ""
test11 = ""
test12 = ""
test13 = ""
test14 = ""
test15 = ""
}
After running terraform fmt, not much (if anything) should change in the above configuration.
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.
something.tf file.terraform fmtThis 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!
I'm not sure if it's related, but the other fmt issues referring to comments I could find are #17436, #16199, and #14576.
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.
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 fmtfunctionality. 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!