terraform 0.12upgrade in v0.12 beta 2 strips comments from list items

Created on 30 Apr 2019  ยท  6Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.0-beta2

Terraform Configuration Files

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
  ]
}

Debug Output

N/A

Crash Output

N/A

Expected Behavior

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
  ]
}

Actual Behavior

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.

Steps to Reproduce

  1. terraform 0.12upgrade

Additional Context

This only appears to occur for individual items in a list; other comments after key/value attributes are formatted with a single leading space.

References

None

bug cli

Most helpful comment

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

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rjinski picture rjinski  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments