Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the apparentlymart/go-versions library to parse module constraints #32377

Merged
merged 5 commits into from Dec 14, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 41 additions & 3 deletions internal/initwd/module_install.go
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"errors"
"fmt"
"github.com/apparentlymart/go-versions/versions"
"log"
"os"
"path"
"path/filepath"
"strings"

version "github.com/hashicorp/go-version"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/earlyconfig"
Expand Down Expand Up @@ -387,9 +389,45 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *earlyc

// If we've found a pre-release version then we'll ignore it unless
// it was exactly requested.
if v.Prerelease() != "" && req.VersionConstraints.String() != v.String() {
log.Printf("[TRACE] ModuleInstaller: %s ignoring %s because it is a pre-release and was not requested exactly", key, v)
continue
//
// The prerelease checking will be handled by a different library for
// 2 reasons. First, this other library automatically includes the
// "prerelease versions must be exactly requested" behaviour that we are
// looking for. Second, this other library is used to handle all version
// constraints for the provider logic and this is the first step to
// making the module and provider version logic match.
if v.Prerelease() != "" {
// At this point all versions published by the module with
// prerelease metadata will be checked. Users may not have even
// requested this prerelease so don't print lots of unnecessary #
// warnings.
acceptableVersions, err := versions.MeetingConstraintsString(req.VersionConstraints.String())
if err != nil {
log.Printf("[WARN] ModuleInstaller: %s ignoring %s because the version constraints (%s) could not be parsed: %s", key, v, req.VersionConstraints.String(), err.Error())
continue
}

// Validate the version is also readable by the other versions
// library.
version, err := versions.ParseVersion(v.String())
if err != nil {
log.Printf("[WARN] ModuleInstaller: %s ignoring %s because the version (%s) reported by the module could not be parsed: %s", key, v, v.String(), err.Error())
continue
}

// Finally, check if the prerelease is acceptable to version. As
// highlighted previously, we go through all of this because the
// apparentlymart/go-versions library handles prerelease constraints
// in the apporach we want to.
if !acceptableVersions.Has(version) {
log.Printf("[TRACE] ModuleInstaller: %s ignoring %s because it is a pre-release and was not requested exactly", key, v)
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting to note that this effectively means that any prerelease versions announced by the registry will be subject to apparentlymart/go-versions matching before we get to the req.VersionConstraints.Check(v) call below.

I don't think this is actually a problem -- this is still more general than the direct string comparison it is replacing -- but might be worth a comment noting this in the hope of minimizing confusion for a future maintainer here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added additional comments, expanding on the context. Hopefully it's more clear now 😅

}

// If we reach here, it means this prerelease version was exactly
// requested according to the extra constraints of this library.
// We fall through and allow the other library to also validate it
// for consistency.
}

if latestVersion == nil || v.GreaterThan(latestVersion) {
Expand Down