From 2953983ad5c5e830a5844be5d6d0a2b49863c05d Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 22 Jun 2022 13:52:32 -0700 Subject: [PATCH] fix(view): error on missing version (#5035) This fixes an error in npm show. When calling npm show with a specific version of a package that does not exist, it does not show anything and gives a zero exit code. This has been changed: now it gives a 404 Error similar to if the package does not exist. Can be tested with npm show express@5.0.0 (local: node bin/npm-cli.js info express@5.0.0) Fixes #4964 Co-authored-by: @lukaskuhn-lku Co-authored-by: @ljharb --- lib/commands/view.js | 9 +++++++++ test/lib/commands/view.js | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/commands/view.js b/lib/commands/view.js index db5d99b685558..3b8524ad3fc0d 100644 --- a/lib/commands/view.js +++ b/lib/commands/view.js @@ -236,6 +236,15 @@ class View extends BaseCommand { } }) + // No data has been pushed because no data is matching the specified version + if (data.length === 0 && version !== 'latest') { + const er = new Error(`No match found for version ${version}`) + er.statusCode = 404 + er.code = 'E404' + er.pkgid = `${pckmnt._id}@${version}` + throw er + } + if ( !this.npm.config.get('json') && args.length === 1 && diff --git a/test/lib/commands/view.js b/test/lib/commands/view.js index da823db5d7507..d347bc9230ec8 100644 --- a/test/lib/commands/view.js +++ b/test/lib/commands/view.js @@ -33,6 +33,7 @@ const packument = (nv, opts) => { }, }, blue: { + _id: 'blue', name: 'blue', 'dist-tags': { latest: '1.0.0', @@ -464,6 +465,14 @@ t.test('throws when unpublished', async t => { ) }) +t.test('throws when version not matched', async t => { + const { npm } = await loadMockNpm(t) + await t.rejects( + npm.exec('view', ['blue@2.0.0']), + { code: 'E404', pkgid: 'blue@2.0.0', message: 'No match found for version 2.0.0' } + ) +}) + t.test('workspaces', async t => { const prefixDir = { 'package.json': JSON.stringify({