Terraform v0.12.0-beta2
Given a v0.11 config file "variables.tf"
variable "security_group_source_sgs" {
type = "list"
description = "Permitted sources of traffic for this Aurora cluster by security group ID."
default = [
"sg-00000000000000000", # Traffic from resource a
"sg-11111111111111111", # Traffic from resource b
"sg-22222222222222222", # Traffic from resource c
]
}
N/A
N/A
terraform 0.12upgrade retains comments following individual list items as it does for other attributes:
variable "security_group_source_sgs" {
type = list(string)
description = "Permitted sources of traffic for this Aurora cluster by security group ID."
default = [
"sg-00000000000000000", # Traffic from resource a
"sg-11111111111111111", # Traffic from resource b
"sg-22222222222222222", # Traffic from resource c
]
}
terraform 0.12upgrade strips/removes the comments after each individual list item:
variable "security_group_source_sgs" {
type = list(string)
description = "Permitted sources of traffic for this Aurora cluster by security group ID."
default = [
"sg-00000000000000000",
"sg-11111111111111111",
"sg-22222222222222222",
]
}
This impairs human readability and understanding of what each individual list item means. In our example, if we wanted to remove default access from "resource b", we now have to return to the AWS console and figure out what each security group is.
terraform 0.12upgradeThis only appears to occur for individual items in a list; other comments after key/value attributes are formatted with a single leading space.
None
Hi,
What's the initial example of the config that you need to format?
Trying to reproduce it leads to the "Actual Behaviour" you listed (copy-pasting or modifying the config randomly with tabs or spaces of the comments for the list items)
Apologies - it isn't terraform fmt that strips the comments, it's terraform 0.12upgrade that does it.
I've updated the issue description and body to match.
Thanks for reporting this, @rts-rob!
The upgrade program is built on a similar basis as terraform fmt from Terraform 0.11, and so it inherits some of its limitations for tracking comments (caused by the structure of the underlying HCL syntax tree) but it should be able to preserve comments at least as well as terraform fmt does, so we'll need to dig in here and find out where in the AST those comments are saved and make sure the upgrade program can place them like terraform fmt would.
(Terraform 0.12 has a new implementation of terraform fmt that works differently, and one of the improvements of it is that it is able to preserve comments exactly where they are in relation to other content. But 0.12upgade is constrained by the 0.11 capabilities of terraform fmt because it must use the old parser in order to be able to successfully read configurations the way Terraform 0.11 would have.)
Thanks for looking into this! b/132334044
I'm guessing you're already aware since the issue remains open, but just confirming that this issue is still relevant in 0.12-rc1.
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, @rts-rob!
The upgrade program is built on a similar basis as
terraform fmtfrom Terraform 0.11, and so it inherits some of its limitations for tracking comments (caused by the structure of the underlying HCL syntax tree) but it should be able to preserve comments at least as well asterraform fmtdoes, so we'll need to dig in here and find out where in the AST those comments are saved and make sure the upgrade program can place them liketerraform fmtwould.(Terraform 0.12 has a new implementation of
terraform fmtthat works differently, and one of the improvements of it is that it is able to preserve comments exactly where they are in relation to other content. But0.12upgadeis constrained by the 0.11 capabilities ofterraform fmtbecause it must use the old parser in order to be able to successfully read configurations the way Terraform 0.11 would have.)