Today, to enable a feedback provider or a tab-completer for a native command, a user has to import a module or run a script manually, or do that in their profile. There is no way to auto-discover a feedback provider or a tab-completer for a specific native command.
As a tool author, I want to provide a feedback provider or a tab-completer along with my tool installation, but I don't want to mess with user's profile to make the feedback provider or tab-completer discoverable.
As a user, I want my feedback providers and tab-completers for the specific native commands to be loaded lazily, instead of having to use my profile to load them at session startup.
Relevant GitHub issues:
For PowerShell commands (Function or Cmdlet), I presume the completion or feedback support, if there is any, will be from the same module. So, when the command becomes available, the feedback provider and/or tab-completer will become available too.
Therefore, the auto-discovery for feedback provider and tab-completer targets native commands only.
- A tool can deploy its feedback provider and/or tab-completer without needing to update a file at a central location, such as the user's profile.
- A tool can remove its deployment cleanly without needing to update a file at a central location.
- PowerShell can discover feedback providers and tab-completers automatically, and load one based on the right trigger.
- A user can enable or disable the auto-discovery for a feedback provider or tab-completer.
The proposal is to adopt the existing mechnism used in Bash, Zsh and Fish's completion system -- have directories contain individual completion scripts for various commands and applications, whose file names should match names of the commands. Those completion scripts are loaded only when their corresponding commands' completion is triggered for the first time.
We will have separate directories for feedback providers and tab-completers, for 2 reasons:
- Trigger is different
- feedback provider for a specific native command -- execute the command will trigger the discovery.
- tab-completer for a specific native command -- tab complete on the command will trigger the discovery.
- Implementation is different
- feedback provider -- a module or an assembly (binary implementation only)
- tab-completer -- a module or a script (binary or script implementation)
The folders for feedback providers and tab-completers will be placed under the same path where modules folders are currently located:
- In-box path:
$PSHOME/Feedbacksand$PSHOME/Completions - Current user path:
<personal>/PowerShell/Feedbacksand<personal>/PowerShell/Completions - All user path:
<shared>/PowerShell/Feedbacksand<shared>/PowerShell/Completions
There are 2 kinds of feedback providers:
-
One that covers different commands and different scenarios. For example, the WinGet CommandNotFound feedback provider acts on
"CommandNotFound"error and suggests theWinGetinstallation command if the tool that user tried to execute can be installed withWinGet. -
One that convers a specific command. For example, a feedback provider for
git.
The 1st kind doesn't have a specific corresponding trigger, so the auto-discovery for it only happens at the startup of a session.
The 2nd kind can use the native command name as the specific trigger, and the auto-discovery happens when the command gets executed.
The strucutre of the Feedbacks folder is as follows:
Feedbacks
│
├───_startup_
│ ├───linux-command-not-found
│ │ linux-command-not-found.json
│ │
│ └───winget-command-not-found
│ winget-command-not-found.json
├───git
│ git.json
│
├───kubectl
│ kubectl.json
│
├───...
Each item within Feedbacks is a folder.
_startup_: feedback providers declared in this folder will be auto-loaded at the startup of an interactiveConsoleHostsession.gitorkubectl: feedback provider for a specific native command should be declared in a folder named after the native command. It will be auto-loaded when the native command gets executed.
Within each sub-folder, a JSON file named after the folder name should be defined to configure the auto-discovery for the feedback provider.
About the module key:
- Its value could be the path to an assembly DLL, in which case the DLL will be loaded as a binary module.
- The processing order is
- First, expand the string. So, the value could contain PowerShell variables such as
$env:ProgramData. - Second, try resolving the value as a path using PowerShell built-in API.
- if it's successfully resolved as a path, use the path for module loading; Otherwise
- if the value contains directory separator, then discovery fails; Otherwise
- use the expanded value as the module name for module loading
- First, expand the string. So, the value could contain PowerShell variables such as
- At startup, auto-load the enabled feedback providers from the
_startup_folder of theFeedbackspaths. Do the auto-loading before processing user's profile. - Report what feedback providers were processed and the time taken, in the streaming way.
- In
NativeCommandProcessor, when a native command gets executed,- Check if a feedback provider for this native command is available in the
Feedbackspaths; - If so, check if the specified module already loaded;
- If not, load it at the
DoCompletephase, so the feedback provider will be ready right after the native command finishes.
- Check if a feedback provider for this native command is available in the
[NOTE:] It would be better if we can check if a feedback provider for the native command has already registered before checking the paths, but unlike the completer registration for native commands, today there is no mapping between a feedback provider and a native command. Also, it's allowed to register multiple feedback providers for a single native command.
-
Should we expand the string value for
modulekey, or always treat the value as literal? It feels like a useful feature, but could come with security implication, especially in WDAC/AppLocker enforced environment.- Note: PowerShell data file (
.psd1) doesn't allow environment variables.
- Note: PowerShell data file (
-
Should we add another key to indicate the target OS?
- A feedback provider may only work on a specific OS, such as the
"WinGet CommandNotFound"feedback provider only works on Windows. - Such a key could be handy if a user wants to share the feedback/tab-completer configurations among multiple machines via a cloud drive.
- A feedback provider may only work on a specific OS, such as the
-
Do we really need a folder for each feedback provider? For example, can we simply have the files
git.jsonandkubectl.jsonunder theFeedbacksfolder, and the fileslinux-command-not-found.jsonandwinget-command-not-found.jsonunder the_startup_folder?- Since it's possible to have non-module feedback provider that comes with a DLL only,
then the DLL might need to be deployed along with the configuration.
In that case, the tool that deploys the DLL will either copy the DLL directly to
Feedbacks, or create a sub-folder named after the tool. Having a folder for each feedback provider makes sense in that case. - We will need a folder for each tab-completer because the completion implementation may just be a script file that needs to be deployed along with the completer's configuration. It's good to keep consistency in the folder structure for both feedback provider and tab-completer.
- Since it's possible to have non-module feedback provider that comes with a DLL only,
then the DLL might need to be deployed along with the configuration.
In that case, the tool that deploys the DLL will either copy the DLL directly to
Similarly, there are 2 kinds of tab-completers for native commands:
-
One that covers a wide range of native commands. For example, the Unix Completion module dynamically detects all the native commands that Bash or Zsh has built-in completion for on a Linux or macOS machine, and support the same completion for them in PowerShell.
-
One that covers a specific native command. For example, the AzCLI tab completion script.
The 1st kind doesn't have a specific corresponding trigger, so the auto-discovery for it only happens at the startup of a session.
The 2nd kind can use the native command name as the specific trigger, and the auto-discovery happens when user tab completes on the command.
The strucutre of the Completions folder is as follows:
Completions
│
├───_startup_
│ └───unix-completer
│ unix-completer.json
├───git
│ git.json
│
├───az
│ az.json
│
├───...
Each item within Completions is a folder.
_startup_: tab-completer declared in this folder will be auto-loaded at the startup of an interactiveConsoleHostsession.gitoraz: tab-completer for a specific native command should be declared in a folder named after the native command. It will be auto-loaded when user tab completes on the command.
Within each sub-folder, a JSON file named after the folder name should be defined to configure the auto-discovery for the tab-completer.
{
"module": "<module-name-or-path>", // Module to load to register the completer.
"script": "<script-path>", // Script to run to register the completer.
"disable": false, // Control whether auto-discovery should find this completer.
}About the module and script keys:
modulekey take precedence. So, if both keys are present,scriptwill be ignored.- For
module, use the same resolution as in theFeedback Providersection above. - For
script, its value must be a.ps1file- If it fails to resolve as a
.ps1file, the auto-discovery fails; - Otherwise, run the script. (the script may run in PSReadLine's module scope, will that be a problem? How to run it in the top-level session?)
- If it fails to resolve as a
- At startup, auto-load the enabled tab-completers from the
_startup_folder of theCompletionspaths. Do the auto-loading before processing user's profile. - Report what tab-completers were processed and the time taken, in the streaming way.
- In the completion system, when tab completion is triggered on a native command,
- Check if a tab-completer for this native command already exists;
- If not, check if a tab-completer for this native command is available in the
Completionspaths; - If so, register it either by loading the module or running the script, and then call the tab-completer.
[NOTE:] For a specific native command, you can only have 1 active completer for it. So, we will check if a completer already exists for a native command before searching in the paths.
Same discussions as in Feedback Provider section:
- Should we expand the string value for
moduleandscriptkeys, or always treat the value as literal? - Should we add another key to indicate the target OS?
Different discussions:
-
Do we really need a folder for each feedback provider?
- [dongbo] Yes, I think we need.
Appx and MSIX packages on Windows have many constraints that make it difficult to integrate with a broader plugin ecosystem. The way for such an Appx/MSIX tool to expose tab-completer could be just running the tool with a special flag, such as
<tool> --ps-completion, to output some PowerShell script text for the user to run. In that case, the user will need to manually save the script text to a file and place the file next to thetool.jsonfile. Having a folder is useful to group them together in that case.
- [dongbo] Yes, I think we need.
Appx and MSIX packages on Windows have many constraints that make it difficult to integrate with a broader plugin ecosystem. The way for such an Appx/MSIX tool to expose tab-completer could be just running the tool with a special flag, such as
-
When running a script, it will run in the PSReadLine's module scope when completion is triggered from within PSReadLine. Will that be a problem? How to run it in the top-level session instead?
- [dongbo] This is more of a implementation-detail question. It's not a problem for module thanks to PowerShell/PowerShell#24909.
-
Today, a user can unregister a tab-completer by running
Register-ArgumentCompleter -CommandName <name> -ScriptBlock $null. But it doesn't remove the<name>from cache, but only sets its value tonull. We probably should fix it to make our cache lookup simpler. -
Today, a user cannot see what commands have tab-completer registered. This may be out-of-scope for this RFC.
This feature is only for interactive session, so we need to decide on when the feature is enabled and when is disabled.
- The feature only works when the PowerShell host is
ConsoleHostor theVisual Studio Code Host(which usesConsoleHostinternally IIRC).- Today, only
ConsoleHostconsumes feedback providers.
- Today, only
- The feature should be disabled when PowerShell starts with
-Commandor-Fileto run a command or a file and then terminates.- So, when
-noexitis specifeid, the feature should be enabled if other conditions are satisfied.
- So, when
- Shall we disable the feature with the
-noninteractiveflag?PSReadLineis disabled when this flag is specified, so maybe this feature should be disabled too.
- Shall we disable the feature with the
-noprofileflag?- The "loading at startup" part of the feature is similar to how profiles are processed, but it's not part of the profile.
- Shall we add a flag (or flags) to allow user to disable this feature (or disable
feedbackandcompleterseparately)? - We report progress when loading
feedbackorcompleterat startup, so how to allow users to disable the progress report?- We have the
-NoProfileLoadTimeflag today to not show the time taken for running profile.
- We have the
- How about on a WDAC/AppLocker enforced environment?
{ "module": "<module-name-or-path>", // Module to load to register the feedback provider. "disable": false, // Control whether auto-discovery should find this feedback provider. }