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

fix(cli): Fix version range matching for 0.x version ranges with the ~> operator #390

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/scripts/check-for-upgrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function getCurrentProviderVersion() {
}

// SEE NOTICE AT THE TOP WHY THIS IS INLINED CURRENTLY
// copied from https://github.com/hashicorp/terraform-cdk/blob/b23fc173715e90c0a5b8c8633d9ec7f71edf9ed4/packages/cdktf-cli/lib/dependencies/version-constraints.ts
// copied from https://github.com/hashicorp/terraform-cdk/blob/df858ccf4ac71a168e3636f053c6743324c98332/packages/%40cdktf/cli-core/src/lib/dependencies/version-constraints.ts
// and converted to JavaScript

// constraints can be prefixed with "~>", ">", "<", "=", ">=", "<=" or "!="
Expand Down Expand Up @@ -158,16 +158,26 @@ function versionMatchesConstraint(version, constraint) {
case "~>": {
// allows rightmost version component to increment

const parts = parsed.version.split(".");
const minorSpecified = parts.length === 2;
const majorIsZero = parts[0] === "0";

// ~>2.0 which allows 2.1 and 2.1.1 needs special handling as
// npm semver handles "~" differently for ~2.0 than for ~2 or ~2.1.0
// So we need to use "^" (e.g. ^2.0) for this case
// see: https://github.com/npm/node-semver/issues/11
const allowMinorAndPatchOnly = parsed.version.split(".").length === 2;
const allowMinorAndPatchOnly = minorSpecified;

const range = allowMinorAndPatchOnly
let range = allowMinorAndPatchOnly
? \`^\${parsed.version}\`
: \`~\${parsed.version}\`;

// versions below 1.0 are treated a bit differently in NPM than in Terraform
// meaning that NPMs ^0.4 doesn't allow 0.55 while TFs ~>0.4 allows 0.55
if (majorIsZero && minorSpecified) {
range = \`>=\${parsed.version} <1.0.0\`;
}

return semver.satisfies(version, range);
}
case ">=":
Expand Down
48 changes: 39 additions & 9 deletions test/__snapshots__/index.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.