-
-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: friendlier error message when command not found #6887
Changes from 14 commits
f85ef36
0a8395b
9c4f092
d0d079d
75df757
080011f
9faf897
a989e62
bd087bc
092e6cf
653e0fd
c797936
00028e9
132487f
54609d1
9042721
6e89548
ea5a008
885c5ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@pnpm/plugin-commands-script-runners": patch | ||
"pnpm": patch | ||
--- | ||
|
||
Make the error message when user attempting to run a command that does not exist friendlier |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import { | |
import { PnpmError } from '@pnpm/error' | ||
import which from 'which' | ||
import writeJsonFile from 'write-json-file' | ||
import { buildCommandNotFoundHint } from './buildCommandNotFoundHint' | ||
import { getNearestProgram, getNearestScript } from './buildCommandNotFoundHint' | ||
|
||
export const shorthands = { | ||
parallel: runShorthands.parallel, | ||
|
@@ -133,6 +133,7 @@ export async function handler ( | |
shellMode?: boolean | ||
resumeFrom?: string | ||
reportSummary?: boolean | ||
implicitlyFellbackFromRun?: boolean | ||
} & Pick<Config, 'extraBinPaths' | 'extraEnv' | 'lockfileDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>, | ||
params: string[] | ||
) { | ||
|
@@ -212,7 +213,34 @@ export async function handler ( | |
result[prefix].duration = getExecutionDuration(startTime) | ||
} catch (err: any) { // eslint-disable-line | ||
if (await isErrorCommandNotFound(params[0], err)) { | ||
err.hint = buildCommandNotFoundHint(params[0], (await readProjectManifestOnly(opts.dir)).scripts) | ||
err.message = `Command "${params[0]}" not found` | ||
if (opts.implicitlyFellbackFromRun) { | ||
let nearestScript: string | null | undefined | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this try needed? There is already a try/catch in getNearestProgram There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I think |
||
nearestScript = getNearestScript(params[0], (await readProjectManifestOnly(opts.dir)).scripts) | ||
} catch (_err) {} | ||
if (nearestScript) { | ||
err.hint = `Did you mean "pnpm ${nearestScript}"?` | ||
} else { | ||
const nearestProgram = await getNearestProgram({ | ||
programName: params[0], | ||
dir: opts.dir, | ||
workspaceDir: opts.workspaceDir, | ||
}) | ||
if (nearestProgram) { | ||
err.hint = `Did you mean "pnpm ${nearestProgram}"?` | ||
} | ||
} | ||
} else { | ||
const nearestProgram = await getNearestProgram({ | ||
programName: params[0], | ||
dir: opts.dir, | ||
workspaceDir: opts.workspaceDir, | ||
}) | ||
if (nearestProgram) { | ||
err.hint = `Did you mean "pnpm exec ${nearestProgram}"?` | ||
} | ||
} | ||
} else if (!opts.recursive && typeof err.exitCode === 'number') { | ||
exitCode = err.exitCode | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is OK if you just use readdir synchronously here and make the function synchronous. We don't care about the performance here so much.