Skip to content

Commit

Permalink
fix: print a better error message when "time" is missing from metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 7, 2024
1 parent 01a4566 commit bb90faf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/ninety-jokes-watch.md
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions cli/default-reporter/src/reportError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
14 changes: 13 additions & 1 deletion resolving/npm-resolver/src/pickPackageFromMeta.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb90faf

Please sign in to comment.