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
Changes from 2 commits
5b27ff2
1f12d16
e82ef31
57ef86b
a1d4541
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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. | ||
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())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 (If There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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 thehashicorp/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 alog.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 forhashicorp/go-version
but not forapparentlymart/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.)There was a problem hiding this comment.
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.