Skip to content

Commit 83538fd

Browse files
barrettclarkchrisarcand
authored andcommitted
Shift errors to error file, improve error text
1 parent 9857097 commit 83538fd

3 files changed

Lines changed: 49 additions & 42 deletions

File tree

‎internal/cloud/backend.go‎

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -784,25 +784,15 @@ func (b *Cloud) VerifyWorkspaceTerraformVersion(workspaceName string) tfdiags.Di
784784
// Even if ignoring version conflicts, it may still be useful to call this
785785
// method and warn the user about a mismatch between the local and remote
786786
// Terraform versions.
787-
severity := tfdiags.Error
788-
if b.ignoreVersionConflict {
789-
severity = tfdiags.Warning
790-
}
791-
suggestion := " If you're sure you want to upgrade the state, you can force Terraform to continue using the -ignore-remote-version flag. This may result in an unusable workspace."
792-
if b.ignoreVersionConflict {
793-
suggestion = ""
794-
}
795-
796787
remoteVersion, err := version.NewSemver(workspace.TerraformVersion)
797788
if err != nil {
789+
log.Printf("[DEBUG] Invalid Terraform version (%s); will try to parse as version constraint", workspace.TerraformVersion)
790+
}
791+
if remoteVersion == nil {
798792
// If it's not a valid version, it might be a valid version constraint:
799793
remoteConstraint, err := version.NewConstraint(workspace.TerraformVersion)
800794
if err != nil {
801-
diags = diags.Append(tfdiags.Sourceless(
802-
tfdiags.Error,
803-
"Error looking up workspace",
804-
fmt.Sprintf("Invalid Terraform version or version constraint: %s", err),
805-
))
795+
diags = diags.Append(terraformInvalidVersionOrConstraint(b.ignoreVersionConflict, workspace.TerraformVersion))
806796
return diags
807797
}
808798

@@ -817,19 +807,7 @@ func (b *Cloud) VerifyWorkspaceTerraformVersion(workspaceName string) tfdiags.Di
817807
return diags
818808
}
819809

820-
diags = diags.Append(tfdiags.Sourceless(
821-
severity,
822-
"Terraform version mismatch",
823-
fmt.Sprintf(
824-
"The local Terraform version (%s) does not meet the version requirements for remote workspace %s/%s (%s).%s",
825-
tfversion.String(),
826-
b.organization,
827-
workspace.Name,
828-
workspace.TerraformVersion,
829-
suggestion,
830-
),
831-
))
832-
810+
diags = diags.Append(terraformMismatchDiagnostic(b.ignoreVersionConflict, b.organization, workspace, tfversion.String()))
833811
return diags
834812
}
835813

@@ -862,19 +840,7 @@ func (b *Cloud) VerifyWorkspaceTerraformVersion(workspaceName string) tfdiags.Di
862840
}
863841
}
864842

865-
diags = diags.Append(tfdiags.Sourceless(
866-
severity,
867-
"Terraform version mismatch",
868-
fmt.Sprintf(
869-
"The local Terraform version (%s) does not match the configured version for remote workspace %s/%s (%s).%s",
870-
tfversion.String(),
871-
b.organization,
872-
workspace.Name,
873-
workspace.TerraformVersion,
874-
suggestion,
875-
),
876-
))
877-
843+
diags = diags.Append(terraformMismatchDiagnostic(b.ignoreVersionConflict, b.organization, workspace, tfversion.String()))
878844
return diags
879845
}
880846

‎internal/cloud/backend_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ func TestCloud_VerifyWorkspaceTerraformVersion_workspaceErrors(t *testing.T) {
705705
if len(diags) != 1 {
706706
t.Fatal("expected diag, but none returned")
707707
}
708-
if got := diags.Err().Error(); !strings.Contains(got, "Error looking up workspace: Invalid Terraform version") {
708+
if got := diags.Err().Error(); !strings.Contains(got, "Terraform version error: The remote workspace specified") {
709709
t.Fatalf("unexpected error: %s", got)
710710
}
711711
}
@@ -760,7 +760,7 @@ func TestCloud_VerifyWorkspaceTerraformVersion_ignoreFlagSet(t *testing.T) {
760760
if got, want := diags[0].Description().Summary, "Terraform version mismatch"; got != want {
761761
t.Errorf("wrong summary: got %s, want %s", got, want)
762762
}
763-
wantDetail := "The local Terraform version (0.14.0) does not match the configured version for remote workspace hashicorp/app-prod (0.13.5)."
763+
wantDetail := "The local Terraform version (0.14.0) does not meet the version requirements for remote workspace hashicorp/app-prod (0.13.5)."
764764
if got := diags[0].Description().Detail; got != wantDetail {
765765
t.Errorf("wrong summary: got %s, want %s", got, wantDetail)
766766
}

‎internal/cloud/errors.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cloud
22

33
import (
44
"fmt"
5+
"strings"
56

7+
tfe "github.com/hashicorp/go-tfe"
68
"github.com/hashicorp/terraform/internal/tfdiags"
79
"github.com/zclconf/go-cty/cty"
810
)
@@ -29,3 +31,42 @@ var (
2931
cty.Path{cty.GetAttrStep{Name: "workspaces"}},
3032
)
3133
)
34+
35+
func terraformMismatchDiagnostic(ignoreVersionConflict bool, organization string, workspace *tfe.Workspace, tfversion string) tfdiags.Diagnostic {
36+
severity := tfdiags.Error
37+
if ignoreVersionConflict {
38+
severity = tfdiags.Warning
39+
}
40+
41+
suggestion := "If you're sure you want to upgrade the state, you can force Terraform to continue using the -ignore-remote-version flag. This may result in an unusable workspace."
42+
if ignoreVersionConflict {
43+
suggestion = ""
44+
}
45+
46+
description := fmt.Sprintf(
47+
"The local Terraform version (%s) does not meet the version requirements for remote workspace %s/%s (%s).\n\n%s",
48+
tfversion,
49+
organization,
50+
workspace.Name,
51+
workspace.TerraformVersion,
52+
suggestion,
53+
)
54+
description = strings.TrimSpace(description)
55+
return tfdiags.Sourceless(severity, "Terraform version mismatch", description)
56+
}
57+
58+
func terraformInvalidVersionOrConstraint(ignoreVersionConflict bool, tfversion string) tfdiags.Diagnostic {
59+
severity := tfdiags.Error
60+
if ignoreVersionConflict {
61+
severity = tfdiags.Warning
62+
}
63+
64+
suggestion := "If you're sure you want to upgrade the state, you can force Terraform to continue using the -ignore-remote-version flag. This may result in an unusable workspace."
65+
if ignoreVersionConflict {
66+
suggestion = ""
67+
}
68+
69+
description := fmt.Sprintf("The remote workspace specified an invalid Terraform version or version constraint: %s\n\n%s", tfversion, suggestion)
70+
description = strings.TrimSpace(description)
71+
return tfdiags.Sourceless(severity, "Terraform version error", description)
72+
}

0 commit comments

Comments
 (0)