Skip to content
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

v0.12: Terraform Version Check Ignored depending on location #21420

Closed
bernadinm opened this issue May 23, 2019 · 15 comments
Closed

v0.12: Terraform Version Check Ignored depending on location #21420

bernadinm opened this issue May 23, 2019 · 15 comments
Labels
bug config v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@bernadinm
Copy link

bernadinm commented May 23, 2019

When using terraform v0.12, it fails to perform the check depending if the condition lives in the main.tf or any other *.tf

Terraform Version

$ terraform -v
Terraform v0.12.0

Terraform Configuration Files

$ cat meta.tf
terraform {
 required_version = "> 0.12.0"
}
$ cat main.tf
#terraform {
#  required_version = "< 0.12.0"
#}

output "a.b.c_unsupported" {
  value = "1.2.3"
}

Actual Behavior

terraform init
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

Error: Invalid output name

  on main.tf line 9, in output "a.b.c_unsupported":
   9: output "a.b.c_unsupported" {

A name must start with a letter and may contain only letters, digits,
underscores, and dashes.

Expected Behavior

terraform init

Error: Unsupported Terraform Core version

This configuration does not support Terraform version 0.12.0. To proceed,
either choose another supported Terraform version or update the root module's
version constraint. Version constraints are normally set for good reason, so
updating the constraint may lead to other errors or unexpected behavior.

Steps to Reproduce

  1. terraform init
@bernadinm
Copy link
Author

Observed an odd behavior that if you remove the meta.tf and have a main.tf it works around the issue.

Terraform Configuration Files

$ rm meta.tf

$ cat main.tf
terraform {
  required_version = "< 0.12.0"
}

output "a.b.c_unsupported" {
  value = "1.2.3"
}

Actual Behavior

terraform init
Error: Unsupported Terraform Core version

This configuration does not support Terraform version 0.12.0. To proceed,
either choose another supported Terraform version or update the root module's
version constraint. Version constraints are normally set for good reason, so
updating the constraint may lead to other errors or unexpected behavior.

@bernadinm
Copy link
Author

Unfortunately, this trick above doesn't work with nested modules.

@apparentlymart
Copy link
Member

Hi @bernadinm!

In your first example you showed a version constraint of > 0.12.0. Was that intentional? Based on everything else you've shown here, it looks like you were trying to write a version constraint to exclude Terraform 0.12.0 and later.

@bernadinm
Copy link
Author

bernadinm commented May 24, 2019 via email

@apparentlymart
Copy link
Member

Thanks for the clarification. In that case, shouldn't the version constraint in your meta.tf actually be < 0.12.0, to match with what you added to main.tf in the later example?

@bernadinm
Copy link
Author

bernadinm commented May 24, 2019

Thanks for the catch. You are correct. However regardless of the condition, it still ignores the required_version check. Here is a single command to help repo:

mkdir -p /tmp/repo/module

cat > /tmp/repo/module/main.tf <<EOF
terraform {
  required_version = "< 0.12.0"
}

output "a.b.c_unsupported" {
  value = "1.2.3"
}
EOF

cat > /tmp/repo/main.tf <<EOF
module "test" {
source = "./module"
}
EOF

cd /tmp/repo/
terraform init

@apparentlymart
Copy link
Member

Thanks!

The Terraform version constraint handling tries its best to ignore errors during loading so that it can detect this situation, but it seems like it's not trying quite hard enough to deal with it here. I suspect it's handling the check on a per-file basis rather than on a whole-module basis, and so it only works if the error is in the same file as the version constraint; instead, it should visit all of the files first to sniff for version constraints, and only then move on to actually trying to load them fully.

@bernadinm
Copy link
Author

It looks like it is even ignoring the required_version even if it's within the same module. You can see this here as well with this example:

mkdir -p /tmp/repo/module

cat > /tmp/repo/module/main.tf <<EOF
terraform {
  required_version = "< 0.12.0"
}

output "a.b.c_unsupported" {
  value = "1.2.3"
}
EOF

cat > /tmp/repo/main.tf <<EOF
terraform {
  required_version = "< 0.12.0"
}

module "test" {
source = "./module"
}
EOF

cd /tmp/repo/
terraform init

Actual Output

$ terraform init
Initializing modules...
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

Error: Invalid output name

  on module/main.tf line 5, in output "a.b.c_unsupported":
   5: output "a.b.c_unsupported" {

A name must start with a letter and may contain only letters, digits,
underscores, and dashes.
$ terraform -v
Terraform v0.12.0

@sudoforge
Copy link

sudoforge commented May 25, 2019

The original content of this comment was reporting a separate issue, and has been filed with the upstream library (hashicorp/go-version#55) responsible for version comparison

@apparentlymart
Copy link
Member

Hi @sudoforge!

Thanks for reporting that, but it seems like a separate problem, so could you please open a new issue for it? Terraform uses an upstream library to handle those, so it's possible that it had an undocumented behavior change that we inherited, but the solution to that will be independent of fixing the handling of the required_version attribute.

@sudoforge
Copy link

@apparentlymart You're absolutely right. My apologies, I had several different tabs open with various issues and must have gotten my wires crossed.

@hashibot hashibot added the v0.12 Issues (primarily bugs) reported against v0.12 releases label Aug 22, 2019
@Misio942
Copy link

good afternoon, I solve the problem by adding to require_version = "<= 0.12.19"

@scardena
Copy link

scardena commented Apr 7, 2020

In my case it worked by adding
required_version = ">= 0.12.14" (I had installed Terraform v0.12.24)

@mildwonkey
Copy link
Contributor

I am going to close this issue due to inactivity, and it looks like the questions have been answered. The underlying code paths have changed for terraform v0.13, so if you still see an issue when using the latest version of terraform, please open a new issue.

Thanks!

@ghost
Copy link

ghost commented Oct 13, 2020

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.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

7 participants