Skip to content

Commit

Permalink
Fail global required_version check if it contains any prerelease fiel…
Browse files Browse the repository at this point in the history
…ds (#31331)

* Fail global required_version check if it contains any prerelease fields

* go mod tidy

* Improve required_version prerelease not supported error string

* Add prerelease version constraint unit tests

* Fix side-effects by populating global diags too soon
  • Loading branch information
liamcervante committed Jun 30, 2022
1 parent 73c4a4c commit d876e68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -42,7 +42,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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Expand Up @@ -404,8 +404,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=
Expand Down
16 changes: 16 additions & 0 deletions internal/terraform/context_test.go
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions internal/terraform/version_required.go
Expand Up @@ -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:
Expand Down

0 comments on commit d876e68

Please sign in to comment.