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

Unable to match version range with pre-releases #323

Open
Wiezzel opened this issue Aug 9, 2024 · 3 comments
Open

Unable to match version range with pre-releases #323

Wiezzel opened this issue Aug 9, 2024 · 3 comments

Comments

@Wiezzel
Copy link

Wiezzel commented Aug 9, 2024

I am trying to match all versions >=1.0.0 and <2.0.0, including pre-release versions. Unfortunately, >=1.0.0-rc, <2.0.0-rc1 doesn't do the trick, as pre-releases only match if some comparator has exactly the same major, minor or patch. Is it even possible to specify this kind of dependency?

@gabrik
Copy link

gabrik commented Oct 23, 2024

I'm having a similar issue, I have a version req that is >=1.0.0, <1.1.0, version that is 1.0.0-beta.6 and apparently the version does not match.
Is there a way to make the VersionReq accept pre-releases?

@Wiezzel
Copy link
Author

Wiezzel commented Oct 23, 2024

@gabrik
I have implemented this custom matching logic:

fn version_matches(version: &semver::Version, req: &semver::VersionReq) -> bool {
    if req.matches(version) {
        return true;
    }

    // This custom matching logic is needed, because semver cannot compare different version with pre-release tags
    let mut version_without_pre = version.clone();
    version_without_pre.pre = "".parse().unwrap();
    for comp in &req.comparators {
        if comp.matches(version) {
            continue;
        }

        // If major & minor & patch are the same, this means there is a mismatch on the pre-release tag
        if comp.major == version.major
            && comp.minor.is_some_and(|m| m == version.minor)
            && comp.patch.is_some_and(|p| p == version.patch)
        {
            return false;
        }

        // Otherwise, compare without pre-release tags
        let mut comp_without_pre = comp.clone();
        comp_without_pre.pre = "".parse().unwrap();
        if !comp_without_pre.matches(&version_without_pre) {
            return false;
        }
    }
    true
}

@gabrik
Copy link

gabrik commented Oct 23, 2024

I see, I think I'm going to do the same thing, thank you @Wiezzel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants