Skip to content

[BUG] new Range(comparator).test() throws TypeError for a prerelease version #908

Open

Description

@nestharus

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When a Range is constructed from a Comparator object, range.options is never set. Testing a prerelease version against that range then throws instead of returning a boolean. satisfies(version, comparator) goes through the same path and throws too.

Cause: in classes/range.js (v7.8.5), the range instanceof Comparator branch (L21-27) returns before this.options = options (L29). test() passes this.options (L210) to testSet, which reads options.includePrerelease (L550).

(Found by an AI system I use; a search of the tracker found nothing covering it.)

Expected Behavior

The same result as for the equivalent string range: true for 1.0.0-beta against >=1.0.0-alpha. Or, if a Comparator argument is not supported, a clear error at construction. The README does not document it either way.

Steps To Reproduce

  1. npm i semver@7.8.5
  2. Run:
    const semver = require('semver')
    const r = new semver.Range(new semver.Comparator('>=1.0.0-alpha'))
    console.log(new semver.Range('>=1.0.0-alpha').test('1.0.0-beta'))  // true
    console.log(r.options)                                            // undefined
    console.log(r.test('1.0.0-beta'))
  3. See error:
    TypeError: Cannot read properties of undefined (reading 'includePrerelease')
        at testSet (.../semver/classes/range.js:550:45)
        at Range.test (.../semver/classes/range.js:210:11)
    

Same on 7.7.3, where the lines are range.js:530 and :207.

Environment

  • npm: n/a (library called directly from node)
  • Node: v24.12.0
  • OS: Windows 11 Pro
  • platform: x64 desktop
  • semver: 7.8.5 (latest) and 7.7.3

Activity

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

    Bugthing that needs fixingNeeds Triageneeds an initial review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions