Skip to content

Commit

Permalink
fix(cli): Fix version range matching for 0.x version ranges with the …
Browse files Browse the repository at this point in the history
…~> operator

based on hashicorp/terraform-cdk#3403
  • Loading branch information
ansgarm committed Jan 10, 2024
1 parent 544961e commit 29c875a
Showing 1 changed file with 13 additions and 3 deletions.
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

0 comments on commit 29c875a

Please sign in to comment.