Skip to content

Command not found should suggest how to install like in bash #1982

Description

@lzybkr

Steps to reproduce

Run a command that's not installed on your Linux machine, e.g. dtrace.

Expected behavior

jasonsh@jasonsh-ubd02:~$ dtrace
The program 'dtrace' is currently not installed. You can install it by typing:
sudo apt-get install systemtap-sdt-dev

Actual behavior

PS> dtrace                                                                                                               
dtrace : The term 'dtrace' 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.
At line:1 char:1
+ dtrace
+ ~~~~~~

Environment data

PS /home/jasonsh> $PSVersionTable                                                                                        

Name                           Value                                                                                    
----                           -----                                                                                    
PSVersion                      6.0.0-alpha                                                                              
PSEdition                      Core                                                                                     
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                  
BuildVersion                   3.0.0.0                                                                                  
GitCommitId                    v6.0.0-alpha.9                                                                           
CLRVersion                                                                                                              
WSManStackVersion              3.0                                                                                      
PSRemotingProtocolVersion      2.3                                                                                      
SerializationVersion           1.1.0.1                                                                                  


PS /home/jasonsh> uname -v                                                                                               
#49~14.04.1-Ubuntu SMP Wed Jun 29 20:22:11 UTC 2016

Activity

  1. rkeithhill commented on Aug 20, 2016

    @rkeithhill
    Collaborator

    That would be nice! Is there some repo/registry that maintains the mapping of "command to pkg name" for the various Linux distro's, as well as the pkg mgr to use (apt-get vs yum vs Install-Module)?

  2. nerdshark commented on Aug 21, 2016

    @nerdshark

    This isn't built-in bash functionality; it's normally defined as an error-handler callback (for error 127) in one of the system-wide shell profiles (probably in /etc/bashrc, /etc/profile, or /etc/profile.d/*).

    Keith Hill (@rkeithhill) It's not likely. Distros that support this kind of functionality generally do two or three things:

    • maintain a database of files owned by installed packages;
    • provide downloadable databases of repositories' package contents;
    • and provide an online, searchable database of package contents.

    I don't think that using or maintaining an unofficial, centralized database of command-to-package mappings or package contents for multiple versions of multiple distros is the right approach. Instead, I think that Powershell should provide relevant error-handler hook points (or use existing ones) in the command loop and let Powershell packagers write callbacks tailored to their specific OS/package manager combination. This is in line with the existing practice above, and should require minimal effort to maintain.

  3. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Sep 29, 2016
  4. added this to the milestone on Mar 13, 2017
  5. SteveL-MSFT commented on Jan 31, 2018

    @SteveL-MSFT
    Member

    Need some api in PSGallery to enable this

  6. BrucePay commented on May 23, 2018

    @BrucePay
    Collaborator

    I think that Powershell should provide relevant error-handler hook points (or use existing ones) in the command loop

    FYI: there's been a "command not found" action available in PowerShell since version 3.

    $executioncontext.InvokeCommand.CommandNotFoundAction = { 
        param ($e, $e2)  
    
       $e2.StopSearch = $true
       $e2.CommandScriptblock = {
           Write-Verbose -Verbose ` 
              "Look for a module containing the command '$e somewhere"}.GetNewClosure()
        }
    }
  7. BrucePay commented on May 24, 2018

    @BrucePay
    Collaborator

    It turns out that the gallery does give you a way to look up modules containing a command Find-Module -Command. So adding that to the hook code, you get something like this:

    $ExecutionContext.InvokeCommand.CommandNotFoundAction = {
        param ($n, $e)
    
        $e.StopSearch = $true
        $e.CommandScriptBlock = {
            Write-Host -ForegroundColor Yellow `
                -Verbose "Command '$n' was not found; searching on-line for modules with this command"
            $result = Find-Module -Command $n
            if ($result)
            {
                $result | Out-String | Write-Host -ForegroundColor Yellow
            }
            else
            {
                Write-Host -ForegroundColor Yellow "No matching modules were found."
            }
        }.GetNewClosure()
    }

    The response from the gallery can be a bit slow, but you can always cancel the lookup with ctrl-C.

  8. SteveL-MSFT commented on May 25, 2018

    @SteveL-MSFT
    Member

    Opened https://github.com/PowerShell/PowerShellGet/issues/287, if they can get the perf down to 100ms or less, I think we can add this capability into PSCore6 (but probably need to have it timeout after 250ms to not impact the interactive experience)

  9. 4 remaining items

  10. Jawz84 commented on Jul 12, 2018

    @Jawz84
    Contributor

    What about changing the CommandNotFoundException to this:

    <data name="CommandNotFoundException" xml:space="preserve">
        <value>The term '{0}' 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.
    You can also try to find a module containing {0} online:
    Find-Module -Command {0}</value>
    

    It's not as fancy as looking up an actual module automatically, but it does point users in the right direction, and it's super easy to implement.

    Maybe the extra line I propose here should only be displayed when {0} -match "^\w+-\w+$" (this probably is not 100% correct regex to capture CmdLet verb-noun, but you get the idea).

  11. iricigor commented on Nov 1, 2018

    @iricigor

    I believe this functionality belongs to package management, but of course, it can/should be shipped together with PowerShell. I did proof-of-concept module which finds required information in about 20ms, so adding suggestions is not a problem. It is inspired with apt-get update from Linux, and it follows what Ilya described above. More information is in the issue Steve opened.

  12. iSazonov commented on Nov 2, 2018

    @iSazonov
    Collaborator

    It will nice to have a public API (in PowerShellGet?) for fast search from the local cache - we could use the API in PowerShell engine for Help and IntelliSense subsystems. Perhaps PowerShell engine need generic API to search from some sources (preinstalled modules, custom modules, native commands and so on).

  13. removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Jun 10, 2020
  14. SteveL-MSFT commented on Jun 10, 2020

    @SteveL-MSFT
    Member

    Can also integrate with winget

  15. ThomasNieto commented on Aug 18, 2023

    @ThomasNieto
    Contributor

    Steven Bucher (@StevenBucher98) can this issue be closed since its covered by feedback providers?

  16. StevenBucher98 commented on Aug 18, 2023

    @StevenBucher98
    Collaborator

    Fixed with Feedback Provider experimental feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugUsability(LEGACY) Helps filter issues that might be higher priority because they directly affect usabilityWG-Interactive-Consolethe console experience

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions