From d46d0526be12eae2cd458fd08dd5c0a0320cc8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlad-=C8=98tefan=20Harbuz?= <291640+vladh@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:19:17 +0100 Subject: [PATCH] fix: tolerate null bugs URLs (#6798) Currently, if the bugs URL returned from `package.json`, which is processed through [hosted-git-info](https://github.com/npm/hosted-git-info), is falsy, an error is thrown. However, hosted-git-info may well return a falsy bugs URL. This can happen when there is no bugs URL in the repo, but hosted-git-info tries to infer one, but one cannot be inferred, for example if the repository URL is a SourceHut URL. This is described here: https://github.com/npm/hosted-git-info/pull/213 For this reason, we should tolerate falsy URLs and fall back to whatever is defined in `bugs.js`, in this case being sent to https://www.npmjs.com/package/. --- lib/commands/bugs.js | 5 +++-- test/lib/commands/bugs.js | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/commands/bugs.js b/lib/commands/bugs.js index 17cbd96649b87..44926afbc9a0a 100644 --- a/lib/commands/bugs.js +++ b/lib/commands/bugs.js @@ -21,8 +21,9 @@ class Bugs extends PackageUrlCmd { // try to get it from the repo, if possible const info = this.hostedFromMani(mani) - if (info) { - return info.bugs() + const infoUrl = info?.bugs() + if (infoUrl) { + return infoUrl } // just send them to the website, hopefully that has some info! diff --git a/test/lib/commands/bugs.js b/test/lib/commands/bugs.js index 953c8e6345a2a..e2ebfb5306574 100644 --- a/test/lib/commands/bugs.js +++ b/test/lib/commands/bugs.js @@ -6,6 +6,10 @@ const pacote = { return spec === 'nobugs' ? { name: 'nobugs', version: '1.2.3', + } : spec === 'nullbugs' ? { + name: 'nullbugs', + version: '1.2.3', + bugs: null, } : spec === 'bugsurl' ? { name: 'bugsurl', version: '1.2.3', @@ -66,6 +70,7 @@ t.test('open bugs urls & emails', async t => { const expected = { '.': 'https://example.com', nobugs: 'https://www.npmjs.com/package/nobugs', + nullbugs: 'https://www.npmjs.com/package/nullbugs', 'bugsobj-nourl': 'https://www.npmjs.com/package/bugsobj-nourl', bugsurl: 'https://bugzilla.localhost/bugsurl', bugsobj: 'https://bugzilla.localhost/bugsobj',