Skip to content

Commit 887385d

Browse files
authored
fix(arborist): use hosted-git-info to correctly parse resolved git urls (#8356)
When custom hosted git url are used to install dependencies it's not installing that dependency when the url is of `git+ssh://[email protected]:a/b/c.git` format instead of `git+ssh://[email protected]/a/b/c.git`. #### Cause: ` new URL(resolved)` throws an error which is getting caught and in-turn results in removal of that node. This behaviour was working as expected before this #8185 which replaced `hgi.parseUrl(resoved)` call to `new URL(resolved)` #### Fix: keep the `hgi.parseUrl` call to correctly parse the git url as mentioned/explained in this PR #5758 Fixes: #8331
1 parent 7233cb3 commit 887385d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

workspaces/arborist/lib/arborist/reify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const debug = require('../debug.js')
99
const { walkUp } = require('walk-up-path')
1010
const { log, time } = require('proc-log')
1111
const rpj = require('read-package-json-fast')
12+
const hgi = require('hosted-git-info')
1213

1314
const { dirname, resolve, relative, join } = require('node:path')
1415
const { depth: dfwalk } = require('treeverse')
@@ -886,7 +887,7 @@ module.exports = cls => class Reifier extends cls {
886887
// Shrinkwrap and Node classes carefully, so for now, just treat
887888
// the default reg as the magical animal that it has been.
888889
try {
889-
const resolvedURL = new URL(resolved)
890+
const resolvedURL = hgi.parseUrl(resolved)
890891

891892
if ((this.options.replaceRegistryHost === resolvedURL.hostname) ||
892893
this.options.replaceRegistryHost === 'always') {

workspaces/arborist/test/arborist/reify.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,6 +3343,24 @@ t.test('should preserve exact ranges, missing actual tree', async (t) => {
33433343
},
33443344
})
33453345

3346+
const customGitSshPackument = JSON.stringify({
3347+
_id: 'gitssh',
3348+
_rev: 'lkjadflkjasdf',
3349+
name: 'gitssh',
3350+
'dist-tags': { latest: '1.1.1' },
3351+
versions: {
3352+
'1.1.1': {
3353+
name: 'gitssh',
3354+
version: '1.1.1',
3355+
dist: {
3356+
// this is a url that `new URL()` cant parse
3357+
// https://github.com/npm/cli/issues/5278
3358+
tarball: 'git+ssh://[email protected]:a/b/c.git#lkjadflkjasdf',
3359+
},
3360+
},
3361+
},
3362+
})
3363+
33463364
const notAUrlPackument = JSON.stringify({
33473365
_id: 'notaurl',
33483366
_rev: 'lkjadflkjasdf',
@@ -3359,6 +3377,36 @@ t.test('should preserve exact ranges, missing actual tree', async (t) => {
33593377
},
33603378
})
33613379

3380+
t.test('valid custom hosted git url', async (t) => {
3381+
const testdir = t.testdir({
3382+
project: {
3383+
'package.json': JSON.stringify({
3384+
name: 'myproject',
3385+
version: '1.0.0',
3386+
dependencies: {
3387+
gitssh: '1.1.1',
3388+
},
3389+
}),
3390+
},
3391+
})
3392+
3393+
tnock(t, 'https://registry.github.com')
3394+
.get('/gitssh')
3395+
.reply(200, customGitSshPackument)
3396+
3397+
const getLogs = warningTracker()
3398+
3399+
const arb = new Arborist({
3400+
path: resolve(testdir, 'project'),
3401+
registry: 'https://registry.github.com',
3402+
cache: resolve(testdir, 'cache'),
3403+
})
3404+
await arb.reify()
3405+
// since it's not throwing an error on invalid url and returning undefined
3406+
// which trashes the node, so here we can only check if it has no warnings
3407+
t.strictSame(getLogs(), [], 'did not get warnings')
3408+
})
3409+
33623410
t.test('host should not be replaced replaceRegistryHost=never', async (t) => {
33633411
const testdir = t.testdir({
33643412
project: {

0 commit comments

Comments
 (0)