GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Numeric-only part with leading zero(s) in prerelease section causes "Invalid Version" · Issue #237 · npm/node-semver

Numeric-only part with leading zero(s) in prerelease section causes "Invalid Version" #237

New issue
Copy link
New issue
Copy link
Closed
Closed
Numeric-only part with leading zero(s) in prerelease section causes "Invalid Version"#237
Copy link

Description

@bartvanandel
bartvanandel
opened on Apr 20, 2018
Issue body actions

Semver fails with 2.0.0-20180101T112233.0100000.0 (the 0100000 part, in our case this is a git hash), i.e. a dot-separated numeric-only part of the prerelease tag with one or more leading zeros. Adding a letter somewhere or removing the initial zeros results in a proper detection again.

Oddly enough, we see different results when using [].map or [].filter (or the lodash equivalents): map will remove the initial zero (unwanted!), filter will keep it. (Maybe this behavior is caused by semver modifying function arguments instead of assigning to new variables?) See below:

λ node
> const semver = require('semver')
undefined
> const _ = require('lodash')
undefined
> semver.valid('2.0.0-20180101T112233.0100000.0')
null
> ['1.2.3', '2.0.0-20180101T112233.0100000.0', '2.0.0-20180101T112233.1100000.0', '2.0.0-20180101T112233.010000x.0', 'latest'].map(semver.valid)
[ '1.2.3',
  '2.0.0-20180101T112233.100000.0',
  '2.0.0-20180101T112233.1100000.0',
  '2.0.0-20180101T112233.010000x.0',
  null ]
> ['1.2.3', '2.0.0-20180101T112233.0100000.0', '2.0.0-20180101T112233.1100000.0', '2.0.0-20180101T112233.010000x.0', 'latest'].filter(semver.valid)
[ '1.2.3',
  '2.0.0-20180101T112233.0100000.0',
  '2.0.0-20180101T112233.1100000.0',
  '2.0.0-20180101T112233.010000x.0' ]

Node version:

λ node --version
v9.11.1

Note: previously this was added as a comment to #232, but while it looks related, it's a different issue

Activity

  1. isaacs commented on Apr 20, 2018

    @isaacs
    isaacs
    on Apr 20, 2018
    Contributor
    More actions

    Note that "filter" sends more than one argument, that's why you're seeing different behavior there. Because the second argument to the filter function is the index in the array, every item after the first will be filtered in "loose" mode, since any number >0 is truthy. You can avoid this by doing something like:

    versionList.filter(v => semver.valid(v))

    This is a bit tricky. The SemVer spec says that dot-separated prerelease sections can be numeric, or alphanumeric, and if they are numeric, they are to be sorted in numeric order. In order to accomplish this, numeric values are turned into actual numbers in loose mode, but (incorrectly) deemed invalid in strict mode.

    One possible approach might be to treat numeric prerelease segments as alphanumeric if they start with 0. However, this would mean, for example, that 1.2.3-02 would be "less than" 1.2.3-1, which seems a bit surprising to me.

    Another option would be to preserve the prerelease sections as strings, but still sort them numerically. (Ie, convert to numbers when doing the gt/lt/eq calculations.) That's a bit more work, but would likely be least surprising.

    What do you think?

  2. bartvanandel commented on May 18, 2018

    @bartvanandel
    bartvanandel
    on May 18, 2018
    Author
    More actions

    Thanks for the hint about filter, that's something we should definitely fix on our side. But the real problem lies somewhere else. The key is in this line from section 9:

    Numeric identifiers MUST NOT include leading zeroes.

    What that means is that the approach taken by node-semver is actually correct. The all-numeric git hash with a leading zero is just an unfortunate edge case. I'll see if we can change our naming scheme, that seems like the best solution.

    It could be helpful though to hint users what's wrong with the version number if deemed invalid though, in this case something like "Numeric identifiers MUST NOT include leading zeroes: '{actual string}'". But that's another story.

  3. bartvanandel
    closed this as completedon May 18, 2018
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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.