diff --git a/tfexec/terraform_providers_test.go b/tfexec/terraform_providers_test.go index 90da214..924e4d4 100644 --- a/tfexec/terraform_providers_test.go +++ b/tfexec/terraform_providers_test.go @@ -2,11 +2,8 @@ package tfexec import ( "context" - "fmt" "reflect" "testing" - - "github.com/hashicorp/go-version" ) var providersStdout = ` @@ -100,18 +97,13 @@ resource "null_resource" "bar" {} t.Fatalf("failed to run terraform providers: %s", err) } - v, err := terraformCLI.Version(context.Background()) - if err != nil { - t.Fatalf("unexpected version error: %s", err) - } - - constraints, err := version.NewConstraint(fmt.Sprintf(">= %s", MinimumTerraformVersionForStateReplaceProvider)) + supportsStateReplaceProvider, _, err := terraformCLI.SupportsStateReplaceProvider(context.Background()) if err != nil { - t.Fatalf("unexpected version constraint error: %s", err) + t.Fatalf("failed to determine if Terraform version supports state replace-provider: %s", err) } want := providersStdout - if !constraints.Check(v) { + if !supportsStateReplaceProvider { want = legacyProvidersStdout } diff --git a/tfexec/terraform_state_replace_provider.go b/tfexec/terraform_state_replace_provider.go index 0c424f4..40c4eae 100644 --- a/tfexec/terraform_state_replace_provider.go +++ b/tfexec/terraform_state_replace_provider.go @@ -34,7 +34,12 @@ func (c *terraformCLI) SupportsStateReplaceProvider(ctx context.Context) (bool, return false, constraints, err } - if !constraints.Check(v) { + ver, err := truncatePreReleaseVersion(v) + if err != nil { + return false, constraints, err + } + + if !constraints.Check(ver) { return false, constraints, nil } diff --git a/tfexec/terraform_version.go b/tfexec/terraform_version.go index ea21b87..5fab486 100644 --- a/tfexec/terraform_version.go +++ b/tfexec/terraform_version.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "strings" "github.com/hashicorp/go-version" ) @@ -29,3 +30,24 @@ func (c *terraformCLI) Version(ctx context.Context) (*version.Version, error) { return version, nil } + +// truncatePreReleaseVersion is a helper function that removes +// pre-release information. +// The hashicorp/go-version returns false when comparing pre-releases, for +// example 1.6.0-rc1 >= 0.13. This is counter-intuitive for determining the +// presence or absence of a feature, so remove the pre-release information +// before comparing. +func truncatePreReleaseVersion(v *version.Version) (*version.Version, error) { + if v.Prerelease() == "" { + return v, nil + } + + vs, _, _ := strings.Cut(v.String(), "-") + + ver, err := version.NewVersion(vs) + if err != nil { + return nil, err + } + + return ver, nil +}