Powershell: Suggestions shown without error

Created on 30 Jan 2019  路  20Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Suggestions shown although command runs fine. This seems to occur only on Linux - not Windows or macOS.

pwsh --version
PowerShell 6.2.0-preview.4

Suggestion [4,General]: The most similar commands are: popd, sp, spps, ps, pip2, pip3, pip, pppd, apg, ps2ps.

Expected behavior

pwsh --version
PowerShell 6.2.0-preview.4

Actual behavior

pwsh --version
PowerShell 6.2.0-preview.4

Suggestion [4,General]: The most similar commands are: popd, sp, spps, ps, pip2, pip3, pip, pppd, apg, ps2ps.

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.4
PSEdition                      Core
GitCommitId                    6.2.0-preview.4
OS                             Linux 4.18.0-13-generic #14-Ubuntu SMP Wed Dec 5 09:04:24 UTC 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Bug

Most helpful comment

After discussing with @JamesWTruher and @PaulHigin it seems the best approach currently is to put the fuzzy suggestion behind an experimental flag because the proper fix is too complicated:

  1. Currently, suggestions are initiated by the host (consolehost in this case) and should be part of the ErrorRecord so that it works correctly remotely and in other hosts
  2. ErrorRecord has a member called RecommendedAction that should contain the suggestion
  3. The code to invoke getting suggestions needs to be moved out of consolehost and closer to where the ErrorRecord is created
  4. Formatter for ErrorRecord needs to be updated to show the RecommendedAction member
  5. Suggestions should be in its own stream or Information stream rather than written directly to the host

All 20 comments

Perhaps this happens for all native commands.

@thorstenkampe: Does it indeed happen for all external programs in $env:PATH, as @iSazonov suggests?

I can't reproduce the problem on Ubuntu 18.04. What specific version are you on?

I'm on Ubuntu 18.10. It actually does happen for all commands.

@SteveL-MSFT Please look this feedback.

I figured it out. It's caused by the following entry in my profile:

Get-Command -Name pspg -CommandType Application -ErrorAction SilentlyContinue

On my Linux machine the command pspg is not available. The suggestions (popd, sp, spps, ps, pip2, pip3, pip, pppd, apg, ps2ps) actually do refer to pspg - not the command I entered manually.

Can we close the issue?

This is obviously a bug. Suggestions are only meaningful in relation to manually entered commands - not commands in a script or profile file.

In my case I enter any command and in response I get a suggestion even though the command was successful. And the suggestion does not even relate to the command I entered.

A general comment: the issue seems to be a misunderstanding of what suggestions are for. Suggestions should be a response to "user entered a command but I, PowerShell cannot find that command. Did the user misspell that command?" - not "user entered a valid command but that command generated an error".

@thorstenkampe Thanks for your great feedback!

@thorstenkampe:

Let me try to summarize the bugs / undesired behavior:

  • Suggestions are triggered not only in an _interactive_ session, but unexpectedly also from _scripts_.

  • Use of Get-Command with a nonexistent command unexpectedly triggers suggestions too; arguably, only _direct invocation_ should do that (yes: nosuch; no: Get-Command nosuch).

    • Additionally, the suggestion appears even with -ErrorAction SilentlyContinue, because the suggestion mechanism is apparently based on errors recorded in $Error; therefore, only -ErrorAction Ignore, which suppressed adding to $Error, is currently effective in silencing the suggestion.
  • A specific bug occurs when a failed lookup occurs anywhere in $PROFILE _and_ no unrelated errors are added to $Error _after that_ during the execution $PROFILE: in an _interactive session_, display of the suggestion is then _delayed until after $PROFILE has finished loading_, and is only printed whenever the _next command is executed_, whatever it is, and whether it is an external program or not (e.g., Get-Date would trigger it too).

Perfect summary, well done.

@mklement0 is correct that the current "suggestions framework" (which existed before my fuzzy matching suggestion feature) has a trigger based on an ErrorRecord being produced. So in that sense, this is "by-design". -ErrorAction Ignore is the correct way to suppress this. Currently, the console host only knows if the session is interactive or not based on a command line switch and doesn't differentiate execution of $profile from when the user can start typing. Since profile execution and suggestions are both in the console host, it seems that it should be easy to pass some data to suppress suggestions while profile is executing. I'll take a look.

@thorstenkampe I'm actually not able to get this to repro. I have this in my $profile:

gcm fsdf -erroraction silentlycontinue

I start pwsh-preview --version:

PowerShell 6.2.0-preview.4

If I execute that script directly . $profile, I do get the suggestion shown.

@SteveL-MSFT is the behaviour the same if you first open pwsh-preview and then at the prompt enter pwsh-preview --version?

@vexx32 I don't see the suggestion output that way either. Does it repro for you?

Not on Windows, at least. I was just curious. :)

Here's how you can reproduce the bug reliably on all platforms:

  • Add the following, intentionally nonexistent command _at the end_ of your $PROFILE: nosuch

  • Start a new interactive session - you'll see NO suggestion at that point (though you'll see the error).

  • Run _any_ command (e.g., whoami), at which the nosuch-related suggestion finally appears.

@mklement0 thanks, that does repro it!

Looking at the code, this isn't so straight forward to fix and isn't just profile. Simple repro:

"gcm asldfj -erroraction silentlycontinue" > test.ps1
./test.ps1

I would not expect the suggestion to be there, but the way the code currently works is that the console checks if the last command had any output and if so, it calls to evaluate suggestions. Suggestions first checks of $? is $false otherwise no suggestion. If something failed, it goes through the suggestion filters where one of them looks at the last ErrorRecord which is what the fuzzy match command uses. Ideally, script invocation shouldn't show suggestions, but the console host doesn't know anything about what it is executing and just sends it to PowerShell to run.

After discussing with @JamesWTruher and @PaulHigin it seems the best approach currently is to put the fuzzy suggestion behind an experimental flag because the proper fix is too complicated:

  1. Currently, suggestions are initiated by the host (consolehost in this case) and should be part of the ErrorRecord so that it works correctly remotely and in other hosts
  2. ErrorRecord has a member called RecommendedAction that should contain the suggestion
  3. The code to invoke getting suggestions needs to be moved out of consolehost and closer to where the ErrorRecord is created
  4. Formatter for ErrorRecord needs to be updated to show the RecommendedAction member
  5. Suggestions should be in its own stream or Information stream rather than written directly to the host
Was this page helpful?
0 / 5 - 0 ratings