Skip to content

[BUG] subset() returns false for an upper-bound-only range such as subset('<2.0.0', '*') #949

Open

Description

@trackoor

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

subset(sub, dom) returns false when sub has only an upper bound (< or <=) and dom has a lower bound, including *, which subset treats as >=0.0.0. Every version that satisfies <2.0.0 also satisfies *, so the README's "entirely contained" definition gives true.

subset('<1.0.0', '0.x') is a related false negative: 0.x is >=0.0.0 <1.0.0-0, which admits the same non-prerelease versions as <1.0.0.

Expected Behavior

The first five lines below print true.

In ranges/subset.js, simpleSubset replaces * in dom with >=0.0.0 and then returns false for lt && hasDomGT && !gt, which treats a missing lower bound in sub as unbounded below instead of >=0.0.0.

Steps To Reproduce

const { subset } = require('semver')
console.log(subset('<2.0.0', '*'))
console.log(subset('<2', '*'))
console.log(subset('<2.0.0', '>=0.0.0'))
console.log(subset('<2.0.0', '>=0.0.0-0'))
console.log(subset('<1.0.0', '0.x'))
console.log(subset('<1.0.0', '>=0.0.0 <1.0.0'))
console.log(subset('^1.2.3', '*'))

Output:

false
false
false
false
false
true
true

Environment

  • semver: 7.8.6, also main at b37b033
  • Node: 26.13.0
  • OS: Ubuntu Linux
  • platform: x86_64

Credit: https://ucsbplse.github.io/

Activity

  1. hamarshehmhmd commented on Nov 7, 2026

    @hamarshehmhmd

    Reproduced on main (b37b033). Two separate spots in ranges/subset.js:

    1. <2.0.0 vs * / >=0.0.0: after * becomes >=0.0.0, the lt && hasDomGT && !gt check returns false outright. A missing lower bound in sub really means "bounded by the lowest version", so it should only fail if some >/>= in dom rejects that minimum.
    2. <1.0.0 vs 0.x: 0.x parses to <1.0.0-0, and lowerLT treats that as stricter than <1.0.0. Without includePrerelease they admit the same versions (the needDomLTPre block already makes this exception for sub, just not for dom).

    Patch:

           if (c.operator === '<' || c.operator === '<=') {
             lower = lowerLT(lt, c, options)
    -        if (lower === c && lower !== lt) {
    +        if (lower === c && lower !== lt && !sameUpperBound(lt, c, options)) {
               return false
             }
    @@
       if (lt && hasDomGT && !gt && gtltComp !== 0) {
    -    return false
    +    const min = options.includePrerelease
    +      ? minimumVersionWithPreRelease[0].semver
    +      : minimumVersion[0].semver
    +    const domAdmitsMin = dom.every(c =>
    +      (c.operator !== '>' && c.operator !== '>=') || c.test(min))
    +    if (!domAdmitsMin) {
    +      return false
    +    }
       }
    @@
    +// without includePrerelease, <1.2.3-0 admits the same versions as <1.2.3
    +const sameUpperBound = (lt, c, options) =>
    +  !options.includePrerelease &&
    +  lt.operator === '<' && c.operator === '<' &&
    +  !lt.semver.prerelease.length &&
    +  c.semver.prerelease.length === 1 && c.semver.prerelease[0] === 0 &&
    +  c.semver.major === lt.semver.major &&
    +  c.semver.minor === lt.semver.minor &&
    +  c.semver.patch === lt.semver.patch

    With the patch:

    subset('<2.0.0', '*')            true
    subset('<2', '*')                true
    subset('<2.0.0', '>=0.0.0')      true
    subset('<2.0.0', '>=0.0.0-0')    true
    subset('<1.0.0', '0.x')          true
    subset('<2.0.0', '>=0.0.1')      false   (still)
    subset('<2.0.0', '>=1.0.0')      false   (still)
    subset('<=1.0.0', '0.x')         false   (still)
    subset('<2.0.0', '>=0.0.0', { includePrerelease: true })  false  (0.0.0-0 is excluded)
    
    • Added those as cases in test/ranges/subset.js. npm test: all 54 test files pass, coverage stays at 100%.
    • I also brute-forced ~8k range pairs against satisfies() over a version grid. False negatives went from 316 to 158, and there are no new false positives. The 84 pre-existing ones are all prerelease sub ranges, so that's a separate issue.

    Happy to open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions