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 2 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
28 changes: 25 additions & 3 deletions internal/initwd/module_install.go
Expand Up @@ -10,7 +10,9 @@ import (
"path/filepath"
"strings"

"github.com/apparentlymart/go-versions/versions"
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,29 @@ 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
if v.Prerelease() != "" {
// Switch over to another library to check these version
// constraints. This means our version constraint logic matches
// between providers and modules. But it does leave this function
// having to switch between the two libraries.
acceptableVersions, err := versions.MeetingConstraintsString(req.VersionConstraints.String())
if err != nil {
// This shouldn't really happen, as we validated the constraints
// string earlier. Let's return a nice error message anyway.
Copy link
Member

Choose a reason for hiding this comment

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

While I agree that this should be okay, it is possible in principle that versions.MeetingConstraintsString is stricter in some cases than the hashicorp/go-versions parser.

Given that for now we're just trying to use apparentlymart/go-versions only as an extra filter for prereleases and not fully embrace it, I wonder if we should instead make this a log.Printf with [WARN] so that a regular user will never see any messaging in this case.

I think it should be safe to assume that any version constraint that represents just an exact version selection should always pass versions.MeetingConstraintsString, and so it seems pragmatic to assume that if we get any errors here then it's not just an exact match and therefore shouldn't select a prerelease.

(Note that v is under the control of the module registry rather than the user, so a user with a version constraint that is somehow valid for hashicorp/go-version but not for apparentlymart/go-versions would see this warning just because the module registry announced at least one prerelease version, regardless of whether they actually intended to use that prerelease version.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done! Changed the tfdiags for a warning log message.

diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"Invalid version constraint string",
fmt.Sprintf("The requested version constraint (%s) could not be be parsed: %s", req.VersionConstraints.String(), err.Error()),
))
continue
}

// We validated the version string previously, so we are quite sure
// that MustParseVersion will not crash/panic.
if !acceptableVersions.Has(versions.MustParseVersion(v.String())) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if it's 100% safe to assume that successful parsing with hashicorp/go-version guarantees successful parsing with apparentlymart/versions. For example: I believe go-version allows a string like 1.0.0.0.0.0.0.0 (any number of numeric segments) if using its main version parser, although not if using its NewSemver function.

If we have been allowing non-semver-syntax version numbers then I suppose this could represent a compatibility regression, so I hope we haven't but I cannot guarantee it off the top of my head. Would probably be worth some testing with some of the examples from the New tests and the NewSemver tests to get a sense of what is and isn't currently accepted by our module installer, and whether all of those inputs are valid per apparentlymart/go-versions.

(If go-versions is rejecting any that seems like valid semver syntax then I would consider that to be a bug, but go-version was originally more general than semver and that would be out of scope for apparentlymart/go-versions, which is focused on the semver sense of version numbers in particular.)

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 a global comment discussing testing more generally. In this case specifically, it checks if the version is parseable now and writes a log message if not.

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 latestVersion == nil || v.GreaterThan(latestVersion) {
Expand Down