fix: avoid printing undefined for package version when reporting error (#9153)

This commit is contained in:
Brandon Cheng
2025-02-23 18:21:21 -05:00
committed by GitHub
parent 43681529cf
commit 5df8de7180
2 changed files with 23 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/default-reporter": patch
---
Fix undefined being printed in install errors when a package does not have a version field.

View File

@@ -103,10 +103,24 @@ function getErrorInfo (logObj: Log, config?: Config, peerDependencyRules?: PeerD
return { title: logObj.message! }
}
function formatPkgsStack (pkgsStack: Array<{ id: string, name: string, version: string }>) {
interface PkgStackItem {
readonly id: string
readonly name: string
// The version may be missing if this was a private workspace package without
// the version field set.
readonly version?: string
}
function formatPkgNameVer ({ name, version }: PkgStackItem) {
return version == null
? name
: `${name}@${version}`
}
function formatPkgsStack (pkgsStack: readonly PkgStackItem[]) {
return `This error happened while installing the dependencies of \
${pkgsStack[0].name}@${pkgsStack[0].version}\
${pkgsStack.slice(1).map(({ name, version }) => `${EOL} at ${name}@${version}`).join('')}`
${formatPkgNameVer(pkgsStack[0])}\
${pkgsStack.slice(1).map((pkgInfo) => `${EOL} at ${formatPkgNameVer(pkgInfo)}`).join('')}`
}
interface PackageMeta {
@@ -489,7 +503,7 @@ protocol are replaced with real specifiers on 'pnpm publish'.
This is likely a bug in the publishing automation of this package. Consider filing
a bug with the authors of:
${highlight(`${problemDep.name}@${problemDep.version}`)}
${highlight(formatPkgNameVer(problemDep))}
`
}