Terraform: `terraform fmt` moves comment...

Created on 13 Mar 2017  ยท  8Comments  ยท  Source: hashicorp/terraform

This is a low-priority cosmetic-ish bug, but one I keep stumbling across when writing Terraform config files and source of some friction of the tedium variety.

Terraform Version

Terraform v0.9.0-dev (aa4676e6222921f06dd451af571512679ef1dea6)

Terraform Configuration Files

variable "foo" {
  type = "list"

  default = [
    "a",
    # "b",
  ]
}

Expected Behavior

The comment should have remained inline inside of the list.

Actual Behavior

$ terraform fmt test.tf
test.tf
$ cat test.tf
variable "foo" {
  type = "list"

  default = [
    "a",
  ]

  # "b",
}

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

Step 1:

cat <<'EOF' > test.tf
variable "foo" {
  type = "list"

  default = [
    "a",
    # "b",
  ]
}
EOF

Step 2: terraform fmt test.tf

Important Factoids

The following snippet does maintain the commented item inside of the list:

variable "foo" {
  type = "list"

  default = [
    "a",
    # "b",
    "c",
  ]
}

becomes:

variable "foo" {
  type = "list"

  default = [
    "a",

    # "b",
    "c",
  ]
}

which is fine because when I uncomment # "b", and re-run terraform fmt the structure of the doc doesn't change:

variable "foo" {
  type = "list"

  default = [
    "a",
    "b",
    "c",
  ]
}
bug dependencies upstream

Most helpful comment

Hi all! Sorry for the weirdness here.

We're currently planning several changes to the language parser which will likely change the way this works, so the reason there's no movement in this area is that we expect the pretty-printer will get significantly updated as part of that work and so it's more efficient to group all that work together. We'll have more to share on this subject in future, but things are still pretty early at the moment.

Sorry for the weirdness in the mean time.

All 8 comments

I have a similar scenario with formatting lists:

variable "foo" {
  type = "list"

  default = [
    "a", # this is a
    "b", # this is b
    "c"  # this is c
  ]
}

becomes:

variable "foo" {
  type = "list"

  default = [
    "a", # this is a
    "b", # this is b
    "c",
  ] # this is c
}

It also seems odd that the last item in the list gets a comma appended to it, but very minor.

This is a real pain for us, causing issues with commenting IPs in whitelist list objects.
Major plus one for a resolution to this - still present in 0.9.11

Hi all! Sorry for the weirdness here.

We're currently planning several changes to the language parser which will likely change the way this works, so the reason there's no movement in this area is that we expect the pretty-printer will get significantly updated as part of that work and so it's more efficient to group all that work together. We'll have more to share on this subject in future, but things are still pretty early at the moment.

Sorry for the weirdness in the mean time.

One more use case.

locals {
  # comment 1
  # comment 2
}

after run terraform fmt, it becomes single line

locals {
  # comment 1  # comment 2
}

If I add a space

locals {
  # comment 1

  # comment 2
}

run terraform fmt twice, they are merged into one line again.

This still has yet to be fixed but here is a workaround for everyone that seems to work well for us.

variable "foo" {
  type = "list"

  default = [
    "a", // this is a
    "b", // this is b
    "c"  // this is c
  ]
}

Don't use # but // for comments and the fmt won't remove the last line.

Hello! :robot:

This issue relates to an older version of Terraform that is no longer in active development, and because the area of Terraform it relates to has changed significantly since the issue was opened we suspect that the issue is either fixed or that the circumstances around it have changed enough that we'd need an updated issue report in order to reproduce and address it.

If you're still seeing this or a similar issue in the latest version of Terraform, please do feel free to open a new bug report! Please be sure to include all of the information requested in the template, even if it might seem redundant with the information already shared in _this_ issue, because the internal details relating to this problem are likely to be different in the current version of Terraform.

Thanks!

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

kklipsch picture kklipsch  ยท  95Comments

lukehoersten picture lukehoersten  ยท  151Comments

mirogta picture mirogta  ยท  74Comments

atkinchris picture atkinchris  ยท  68Comments

oillio picture oillio  ยท  78Comments