click-contrib / click-completion

Add or enhance bash, fish, zsh and powershell completion in Click
MIT License
288 stars 32 forks source link

Install fails when running PowerShell from emulated terminal cmder #20

Open pstephenson02 opened 5 years ago

pstephenson02 commented 5 years ago

First of all, thank you for this library - it's great.

When running PowerShell, cmder by default invoke's the shell with a few parameters:

*PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. ''%ConEmuDir%\..\profile.ps1'''"

(these can be found in the cmder Settings)

These parameters affect click-completion install a couple of ways:

  1. The above command sets the ExecutionPolicy on the Process scope to Bypass. When click-completion's install command runs, it tries to set the ExecutionPolicy to Unrestricted on the CurrentUser scope. We end up with this:
    
    Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more
    specific scope.  Due to the override, your shell will retain its current effective execution policy of Bypass. Type "Get-ExecutionPolicy -List" to
    view your execution policy settings. For more information please see "Get-Help Set-ExecutionPolicy".
    At line:1 char:1
    + Set-ExecutionPolicy Unrestricted -Scope CurrentUser
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
    Traceback (most recent call last):

... File "c:\code\click-completion\click_completion\core.py", line 353, in install subprocess.check_call(['powershell', 'Set-ExecutionPolicy Unrestricted -Scope CurrentUser']) File "c:\users\pstephenson.aa\appdata\local\programs\python\python37-32\lib\subprocess.py", line 328, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['powershell', 'Set-ExecutionPolicy Unrestricted -Scope CurrentUser']' returned non-zero exit status 1.


2. cmder also defines its own `user_profile.ps1` file that, by default, does *not* load the PowerShell profile defined in `$profile`. This has been noted in some [GitHub issues](https://github.com/cmderdev/cmder/issues/505), but it's also relevant for this project because it means that while the completion code gets added to the `$profile` file, you won't get the expected behavior without dot sourcing $profile each time you load a new cmder window.

I am happy to submit some documentation for cmder users to look out for these things. However, I wanted to ask your thoughts on the first issue: One thing to consider might be to before setting the execution policy, you could first check the execution policy on the Process scope to see if it's anything other than `Undefined` or `Unrestricted`: https://github.com/click-contrib/click-completion/blob/master/click_completion/core.py#L353. If that is the case, maybe we throw an exception and display a helpful error message to the user indicating the issue? What are your thoughts?