Skip to content

[BUG] X-Range with Prerelease Ignores Prerelease #510

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

Closed
1 task done
WalkerCodeRanger opened this issue Dec 26, 2022 · 2 comments
Closed
1 task done

[BUG] X-Range with Prerelease Ignores Prerelease #510

WalkerCodeRanger opened this issue Dec 26, 2022 · 2 comments
Labels
Bug thing that needs fixing Needs Triage needs an initial review

Comments

@WalkerCodeRanger
Copy link

WalkerCodeRanger commented Dec 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

A range like >=1.x.x-rc appears to ignore the -rc and be treated as equal to >=1.x.x. For example, it does not match the version 1.0.0-rc.1.

Expected Behavior

Since it isn't clear what prerelease versions >=1.x.x-rc should match, I think it should be an invalid range. If one really wanted to support it, then it would need to match multiple prerelease ranges (e.g. 1.0.0-rc, 1.0.5-rc, 1.5.6-rc, etc.).

Steps To Reproduce

As I am not a Node developer but rather a maintainer of a semver package for C# that needs to support npm style version ranges, I am using https://semver.npmjs.com/ to test. Simply put the listed versions into it.

@mbtools
Copy link
Contributor

mbtools commented Apr 25, 2025

Ranges are mapped to so-called comparators which are a combination of a semantic version and an operator. Example:

node
> const Range = require('semver/classes/range')
undefined
> new Range('>=1.x.x-rc')
Range {
  options: {},
  loose: false,
  includePrerelease: false,
  raw: '>=1.x.x-rc',
  set: [ [ [Comparator] ] ],
  range: '>=1.0.0'
}

You can see the default behaviour of semver which is ignoring pre-releases. You get >=1.0.0. This explains why pre-releases like 1.0.0-rc.1 don't match since they are lower than 1.0.0 (i.e. "happening before on a timeline").

You can include prereleases via option:

> new Range('>=1.x.x-rc', { includePrerelease: true })
Range {
  options: { includePrerelease: true },
  loose: false,
  includePrerelease: true,
  raw: '>=1.x.x-rc',
  set: [ [ [Comparator] ] ],
  range: '>=1.0.0-0'
}

You get >=1.0.0-0 matching all pre-releases due to:

// if we're including prereleases in the match, then we need
// to fix this to -0, the lowest possible prerelease value
pr = options.includePrerelease ? '-0' : ''

Here are your examples:

> new Range('>=1.x.x-rc').test('1.0.0-rc.1')
false
> new Range('>=1.x.x-rc', { includePrerelease: true }).test('1.0.0-rc.1')
true
> new Range('>=1.x.x-rc').test('1.0.5-rc.1')
false
> new Range('>=1.x.x-rc', { includePrerelease: true }).test('1.0.5-rc.1')
true
> new Range('>=1.x.x-rc').test('1.5.6-rc.1')
false
> new Range('>=1.x.x-rc', { includePrerelease: true }).test('1.5.6-rc.1')
true

Hope that helps. Works as designed 😃

@wraithgar
Copy link
Member

Thanks @mbtools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs an initial review
Projects
None yet
Development

No branches or pull requests

3 participants