Skip to content

Commit d876e68

Browse files
author
Liam Cervante
authored
Fail global required_version check if it contains any prerelease fields (#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
1 parent 73c4a4c commit d876e68

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ require (
4242
github.com/hashicorp/go-retryablehttp v0.7.0
4343
github.com/hashicorp/go-tfe v1.0.0
4444
github.com/hashicorp/go-uuid v1.0.2
45-
github.com/hashicorp/go-version v1.3.0
45+
github.com/hashicorp/go-version v1.6.0
4646
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
4747
github.com/hashicorp/hcl/v2 v2.13.0
4848
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,9 @@ github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
404404
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
405405
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
406406
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
407-
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
408407
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
408+
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
409+
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
409410
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
410411
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
411412
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

internal/terraform/context_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ func TestNewContextRequiredVersion(t *testing.T) {
6767
false,
6868
},
6969

70+
{
71+
"prerelease doesn't match with inequality",
72+
"",
73+
"0.8.0",
74+
"> 0.7.0-beta",
75+
true,
76+
},
77+
78+
{
79+
"prerelease doesn't match with equality",
80+
"",
81+
"0.7.0",
82+
"0.7.0-beta",
83+
true,
84+
},
85+
7086
{
7187
"module matches",
7288
"context-required-version-module",

internal/terraform/version_required.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics {
2727
module := config.Module
2828

2929
for _, constraint := range module.CoreVersionConstraints {
30+
// Before checking if the constraints are met, check that we are not using any prerelease fields as these
31+
// are not currently supported.
32+
var prereleaseDiags tfdiags.Diagnostics
33+
for _, required := range constraint.Required {
34+
if required.Prerelease() {
35+
prereleaseDiags = prereleaseDiags.Append(&hcl.Diagnostic{
36+
Severity: hcl.DiagError,
37+
Summary: "Invalid required_version constraint",
38+
Detail: fmt.Sprintf(
39+
"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.",
40+
required.String()),
41+
Subject: constraint.DeclRange.Ptr(),
42+
})
43+
}
44+
}
45+
46+
if len(prereleaseDiags) > 0 {
47+
// There were some prerelease fields in the constraints. Don't check the constraints as they will
48+
// fail, and populate the diagnostics for these constraints with the prerelease diagnostics.
49+
diags = diags.Append(prereleaseDiags)
50+
continue
51+
}
52+
3053
if !constraint.Required.Check(tfversion.SemVer) {
3154
switch {
3255
case len(config.Path) == 0:

0 commit comments

Comments
 (0)