compare-versions icon indicating copy to clipboard operation
compare-versions copied to clipboard

compareVersions('~12.1.2', '11.1.2') => 0 ?

Open pawtwa opened this issue 2 years ago • 5 comments

Hi,

this test fails:

it('should return 1 for versions ~12.1.2 and 11.1.2', () => { const result: VersionsComparison = compareVersions('~12.1.2', '11.1.2'); expect(result).toBe(1); });

with error:

Expected 0 to be 1.

Is this behaving expected?

pawtwa avatar Jan 14 '22 12:01 pawtwa

I've found some weird things using this library, too:

satisifes('10.0.4', '>=14.0.0') // true

When a dep of 10.x obviously does NOT satisfy a requirement of >=14.x.

lmiller1990 avatar Apr 01 '22 02:04 lmiller1990

compareVersions('~12.1.2', '11.1.2')

@pawtwa The compareVersions function tests which of two versions is bigger. This example uses ~ in the first version string which is a range. The function you can use in this case is satisfies('11.1.2', '~12.1.2') (which in this case would be false) :)

omichelsen avatar Apr 02 '22 16:04 omichelsen

satisifes('10.0.4', '>=14.0.0') // true

@lmiller1990 I added your example to the unit test suite and it correctly returns false. Can you share a code pen/snippet of the full example if you are still seeing this problem?

omichelsen avatar Apr 02 '22 17:04 omichelsen

Sure - I just installed the module from rpm and did the absolute most basic program possible. I'm now using the semver lib which works as expected, out of the box, but I hope we can get to the bottom of the bug here, too. I must be missing something, this library has millions downloads, there's no way the most basic feature is not working.

I just did:

yarn init -y
yarn add compare-versions

Here's the entire source (shown via cat and ls):

$ ls
index.js	node_modules	package.json	yarn.lock
$ cat package.json
{
  "name": "anything-node",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "compare-versions": "^4.1.3"
  }
}

$ cat yarn.lock
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


compare-versions@^4.1.3:
  version "4.1.3"
  resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-4.1.3.tgz#8f7b8966aef7dc4282b45dfa6be98434fc18a1a4"
  integrity sha512-WQfnbDcrYnGr55UwbxKiQKASnTtNnaAWVi8jZyy8NTpVAXWACSne8lMD1iaIo9AiU6mnuLvSVshCzewVuWxHUg==


$ cat index.js
const { satisfies } = require('compare-versions')

console.log(
  satisfies('10.0.4', '>=14.0.0') // true
)

$ node index.js
true

$ node --version
v16.14.2

lmiller1990 avatar Apr 03 '22 20:04 lmiller1990

I think you are right, there is a discrepancy between the CJS and MJS version of the package. I will change the build system so they are identical to avoid this happening again in the future. Thanks for the great repro example.

omichelsen avatar Apr 05 '22 15:04 omichelsen

This has now been fixed by v5 which ensures identical behavior between CJS and ESM versions.

omichelsen avatar Aug 26 '22 02:08 omichelsen