fix: get the right pkg name of a pkg resolved from GitHub registry

ref #2687
PR #2734
This commit is contained in:
Zoltan Kochan
2020-07-31 11:09:32 +03:00
committed by GitHub
parent 1140ef721b
commit 622c0b6f93
4 changed files with 79 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/npm-resolver": patch
---
Always use the package name that is given at the root of the metadata object. Override any names that are specified in the version manifests. This fixes an issue with GitHub registry.

View File

@@ -20,7 +20,16 @@ export default function (
version = pickVersionByVersionRange(meta, spec.fetchSpec, preferredVersionSelectors)
break
}
return meta.versions[version]
const manifest = meta.versions[version]
if (manifest && meta['name']) {
// Packages that are published to the GitHub registry are always published with a scope.
// However, the name in the package.json for some reason may omit the scope.
// So the package published to the GitHub registry will be published under @foo/bar
// but the name in package.json will be just bar.
// In order to avoid issues, we consider that the real name of the package is the one with the scope.
manifest.name = meta['name']
}
return manifest
}
function pickVersionByVersionRange (

View File

@@ -12,6 +12,7 @@ import tempy = require('tempy')
const isPositiveMeta = loadJsonFile.sync<any>(path.join(__dirname, 'meta', 'is-positive.json'))
const isPositiveMetaWithDeprecated = loadJsonFile.sync<any>(path.join(__dirname, 'meta', 'is-positive-with-deprecated.json'))
const isPositiveMetaFull = loadJsonFile.sync<any>(path.join(__dirname, 'meta', 'is-positive-full.json'))
const isPositiveBrokenMeta = loadJsonFile.sync<any>(path.join(__dirname, 'meta', 'is-positive-broken.json'))
const sindresorhusIsMeta = loadJsonFile.sync<any>(path.join(__dirname, 'meta', 'sindresorhus-is.json'))
// tslint:enable:no-any
@@ -1553,3 +1554,39 @@ test('throws error when package name has "/" but not starts with @scope', async
t.end()
}
})
test('resolveFromNpm() should always return the name of the package that is specified in the root of the meta', async t => {
nock(registry)
.get('/is-positive')
.reply(200, isPositiveBrokenMeta)
const storeDir = tempy.directory()
const resolve = createResolveFromNpm({
metaCache: new Map(),
rawConfig: { registry },
storeDir,
})
const resolveResult = await resolve({ alias: 'is-positive', pref: '3.1.0' }, {
registry,
})
t.equal(resolveResult!.resolvedVia, 'npm-registry')
t.equal(resolveResult!.id, 'registry.npmjs.org/is-positive/3.1.0')
t.equal(resolveResult!.latest!.split('.').length, 3)
t.deepEqual(resolveResult!.resolution, {
integrity: 'sha512-9Qa5b+9n69IEuxk4FiNcavXqkixb9lD03BLtdTeu2bbORnLZQrw+pR/exiSg7SoODeu08yxS47mdZa9ddodNwQ==',
registry,
tarball: 'https://registry.npmjs.org/is-positive/-/is-positive-3.1.0.tgz',
})
t.ok(resolveResult!.manifest)
t.equal(resolveResult!.manifest!.name, 'is-positive')
t.equal(resolveResult!.manifest!.version, '3.1.0')
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(storeDir, 'metadata/registry.npmjs.org/is-positive.json')) // tslint:disable-line:no-any
t.ok(meta.name)
t.ok(meta.versions)
t.ok(meta['dist-tags'])
t.end()
})

View File

@@ -0,0 +1,27 @@
{
"versions": {
"3.1.0": {
"name": "bad-name",
"version": "3.1.0",
"devDependencies": {
"ava": "*",
"xo": "*"
},
"_hasShrinkwrap": false,
"directories": {},
"dist": {
"integrity": "sha512-9Qa5b+9n69IEuxk4FiNcavXqkixb9lD03BLtdTeu2bbORnLZQrw+pR/exiSg7SoODeu08yxS47mdZa9ddodNwQ==",
"shasum": "857db584a1ba5d1cb2980527fc3b6c435d37b0fd",
"tarball": "https://registry.npmjs.org/is-positive/-/is-positive-3.1.0.tgz"
},
"engines": {
"node": ">=0.10.0"
}
}
},
"name": "is-positive",
"dist-tags": {
"latest": "3.1.0"
},
"modified": "2017-08-17T19:26:00.508Z"
}