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" #237DescriptionSemver 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:
Note: previously this was added as a comment to #232, but while it looks related, it's a different issue Activityisaacs commented on Apr 20, 2018 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? bartvanandel commented on May 18, 2018 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.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|