Integrate Your Plugin With the doctor Command
Salesforce CLI includes the doctor command that you can run if you're having issues with the CLI. The command inspects your CLI installation and environment, runs diagnostic tests, provides suggestions, and writes the data to a local diagnostic file. See the Salesforce CLI Command Reference and Setup Guide for more information about the doctor command.
To help your users troubleshoot problems with your plugin, you can integrate it into the doctor command so it runs your custom diagnostic tests. You can add custom information to both the Running all diagnostics and Suggestions sections and to the JSON results file. For example:
The main reason is to provide your users a quick and potential fix to an issue, and thus provide a better experience. Because you have a deep understanding of your plugin, you can likely predict potential problems your customers will run into. With the diagnostic tests, you can inspect their CLI and plugin configuration and suggest immediate actions if your users run into these predicted issues. If the suggestions don't fix the problem, the doctor gathers the required information that users attach to GitHub issues or provide to Salesforce Customer Support.
Writing diagnostic tests isn't a replacement for good command error handling. Rather, it's a way to educate and clarify command usage within a complex system.
Examples of diagnostic tests include checking for:
- An environment variable that changes the behavior of your plugin or a library that your plugin uses.
- A setting within a project that changes plugin behavior.
- Required dependencies (outside of NPM) that are missing or out of date.
- A system that is required by your plugin that is unavailable.
- Anything that your customers regularly need clarification on that the commands can't handle directly.
See the diagnostic test in the deploy and retrieve plugin for a real-life example. Also see the source code for the doctor command.
-
Define a hook in the
oclifsection of your plugin'spackage.jsonwith this pattern, where<plugin-name>is thenameproperty in yourpackage.jsonfile:For example, the
plugin-deploy-retrievediagnostic hook is defined with this JSON snippet. The Typescript source file and directory for the hook handler issrc/hooks/diagnostics.ts: -
If you coded your plugin in TypeScript, add a
devDependenciesentry inpackage.jsonthat points to@salesforce/plugin-info, which contains the requiredSfDoctortype. Use the latest version ofplugin-info. For example: -
In the hook handler source file (
src/hooks/diagnostics.tsin our example), define and export ahookfunction that runs after thedoctorcommand is executed. Here's a basic Typescript example that includes one diagnostic test calledtest1: -
Code the
hookfunction to return the result of all your plugin's diagnostic tests usingPromise.all([test1(), test2(), test3()]);. -
Use
doctor.addPluginData()in your diagnostic tests to add JSON data to the fulldoctordiagnostics report. This JSON data appears in thepluginSpecificDatasection of the JSON report. For example, this code in the hook handler:Results in this JSON snippet in the full
doctordiagnostic report: -
Use the global singleton
Lifecycleclass in your diagnostic tests to include the test name and status to the beginning section of thedoctorcommand output. For example:You must name the event
Doctor:diagnosticand the payload must have this shape:{ testName: string, status: string }, wherestatusis one of'pass' | 'fail' | 'warn' | 'unknown' -
Use the
doctor.addSuggestion()method in your diagnostic tests to add suggestions, based on the test results, to the last part of thedoctoroutput. For example: -
Diagnostic tests can reference the command run by the
doctorand all generated command output from thedoctordiagnostics. For example: