minamijoyo / tfupdate

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

Version update of HCL files #96

Closed jfut closed 1 year ago

jfut commented 1 year ago

Hi, since v0.7.0 of tfupdate, version updates within HCL files no longer work. It was working correctly in tfupdate v0.6.8.

Test file:

$ cat test.hcl
terraform {
  required_version = "1.5.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.3.0"
    }
  }
}

Executing the updates:

$ tfupdate terraform -v 1.5.2 test.hcl
$ tfupdate provider aws -v 5.7.0 test.hcl

Actual result:

$ cat test.hcl
terraform {
  required_version = "1.5.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.3.0"
    }
  }
}

Expected result:

With tfupdate v0.6.8, the result would be as follows.

$ cat test.hcl
terraform {
  required_version = "1.5.2"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.7.0"
    }
  }
}

We would like to manage the provider file with a unified .hcl extension because we are using the Terragrunt generate block to read various HCL files.

minamijoyo commented 1 year ago

Hmmm, this is an odd behavior I hadn't noticed. I thought it was only processing .tf.

Since v0.7.0, to support .terraform.lock.hcl, I had to load the new .hcl extension as well. Since the schema definitions of the provider block in .terraform.lock.hcl and the provider block in .tf were different, the filename was the only clue to distinguish the schemas. Therefore, while expanding the range of filenames to be processed, I added a check of the filename in each updater.

In v0.6.8, I thought that only .tf files were updated, but it was the wrong assumption. v0.6.8 checked the file extension if the target was a directory but did not check the file extension if the filename was directly specified. https://github.com/minamijoyo/tfupdate/blob/v0.6.8/tfupdate/file.go#L87-L91 https://github.com/minamijoyo/tfupdate/blob/v0.6.8/tfupdate/file.go#L114

In v0.7.0, each updater checks the filename and ignores files other than .tf. https://github.com/minamijoyo/tfupdate/blob/v0.7.0/tfupdate/provider.go#L40-L43

Strictly speaking, the .hcl extension is too generic to determine the application schema. Therefore, processing directories is risky and potentially collapses files of other applications. However, when you specify the filename, not the directory, you intend to update it, so we should accept it whenever possible. Fortunately or unfortunately, the filename of the .terraform.lock.hcl file cannot be changed, so it is safe to check this more strictly.

Even though this was an unintended use for me, I will treat it as a regression and fix it. Thanks!

minamijoyo commented 1 year ago

@jfut Fixed it in v0.7.2 :shipit:

jfut commented 1 year ago

@minamijoyo Thank you for fixing it. I have confirmed that it is working now.