Skip to content

PowerShell completions installation does not work as documented #3128

@l0calh05t

Description

@l0calh05t

Problem

Depending on the particular setup, the installation either doesn't work as documented (incorrect profile file, so it has no effect) or causes an error when a new PowerShell instance is opened, if the profile file is correct but not empty (in my case, it contained the single line Import-Module posh-git):

ParserError: D:\Users\<xxxxxxxx>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:4
Line |
   4 |  using namespace System.Management.Automation
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | A 'using' statement must appear before any other statements in a script.

For completeness sake, the output of pwsh --version is PowerShell 7.3.1

Steps

  1. Use the new PowerShell pwsh instead of the bundled PowerShell OR have your Documents library set to a different path

  2. Install rustup PowerShell completions by following the instructions given by rustup completions --help

    Test-Path $profile # shows, true, so I skip New-Item -path $profile -type file -force
    rustup completions powershell >> ${env:USERPROFILE}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  3. Open a new PowerShell instance and try to use the completions

Possible Solution(s)

On my system, ${env:USERPROFILE}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 does not match $profile (or $profile.CurrentUserAllHosts). So in general, the command has no effect except creating an unused file. At the very, least, the documentation should be corrected to suggest running rustup completions powershell >> $profile and remove the text about New-Item -path $profile -type file -force creating the above file (it doesn't).

Since the generated completions are incompatible with a non-empty profile, I would go further to remove the suggestion of appending to the profile entirely, and suggest something like:

rustup completions powershell > "$(Split-Path $profile)\rustup_completions.ps1"
Write-Output ". `"`$PSScriptRoot\rustup_completions.ps1`"" >> $profile

Addendum: Instead of $profile which evaluates to $profile.CurrentUserCurrentHost, it may make sense to also suggest $profile.CurrentUserAllHosts as an alternative, although even that will not necessarily affect all PowerShell versions, since pwsh has its profile files under D:\Users\<xxxxxxxx>\Documents\PowerShell\ for me, while the bundled powershell has its profiles under D:\Users\<xxxxxxxx>\Documents\WindowsPowerShell\, neither of which matches the documented ${env:USERPROFILE}\Documents\WindowsPowerShell because I have my Documents library on a different drive:

Screenshot of the properties dialog for the Documents library showing the Location tab with a non-default location on drive D (with part of the path redacted)

Notes

Regarding the second part of the bug, I have attempted to move the output into a separate file rustup_completions.ps1 in the same directory as my profile script and sourcing it via . "$PSScriptRoot\rustup_completions.ps1". This still has no effect. The code is definitely run as checked by adding a Write-Output to the script, suggesting a third part to this bug. Update: The completions work when written to a separate file and sourced, but partial completions are unsupported since rustup uses clap 2 and partial completions are only supported since 3.0.6. This annoyance would be solved with #3094

Rustup version

rustup 1.25.1 (bb60b1e89 2022-07-12)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.66.0 (69f9c33d7 2022-12-12)`

Installed toolchains

Default host: x86_64-pc-windows-msvc
rustup home:  C:\Users\<xxxxxxxx>\.rustup

installed toolchains
--------------------

stable-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-msvc
1.34.0-x86_64-pc-windows-msvc

installed targets for active toolchain
--------------------------------------

armv7-unknown-linux-gnueabihf
nvptx64-nvidia-cuda
thumbv6m-none-eabi
wasm32-wasi
x86_64-pc-windows-gnu
x86_64-pc-windows-msvc

active toolchain
----------------

stable-x86_64-pc-windows-msvc (default)
rustc 1.66.0 (69f9c33d7 2022-12-12)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions