-
-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tfenv] terraform_fmt fails with tfenv and "min-required" version #508
Comments
That's a https://github.com/tfutils/tfenv bug. Please open the issue to them.
|
Respectfully, I do not believe this to be a bug in When this plugin runs, it changes the current working directory successively to each sub-directory of the repository with changed files before running Even if At the very least, this plugin should use a consistent version of Terraform throughout. In my opinion, this plugin should not change working directory into each sub-directory containing changed files, but instead pass as arguments to |
As I said above,
|
Yes, When In my example configuration, the The problem occurs when you change directories into the included sub-module. This is no longer the configuration root, and this included module does not specify an exact version of Terraform, but rather, a minimum viable version of Terraform that the current module is compatible with. So from the repo root, But if you So from that point of view, this issue doesn’t really have anything to do with What I am suggesting here is that perhaps the |
Step 2.1. |
What are you suggesting needs to be changed about my sample configuration above in order to work properly? |
Okay, let's imagine that Firstly, did you see the hooks code? You propose or change how works mostly all hooks here: pre-commit-terraform/hooks/_common.sh Lines 229 to 238 in e1d78ae
Or make Also,
Open bug report to
terraform {
required_version = "~> 1.0"
}
|
Hmm, that could not work too, please check. No idea, don't use |
Describe the bug
tfenv
allows for the use ofmin-required
as the version specified in a project's.terraform-version
file, which then parses the Terraformrequired_version
from the configuration files.The
terraform_fmt
hook runsterraform fmt
using the directory of the changed file as the working directory, rather than the repository root as the working directory.In a configuration that includes sub-modules as sub-directories, where that sub-module requires a different, less-restrictive version of Terraform, the
terraform_fmt
hook then attempts to install the wrong version of Terraform by installing the minimum version for that sub-module, rather than the version for that project.For example, I have a root configuration that uses Terraform 1.4.4, and a sub-module which requires Terraform
>= 0.14
, which 1.4.4 satisfies. However, when files in the sub-module change,terraform_fmt
failed becausetfenv
is attempting to install Terraform 0.14, and on my M1 Apple Silicon Mac, this fails, since there is no ARM build of Terraform 0.14.How can we reproduce it?
Observe the following sample configuration:
main.tf
modules/sub/main.tf
The repository root also includes the following files:
.terraform-version
.pre-commit-config.yaml
With the above configuration in a clean git working tree:
The following error is produced:
Environment information
uname -a
output:$ uname -a Darwin Thor 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64
.pre-commit-config.yaml
Proposed Solution
The
terraform_fmt
hook appears to receive an array of files which have changed and should be formatted. Instead of dropping into each containing directory to runterraform fmt
, runterraform fmt
from the repository root and pass the relative path of each file as an argument toterraform fmt
.For example, do this:
And not:
terraform fmt cd modules/sub terraform fmt
This will allow
tfenv
to detect the Terraformrequired_version
specified at the root instead of in themodules/sub
module.The text was updated successfully, but these errors were encountered: