hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.64k stars 9.41k forks source link

terraform fmt add spaces after % #22356

Closed d-helios closed 4 years ago

d-helios commented 4 years ago

terraform fmt command insert space (" ") character in inline template

Terraform Version

Terraform v0.12.6

Terraform Configuration Files

variable "IN" {
  default = [[
    "a",
    "b",
    ],
    ["c",
      "d"
  ]]
}

locals {
  OUT = join(";", [for i in var.IN : "start_${index(var.IN, i)},${join(",", i)},next_%{if index(var.IN, i) + 1 != length(var.IN)}${index(var.IN, i) + 1}%{else}END%{endif}"])

}

output "default" {
  value = local.OUT
}

Expected Behavior

terraform fmt doesn't insert any additional character in inline templates (string)

Actual Behavior

~/t/awef > cp main.tf /tmp/
~/t/awef > terraform fmt
main.tf
~/t/awef > diff /tmp/main.tf main.tf
12c12
<   OUT = join(";", [for i in var.IN : "start_${index(var.IN, i)},${join(",", i)},next_%{if index(var.IN, i) + 1 != length(var.IN)}${index(var.IN, i) + 1}%{else}END%{endif}"])
---
>   OUT = join(";", [for i in var.IN : "start_${index(var.IN, i)},${join(",", i)},next_%{if index(var.IN, i) + 1 != length(var.IN)}${index(var.IN, i) + 1} %{else}END%{endif}"])

Steps to Reproduce

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

  1. terraform init
  2. cp main.tf /tmp/main.tf
  3. terraform fmt
  4. diff /tmp/main.tf main.tf

wata727 commented 4 years ago

I got a minimal reproduction code like the following:

variable "foo" {
  default = "foo"
}

foo = "%{if true}${var.foo}%{endif}"

And I created the PR to HCL repository https://github.com/hashicorp/hcl2/pull/125 Maybe it fixes the issue.

apparentlymart commented 4 years ago

(Reopening because the change in the HCL repository can't fix this until it's vendored into this repository.)

ghost commented 4 years ago

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.