When a command is not found, pwsh helpfully tries to suggest other commands ("Suggestion [4,General]: The most similar commands are: blah blah blah"). That's cool. But if I'm suppressing the error, then I probably don't want that suggestion to show up, either. I.e. I've written a script that does something where the command might not exist, so I've used -ErrorAction Ignore, and so the user runs my script, and things seem to mostly work, but they end up with this weird, "random" suggestion text in the output, which confuses and worries them.
And side note, I thought -ErrorAction Ignore should also make the error not show up in $error... but it is... is that specific to just this cmdlet that I ran into it with (-PSRepository commands), or is my expectation wrong, or what?
$error.clear()
Unregister-PSRepository asdfadsfadsf -ErrorAction Ignore
$error
<no output>
Get-Command: C:\tools\pwsh\7\Modules\PowerShellGet\PSModule.psm1:12812
Line |
12812 | … $nugetCmd = Microsoft.PowerShell.Core\Get-Command -Name $script:NuGet …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The term 'NuGet.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Suggestion [4,General]: The most similar commands are: net.exe, net1.exe, nltest.exe, quser.exe, reset.exe, subst.exe, git.exe, alg.exe, at.exe, CCG.exe.
(this is actually the most recent CI build that I could find--very fresh!)
Name Value
---- -----
PSVersion 7.0.0-preview.7
PSEdition Core
GitCommitId 7.0.0-preview.7
OS Microsoft Windows 10.0.19570
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
This looks like the issue here isn't actually the suggestion being shown... Suggestions are shown if any errors show up in $error.
I suspect the reason the error turns up is to do with how common parameters like -ErrorAction are handled when a command inside a module calls a command from another module. If I'm not mistaken, it might be a known issue that such parameters aren't always passed properly when calling commands in this way?
Don't see the issue right now, but I'm sure I've seen it before somewhere...
Thanks vexx32.
Suggestions are shown if any errors show up in $error
That doesn't seem like the right way to do it. For example, if I instead use -ErrorAction SilentlyContinue, I do expect the error to go into $error, but not show up in output. And if the error is not showing up in output, then I don't know why we'd want to show the suggestion--there's no context for the user to understand why in the world this suggestion is being given.
Yep, I'd definitely agree. 🙂
I have a WIP PR #9115 that addresses that by adding a Suggestion property onto the ErrorRecord itself and having the suggestion be shown as part of the error message formatting itself. I've not had time to finish it, but @SteveL-MSFT has expressed interest in continuing that work.