From 6993ecb0a6d48c2e3011937b6655212000a2e1a0 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 3 Sep 2020 17:23:22 -0700 Subject: [PATCH] internal/getproviders: VersionConstraintsString for "~> 2" input The version constraint parser allows "~> 2", but it behavior is identical to "~> 2.0". Due to a quirk of the constraint parser (caused by the fact that it supports both Ruby-style and npm/cargo-style constraints), it ends up returning "~> 2" with the minor version marked as "unconstrained" rather than as zero, but that means the same thing as zero in this context anyway and so we'll prefer to stringify as "~> 2.0" so that we can be clearer about how Terraform is understanding that version constraint. --- internal/getproviders/types.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/getproviders/types.go b/internal/getproviders/types.go index f82af8cff..44eab6ccd 100644 --- a/internal/getproviders/types.go +++ b/internal/getproviders/types.go @@ -385,7 +385,17 @@ func VersionConstraintsString(spec VersionConstraints) string { if sel.Operator == constraints.OpGreaterThanOrEqualMinorOnly { // The minor-pessimistic syntax uses only two version components. - fmt.Fprintf(&b, "%s.%s", sel.Boundary.Major, sel.Boundary.Minor) + if sel.Boundary.Minor.Unconstrained { + // The parser allows writing ~> 2, which ends up being + // represented in memory as ~> 2.* because the minor + // version is unconstrained, but that's not really any + // different than saying 2.0 and so we'll prefer that in + // our serialization in order to be clearer about how we + // understood the version constraint. + fmt.Fprintf(&b, "%s.0", sel.Boundary.Major) + } else { + fmt.Fprintf(&b, "%s.%s", sel.Boundary.Major, sel.Boundary.Minor) + } } else { fmt.Fprintf(&b, "%s.%s.%s", sel.Boundary.Major, sel.Boundary.Minor, sel.Boundary.Patch) }