fix: print a better error message when "time" is missing from metadata (#8059)

This commit is contained in:
Zoltan Kochan authored and GitHub committed 2024-05-07 12:58:31 +02:00
1 parent 01a4566655
commit 43b6bb7ce3
3 files changed
+22 -1

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
---
"@pnpm/npm-resolver": patch
"@pnpm/default-reporter": patch
"pnpm": patch
---
Print a better error message when `resolution-mode` is set to `time-based` and the registry fails to return the `"time"` field in the package's metadata.
+2
View File
@@ -67,6 +67,8 @@ function getErrorInfo (logObj: Log, config?: Config, peerDependencyRules?: PeerD
return reportLockfileBreakingChange(err, logObj)
case 'ERR_PNPM_RECURSIVE_RUN_NO_SCRIPT':
return { title: err.message }
case 'ERR_PNPM_MISSING_TIME':
return { title: err.message, body: 'If you cannot fix this registry issue, then set "resolution-mode" to "highest".' }
case 'ERR_PNPM_NO_MATCHING_VERSION':
return formatNoMatchingVersion(err, logObj)
case 'ERR_PNPM_RECURSIVE_FAIL':
@@ -1,6 +1,7 @@
import { PnpmError } from '@pnpm/error'
import { type VersionSelectors } from '@pnpm/resolver-base'
import semver from 'semver'
import util from 'util'
import { type RegistryPackageSpec } from './parsePref'
import { type PackageInRegistry, type PackageMeta } from './pickPackage'
@@ -50,7 +51,15 @@ export function pickPackageFromMeta (
manifest.name = meta['name']
}
return manifest
} catch (err: any) { // eslint-disable-line
} catch (err: unknown) {
if (
util.types.isNativeError(err) &&
'code' in err &&
typeof err.code === 'string' &&
err.code.startsWith('ERR_PNPM_')
) {
throw err
}
throw new PnpmError('MALFORMED_METADATA',
`Received malformed metadata for "${spec.name}"`,
{ hint: 'This might mean that the package was unpublished from the registry' }
@@ -128,6 +137,9 @@ export function pickVersionByVersionRange (
let versions = Object.keys(meta.versions)
if (publishedBy) {
if (meta.time == null) {
throw new PnpmError('MISSING_TIME', `The metadata of ${meta.name} is missing the "time" field`)
}
versions = versions.filter(version => new Date(meta.time![version]) <= publishedBy)
if (!versions.includes(latest)) {
latest = undefined