Skip to content

Commit

Permalink
fix: when resolution fails, print the project directory path (#5456)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Oct 5, 2022
1 parent 86bd1b1 commit e8a631b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-rice-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/error": minor
---

Add new optional field: prefix.
7 changes: 7 additions & 0 deletions .changeset/twelve-ties-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pnpm/default-reporter": patch
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---

When a direct dependency fails to resolve, print the path to the project directory in the error message.
1 change: 1 addition & 0 deletions packages/core/test/install/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ test('fail if none of the available resolvers support a version spec', async ()
err = _err
}
expect(err.code).toBe('ERR_PNPM_SPEC_NOT_SUPPORTED_BY_ANY_RESOLVER')
expect(err.prefix).toBe(process.cwd())
expect(err.pkgsStack).toStrictEqual(
[
{
Expand Down
8 changes: 6 additions & 2 deletions packages/default-reporter/src/reportError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ const colorPath = chalk.gray
export default function reportError (logObj: Log, config?: Config) {
const errorInfo = getErrorInfo(logObj, config)
let output = formatErrorSummary(errorInfo.title, logObj['err']['code'])
if (logObj['pkgsStack']?.length) {
output += `\n\n${formatPkgsStack(logObj['pkgsStack'])}`
if (logObj['pkgsStack'] != null) {
if (logObj['pkgsStack'].length > 0) {
output += `\n\n${formatPkgsStack(logObj['pkgsStack'])}`
} else if (logObj['prefix']) {
output += `\n\nThis error happened while installing a direct dependency of ${logObj['prefix'] as string}`
}
}
if (errorInfo.body) {
output += `\n\n${errorInfo.body}`
Expand Down
16 changes: 12 additions & 4 deletions packages/default-reporter/test/reportingErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ test('prints suggestions when an internet-connection related error happens', (do
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_BAD_TARBALL_SIZE', 'Actual size (99) of tarball (https://foo) did not match the one specified in \'Content-Length\' header (100)')}
expect(output).toBe(`/project-dir:
${formatError('ERR_PNPM_BAD_TARBALL_SIZE', 'Actual size (99) of tarball (https://foo) did not match the one specified in \'Content-Length\' header (100)')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing the dependencies of foo@1.0.0
${ERROR_PAD}
Expand All @@ -150,6 +151,7 @@ ${ERROR_PAD}For instance, \`pnpm install --fetch-retries 5 --network-concurrency
})

const err = new PnpmError('BAD_TARBALL_SIZE', 'Actual size (99) of tarball (https://foo) did not match the one specified in \'Content-Length\' header (100)')
err.prefix = '/project-dir'
err.pkgsStack = [
{
id: 'registry.npmjs.org/foo/1.0.0',
Expand Down Expand Up @@ -355,13 +357,14 @@ test('prints error even if the error object not passed in through the message ob
})
})

test('prints error without packages stacktrace when pkgsStack is empty', (done) => {
test('prints error without packages stacktrace when pkgsStack is empty but do print the project directory path', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})

const err = new PnpmError('SOME_ERROR', 'some error')
err.prefix = '/project-dir'
err.pkgsStack = []
logger.error(err, err)

Expand All @@ -371,7 +374,10 @@ test('prints error without packages stacktrace when pkgsStack is empty', (done)
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(formatError('ERR_PNPM_SOME_ERROR', 'some error'))
expect(output).toBe(`/project-dir:
${formatError('ERR_PNPM_SOME_ERROR', 'some error')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing a direct dependency of /project-dir`)
},
})
})
Expand Down Expand Up @@ -413,6 +419,7 @@ test('prints error with packages stacktrace - depth 2', (done) => {
})

const err = new PnpmError('SOME_ERROR', 'some error')
err.prefix = '/project-dir'
err.pkgsStack = [
{
id: 'registry.npmjs.org/foo/1.0.0',
Expand All @@ -433,7 +440,8 @@ test('prints error with packages stacktrace - depth 2', (done) => {
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_SOME_ERROR', 'some error')}
expect(output).toBe(`/project-dir:
${formatError('ERR_PNPM_SOME_ERROR', 'some error')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing the dependencies of foo@1.0.0
${ERROR_PAD} at bar@1.0.0`)
Expand Down
1 change: 1 addition & 0 deletions packages/error/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default class PnpmError extends Error {
public readonly code: string
public readonly hint?: string
public attempts?: number
public prefix?: string
public pkgsStack?: Array<{ id: string, name: string, version: string }>
constructor (
code: string,
Expand Down
1 change: 1 addition & 0 deletions packages/resolve-dependencies/src/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ async function resolveDependency (
})
return null
}
err.prefix = options.prefix
err.pkgsStack = nodeIdToParents(options.parentPkg.nodeId, ctx.resolvedPackagesByDepPath)
throw err
}
Expand Down

0 comments on commit e8a631b

Please sign in to comment.