minamijoyo / tfupdate

Update version constraints in your Terraform configurations
MIT License
556 stars 23 forks source link

tfupdate unexpectedly alters a file to a broken format #24

Closed pdecat closed 3 years ago

pdecat commented 4 years ago

Hi,

I just came across this weird behavior with one of my configuration files which I have stripped down as the following minimal reproduction case:

# terraform fmt test.tf

# cat test.tf
resource "google_container_cluster" "gke_cluster" {
  addons_config {
    network_policy_config {
      disabled = ! (length(var.gke_network_policy) > 0 && var.gke_network_policy[0].enabled)
    }
  }
}

# tfupdate terraform test.tf

# cat test.tf
resource "google_container_cluster" "gke_cluster" {
  addons_config {
    network_policy_config {
      disabled = ! (length(var.gke_network_policy) > 0 && var.gke_network_policy[0].enabled
    ) }
  }
}

# tfupdate terraform test.tf
failed to parse input: test.tf:5,7-8: Missing newline after argument; An argument definition must end with a newline.

# terraform fmt test.tf

Error: Missing newline after argument

  on test.tf line 5, in resource "google_container_cluster" "gke_cluster":
   4:       disabled = ! (length(var.gke_network_policy) > 0 && var.gke_network_policy[0].enabled
   5:     ) }

An argument definition must end with a newline.
minamijoyo commented 4 years ago

@pdecat Umm, very weird behavior. I can reproduce it. Since the fmt logic of tfupdate almost depends on hclwrite package in hcl2 library. So it may be a bug in upstream. I'll check it later. Thank you for reporting the issue!

minamijoyo commented 4 years ago

@pdecat I confirmed the bug was caused by the hclwrite parser, so I opened a new issue in upstream. https://github.com/hashicorp/hcl/issues/402

As a workaround until fix, you can exclude any files with -i (--ignore-path) option:

$ tfupdate terraform --help
Usage: tfupdate terraform [options] <PATH>

Arguments
  PATH               A path of file or directory to update

Options:
  -v  --version      A new version constraint (default: latest)
                     If the version is omitted, the latest version is automatically checked and set.
  -r  --recursive    Check a directory recursively (default: false)
  -i  --ignore-path  A regular expression for path to ignore
                     If you want to ignore multiple directories, set the flag multiple times.
pdecat commented 4 years ago

I worked it around with:

-      disabled = ! (length(var.gke_network_policy) > 0 && var.gke_network_policy[0].enabled)
+      disabled = length(var.gke_network_policy) <= 0 || ! var.gke_network_policy[0].enabled

Surprisingly, it's the only line causing an issue in my somewhat large project.

minamijoyo commented 3 years ago

@pdecat Fixed in v0.4.3 🎉