From 5220c4c7ff72109aac41b8134024ee3e4ec1fe56 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 28 Jun 2022 13:09:17 +0100 Subject: [PATCH 1/5] Fail global required_version check if it contains any prerelease fields --- go.mod | 2 +- go.sum | 2 ++ internal/terraform/version_required.go | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) 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..26cb3aec9eab 100644 --- a/go.sum +++ b/go.sum @@ -404,6 +404,8 @@ github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 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/version_required.go b/internal/terraform/version_required.go index f14d93f681d3..6a92c21e5205 100644 --- a/internal/terraform/version_required.go +++ b/internal/terraform/version_required.go @@ -27,6 +27,25 @@ 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. + for _, required := range constraint.Required { + if required.Prerelease() { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid required_version constraint", + Detail: fmt.Sprintf("Prerelease version constraints are not supported: %s.", required.String()), + Subject: constraint.DeclRange.Ptr(), + }) + } + } + + if len(diags) > 0 { + // There were some prerelease fields in the constraints. Don't check the constraints as they will + // fail, and the diagnostics are already populated. + continue + } + if !constraint.Required.Check(tfversion.SemVer) { switch { case len(config.Path) == 0: From df90f749bf403947192d0c3812739004f3dad4e1 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 28 Jun 2022 13:19:58 +0100 Subject: [PATCH 2/5] go mod tidy --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index 26cb3aec9eab..1554c06a54f0 100644 --- a/go.sum +++ b/go.sum @@ -402,7 +402,6 @@ 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= From 6f423c1688caaa5ab715fc3eb9e9171f45b80eb5 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 28 Jun 2022 13:53:42 +0100 Subject: [PATCH 3/5] Improve required_version prerelease not supported error string --- internal/terraform/version_required.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/terraform/version_required.go b/internal/terraform/version_required.go index 6a92c21e5205..9bcab0ce87d5 100644 --- a/internal/terraform/version_required.go +++ b/internal/terraform/version_required.go @@ -34,8 +34,10 @@ func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics { diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Invalid required_version constraint", - Detail: fmt.Sprintf("Prerelease version constraints are not supported: %s.", required.String()), - Subject: constraint.DeclRange.Ptr(), + 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(), }) } } From b5e1794537155f2de840c0552221382084db4cfb Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 28 Jun 2022 15:49:14 +0100 Subject: [PATCH 4/5] Add prerelease version constraint unit tests --- internal/terraform/context_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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", From 2b5fb66ae2dd2dd31e91139c2c7570490dff9f89 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Wed, 29 Jun 2022 14:36:56 +0100 Subject: [PATCH 5/5] Fix side-effects by populating global diags too soon --- internal/terraform/version_required.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/terraform/version_required.go b/internal/terraform/version_required.go index 9bcab0ce87d5..1861050b9573 100644 --- a/internal/terraform/version_required.go +++ b/internal/terraform/version_required.go @@ -29,9 +29,10 @@ func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics { 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() { - diags = diags.Append(&hcl.Diagnostic{ + prereleaseDiags = prereleaseDiags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Invalid required_version constraint", Detail: fmt.Sprintf( @@ -42,9 +43,10 @@ func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics { } } - if len(diags) > 0 { + if len(prereleaseDiags) > 0 { // There were some prerelease fields in the constraints. Don't check the constraints as they will - // fail, and the diagnostics are already populated. + // fail, and populate the diagnostics for these constraints with the prerelease diagnostics. + diags = diags.Append(prereleaseDiags) continue }