Version requirement to match X and version above, including prereleases

Hi eveyone,
I am looking to match all versions above 1.11 including 1.11.0-rc0, any idea how to achieve this?

iex(35)> Version.match?("1.11.0-rc1", "~> 1.11")  
false

iex(36)> Version.match?("1.11.0-rc1", "~> 1.11", allow_pre: true)
false

iex(37)> Version.match?("1.11.0-rc1", ">= 1.11", allow_pre: true)
** (Version.InvalidRequirementError) invalid requirement: ">= 1.11"
    (elixir 1.13.0-rc.1) lib/version.ex:433: Version.parse_requirement!/1
    (elixir 1.13.0-rc.1) lib/version.ex:278: Version.match?/3

iex(37)> Version.match?("1.11.0-rc1", ">= 1.11.0", allow_pre: true)
false

iex(38)> Version.match?("1.11.0-rc1", ">= 1.11-0")                 
** (Version.InvalidRequirementError) invalid requirement: ">= 1.11-0"
    (elixir 1.13.0-rc.1) lib/version.ex:433: Version.parse_requirement!/1
    (elixir 1.13.0-rc.1) lib/version.ex:278: Version.match?/3

The only way to achieve this, is by using the lowest prerelease that I could come up with -0 which is a bit hackish but gets the job done.

iex(38)> Version.match?("1.11.0-rc1", ">= 1.11.0-0")
true
iex(39)> Version.match?("1.12.0", ">= 1.11.0-0")
true

iex(41)> Version.match?("1.11.0-rc1", "~> 1.11-0")
true
iex(41)> Version.match?("1.12.0", "~> 1.11-0")  
true

Is there anything that I am missing?

1 Like

FWIW, I couldn’t make your examples work with Rust either – tried it as a sort of a sanity double-check.

use semver::*;

fn main() {
    let req_a = VersionReq::parse(">=1.11.0-rc").unwrap();

    let v0 = Version::parse("1.11.0-rc0").unwrap();
    let v1 = Version::parse("1.11.0-rc1").unwrap();

    println!("Does {} match {} ? => {}", v0, req_a, req_a.matches(&v0));
    println!("Does {} match {} ? => {}", v1, req_a, req_a.matches(&v1));
}

That was the only way I could make it return true. All other variants – namely 1.11.0 and 1.11 – yielded false. (And their library doesn’t accept the ~> ... format)

1 Like

I typically do it this way:

iex> Version.match?("1.11.0-rc1", "~> 1.11 or ~> 1.11-rc")                 
true
1 Like

Isn’t ~> 1.11 covered by ~> 1.11-rc ?

iex> Version.match?("1.11.0", "~> 1.11-rc")
true

You are right!!! Wonder why I didn’t notice that before.

1 Like

The thing is that works with Elixir because prereleases start with “rc”, but I was looking for a more generic solution.