diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2016-05-13 05:29:07 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2016-05-20 00:34:26 +0300 |
commit | 42d71be2cec674dd9e860ad414f53184f667620d (patch) | |
tree | e5860538694b09a0e19576b418499ae3df089e7a | |
parent | c698ae666afc92fbc0fcba3c082cfa9b34a4420d (diff) |
ls: fix filter when prerelease version present
If `ls` for a package without using a semver filter,
for example, `npm ls foo` vs `foo@1.2.3`, `ls` was using semver
ranges based on `*`, which doesn't match prerelease versions.
So, if you had installed a prerelease version (`foo@1.2.3-xyz`),
the `npm ls` will return no results for `foo`, at all.
This patch bypasses the semver check entirely when there's no semver
filter for the search.
Fixes: https://github.com/npm/npm/issues/9436
Credit: @zkat
PR-URL: https://github.com/npm/npm/pull/12685
Reviewed-By: @othiym23
-rw-r--r-- | lib/ls.js | 11 | ||||
-rw-r--r-- | test/tap/ls.js | 37 |
2 files changed, 46 insertions, 2 deletions
@@ -62,7 +62,11 @@ var lsFromTree = ls.fromTree = function (dir, physicalTree, args, silent, cb) { args = args.map(function (a) { var p = npa(a) var name = p.name - var ver = semver.validRange(p.rawSpec) || '' + // When version spec is missing, we'll skip using it when filtering. + // Otherwise, `semver.validRange` would return '*', which won't + // match prerelease versions. + var ver = (p.rawSpec && + (semver.validRange(p.rawSpec) || '')) return [ name, ver, a ] }) } @@ -292,8 +296,11 @@ function filterFound (root, args) { var argName = args[ii][0] var argVersion = args[ii][1] var argRaw = args[ii][2] - if (depName === argName) { + if (depName === argName && argVersion) { found = semver.satisfies(dep.version, argVersion, true) + } else if (depName === argName) { + // If version is missing from arg, just do a name match. + found = true } else if (dep.path === argRaw) { found = true } diff --git a/test/tap/ls.js b/test/tap/ls.js index f75811ec8..acec723af 100644 --- a/test/tap/ls.js +++ b/test/tap/ls.js @@ -127,6 +127,43 @@ test('ls with missing filtered arg', function (t) { }) }) +test('ls with prerelease pkg', function (t) { + var fixture = new Tacks( + Dir({ + 'npm-test-ls': Dir({ + 'package.json': File({ + name: 'npm-test-ls', + version: '1.0.0', + dependencies: { + 'dep': 'file:../dep' + } + }) + }), + 'dep': Dir({ + 'package.json': File({ + name: 'dep', + version: '1.0.0-pre' + }) + }) + }) + ) + withFixture(t, fixture, function (done) { + common.npm([ + 'ls', 'dep', + '--json' + ], { + cwd: path.join(fixturepath, 'npm-test-ls') + }, function (err, code, stdout, stderr) { + t.ifErr(err, 'ls succeeded') + t.equal(0, code, 'exit 0 on ls') + var pkg = JSON.parse(stdout) + var deps = pkg.dependencies + t.ok(deps.dep, 'dep present') + t.done() + }) + }) +}) + test('cleanup', function (t) { rimraf.sync(basepath) t.done() |