Strip "v" prefix from constraints when parsing by apparentlymart/go-versions fails.

Signed-off-by: AYM1607 <u.g.a.mariano@gmail.com>
This commit is contained in:
AYM1607 2024-11-05 21:37:38 +00:00 committed by Martin Atkins
parent c3cb9c0485
commit 8de4488443

View File

@ -13,6 +13,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"github.com/apparentlymart/go-versions/versions" "github.com/apparentlymart/go-versions/versions"
@ -412,6 +413,11 @@ func (i *ModuleInstaller) installLocalModule(req *configs.ModuleRequest, key str
return mod, diags return mod, diags
} }
// versionRegexp is used to handle edge cases around prerelease version constraints
// when installing registry modules, its usage is discouraged in favor of the
// public hashicorp/go-version API.
var versionRegexp = regexp.MustCompile(version.VersionRegexpRaw)
func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *configs.ModuleRequest, key string, instPath string, addr addrs.ModuleSourceRegistry, manifest modsdir.Manifest, hooks ModuleInstallHooks, fetcher *getmodules.PackageFetcher) (*configs.Module, *version.Version, hcl.Diagnostics) { func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *configs.ModuleRequest, key string, instPath string, addr addrs.ModuleSourceRegistry, manifest modsdir.Manifest, hooks ModuleInstallHooks, fetcher *getmodules.PackageFetcher) (*configs.Module, *version.Version, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
@ -517,11 +523,30 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
// prerelease metadata will be checked. Users may not have even // prerelease metadata will be checked. Users may not have even
// requested this prerelease so don't print lots of unnecessary # // requested this prerelease so don't print lots of unnecessary #
// warnings. // warnings.
acceptableVersions, err := versions.MeetingConstraintsString(req.VersionConstraint.Required.String()) constraint := req.VersionConstraint.Required.String()
acceptableVersions, err := versions.MeetingConstraintsString(constraint)
if err != nil {
log.Printf("[WARN] ModuleInstaller: Attempting to strip \"v\" prefixes, because version constraints %q was unable to be parsed: %s", constraint, err.Error())
// apparentlymart/go-versions purposely doesn't accept "v" prefixes.
// However, hashicorp/go-version does, which leads to inconsistent
// errors when specifying constraints that contain prerelease
// versions with "v" prefixes. This creates a semantically equivalent
// constraint with all prefixes stripped so it can be checked
// against apparentlymart/go-versions.
//
// strippedConstraint should not live beyond this scope.
strippedConstraint := string(versionRegexp.ReplaceAllFunc([]byte(constraint), func(match []byte) []byte {
if match[0] == 'v' {
return match[1:]
}
return match
}))
acceptableVersions, err = versions.MeetingConstraintsString(strippedConstraint)
if err != nil { if err != nil {
log.Printf("[WARN] ModuleInstaller: %s ignoring %s because the version constraints (%s) could not be parsed: %s", key, v, req.VersionConstraint.Required.String(), err.Error()) log.Printf("[WARN] ModuleInstaller: %s ignoring %s because the version constraints (%s) could not be parsed: %s", key, v, req.VersionConstraint.Required.String(), err.Error())
continue continue
} }
}
// Validate the version is also readable by the other versions // Validate the version is also readable by the other versions
// library. // library.