diff --git a/go.mod b/go.mod index c91bde89a698..6b6c618e146c 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.0 github.com/hashicorp/go-tfe v1.0.0 github.com/hashicorp/go-uuid v1.0.2 - github.com/hashicorp/go-version v1.3.0 + github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f github.com/hashicorp/hcl/v2 v2.13.0 github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2 diff --git a/go.sum b/go.sum index f0386fa2c128..1554c06a54f0 100644 --- a/go.sum +++ b/go.sum @@ -402,8 +402,9 @@ github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw= github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/internal/terraform/context_test.go b/internal/terraform/context_test.go index d1926a8803fe..52a254b0b1e1 100644 --- a/internal/terraform/context_test.go +++ b/internal/terraform/context_test.go @@ -67,6 +67,22 @@ func TestNewContextRequiredVersion(t *testing.T) { false, }, + { + "prerelease doesn't match with inequality", + "", + "0.8.0", + "> 0.7.0-beta", + true, + }, + + { + "prerelease doesn't match with equality", + "", + "0.7.0", + "0.7.0-beta", + true, + }, + { "module matches", "context-required-version-module", diff --git a/internal/terraform/version_required.go b/internal/terraform/version_required.go index f14d93f681d3..1861050b9573 100644 --- a/internal/terraform/version_required.go +++ b/internal/terraform/version_required.go @@ -27,6 +27,29 @@ func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics { module := config.Module for _, constraint := range module.CoreVersionConstraints { + // Before checking if the constraints are met, check that we are not using any prerelease fields as these + // are not currently supported. + var prereleaseDiags tfdiags.Diagnostics + for _, required := range constraint.Required { + if required.Prerelease() { + prereleaseDiags = prereleaseDiags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid required_version constraint", + Detail: fmt.Sprintf( + "Prerelease version constraints are not supported: %s. Remove the prerelease information from the constraint. Prerelease versions of terraform will match constraints using their version core only.", + required.String()), + Subject: constraint.DeclRange.Ptr(), + }) + } + } + + if len(prereleaseDiags) > 0 { + // There were some prerelease fields in the constraints. Don't check the constraints as they will + // fail, and populate the diagnostics for these constraints with the prerelease diagnostics. + diags = diags.Append(prereleaseDiags) + continue + } + if !constraint.Required.Check(tfversion.SemVer) { switch { case len(config.Path) == 0: