Skip to content

Commit

Permalink
fix: print a better error message on prepare pkg failure
Browse files Browse the repository at this point in the history
close #5845
  • Loading branch information
zkochan committed Dec 28, 2022
1 parent d71dbf2 commit 5a719c6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
19 changes: 19 additions & 0 deletions cli/default-reporter/src/reportError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const colorPath = chalk.gray
export function reportError (logObj: Log, config?: Config) {
const errorInfo = getErrorInfo(logObj, config)
let output = formatErrorSummary(errorInfo.title, logObj['err']['code'])
if (errorInfo.prepend) {
output = `${errorInfo.prepend}\n${output}`
}
if (logObj['pkgsStack'] != null) {
if (logObj['pkgsStack'].length > 0) {
output += `\n\n${formatPkgsStack(logObj['pkgsStack'])}`
Expand All @@ -36,6 +39,7 @@ export function reportError (logObj: Log, config?: Config) {
function getErrorInfo (logObj: Log, config?: Config): {
title: string
body?: string
prepend?: string
} {
if (logObj['err']) {
const err = logObj['err'] as (PnpmError & { stack: object })
Expand Down Expand Up @@ -69,6 +73,8 @@ function getErrorInfo (logObj: Log, config?: Config): {
case 'ERR_PNPM_FETCH_401':
case 'ERR_PNPM_FETCH_403':
return reportAuthError(err, logObj as any, config) // eslint-disable-line @typescript-eslint/no-explicit-any
case 'ERR_PNPM_PREPARE_PACKAGE':
return reportPreparePackage(err, logObj as any) // eslint-disable-line @typescript-eslint/no-explicit-any
default: {
// Errors with unknown error codes are printed with stack trace
if (!err.code?.startsWith?.('ERR_PNPM_')) {
Expand Down Expand Up @@ -386,6 +392,19 @@ ${foundSettings.join('\n')}`
}
}

function reportPreparePackage (
err: Error,
msg: {
stderr: string
stdout: string
},
) {
return {
title: err.message,
prepend: `STDOUT:\n${msg.stdout}\nSTDERR:\n${msg.stderr}`,
}
}

function hideSecureInfo (key: string, value: string) {
if (key.endsWith('_password')) return '[hidden]'
if (key.endsWith('_auth') || key.endsWith('_authToken')) return `${value.substring(0, 4)}[hidden]`
Expand Down
4 changes: 3 additions & 1 deletion exec/prepare-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export async function preparePackage (opts: { rawConfig: object }, pkgDir: strin
await execa(pm, ['run', scriptName], execOpts)
}
} catch (err: any) { // eslint-disable-line
throw new PnpmError('PREPARE_PKG_FAILURE', err.shortMessage ?? err.message)
err.code = 'ERR_PNPM_PREPARE_PACKAGE'
err.message = err.shortMessage
throw err
}
await rimraf(path.join(pkgDir, 'node_modules'))
}
Expand Down
7 changes: 6 additions & 1 deletion fetching/git-fetcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export function createGitFetcher (createOpts: { gitShallowHosts?: string[], rawC
await execGit(['clone', resolution.repo, tempLocation])
}
await execGit(['checkout', resolution.commit], { cwd: tempLocation })
await preparePkg(tempLocation)
try {
await preparePkg(tempLocation)
} catch (err: any) { // eslint-disable-line
err.message = `Failed to prepare git-hosted package fetched from "${resolution.repo}": ${err.message}`
throw err
}
// removing /.git to make directory integrity calculation faster
await rimraf(path.join(tempLocation, '.git'))
const filesIndex = await cafs.addFilesFromDir(tempLocation, opts.manifest)
Expand Down
8 changes: 6 additions & 2 deletions fetching/tarball-fetcher/src/gitHostedTarballFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ interface Resolution {
export function createGitHostedTarballFetcher (fetchRemoteTarball: FetchFunction, rawConfig: object): FetchFunction {
const fetch = async (cafs: Cafs, resolution: Resolution, opts: FetchOptions) => {
const { filesIndex } = await fetchRemoteTarball(cafs, resolution, opts)

return { filesIndex: await prepareGitHostedPkg(filesIndex as FilesIndex, cafs, rawConfig) }
try {
return { filesIndex: await prepareGitHostedPkg(filesIndex as FilesIndex, cafs, rawConfig) }
} catch (err: any) { // eslint-disable-line
err.message = `Failed to prepare git-hosted package fetched from "${resolution.tarball}": ${err.message}`
throw err
}
}

return fetch as FetchFunction
Expand Down

0 comments on commit 5a719c6

Please sign in to comment.