Skip to content

[BUG] semver.lt returns wrong value for pre-release version comparison #483

Open

Description

@IainSAP

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Using semver.lt() returns the incorrect value when comparing pre-release version with includePrerelease set to false (or the default).

image

By comparison, explicitly coercing you get:

semver_1.lt(semver_1.coerce('0.2.0-1'), minVersion)
false

My assumption is that strings are implicitly coerced when calling comparison functions, perhaps this is incorrect?

Expected Behavior

semver.lt('0.2.0-1', '0.2.0', { includePrerelease: false }) should return false.

Steps To Reproduce

  1. In node shell
  2. With the current latest version of semver installed
  3. Run 'require('semver').lt('0.2.0-1', '0.2.0', { includePrerelease: false })
  4. Wrong value true is returned

Environment

  • npm: 6.14.15
  • Node: 14.8.1
  • OS: Mac Monterey 12.5.1
  • platform: Macbook Pro i9

Activity

  1. changed the title [-][BUG] <title>[/-] [+][BUG] semver.lt returns wrong value for pre-release version comparison[/+] on Sep 22, 2022
  2. lukekarrys commented on Oct 27, 2022

    @lukekarrys
    Contributor

    This is similar to #345, and I think the fix might even be the same for both.

  3. mbtools commented on Jan 21, 2025

    @mbtools
    Contributor

    The comparison functions do not use the includePrerelease option! If major.minor.patch are the same, they always compare the pre-release parts.

    Maybe the docs could be improved since it's suggesting that all functions use evaluate all options:

    node-semver/README.md

    Lines 402 to 414 in 868d4bb

    All methods and classes take a final `options` object argument. All
    options in this object are `false` by default. The options supported
    are:
    - `loose`: Be more forgiving about not-quite-valid semver strings.
    (Any resulting output will always be 100% strict compliant, of
    course.) For backwards compatibility reasons, if the `options`
    argument is a boolean value instead of an object, it is interpreted
    to be the `loose` param.
    - `includePrerelease`: Set to suppress the [default
    behavior](https://github.com/npm/node-semver#prerelease-tags) of
    excluding prerelease tagged versions from ranges unless they are
    explicitly opted into.

  4. 6Demon6Child6 commented on Jan 21, 2025

    @6Demon6Child6
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 fixing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions