Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lzybkr opened this issue Aug 20, 2016 · 13 comments
Closed

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

lzybkr opened this issue Aug 20, 2016 · 13 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Usability (LEGACY) Helps filter issues that might be higher priority because they directly affect usability WG-Interactive-Console the console experience
Milestone

Comments

@lzybkr
Copy link
Member

lzybkr commented Aug 20, 2016

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
@lzybkr lzybkr added OS-Linux Usability (LEGACY) Helps filter issues that might be higher priority because they directly affect usability WG-Interactive-Console the console experience labels Aug 20, 2016
@rkeithhill
Copy link
Collaborator

rkeithhill commented Aug 20, 2016

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)?

@nerdshark
Copy link

nerdshark commented Aug 21, 2016

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/*).

@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.

@SteveL-MSFT SteveL-MSFT added the Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors label Sep 29, 2016
@SteveL-MSFT SteveL-MSFT added this to the 6.1.0 milestone Mar 13, 2017
@SteveL-MSFT SteveL-MSFT added Issue-Enhancement the issue is more of a feature request than a bug and removed OS-Linux labels Jun 12, 2017
@SteveL-MSFT
Copy link
Member

Need some api in PSGallery to enable this

@BrucePay
Copy link
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()
    }
}

@BrucePay
Copy link
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.

@SteveL-MSFT
Copy link
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)

@iSazonov
Copy link
Collaborator

Web response time is not predictable. Can we use local cache like for help files? We could download a catalog and only check the catalog version and suggest users to update it.

@SteveL-MSFT SteveL-MSFT modified the milestones: 6.2.0-Consider, Future Jun 21, 2018
@Jawz84
Copy link
Contributor

Jawz84 commented Jul 12, 2018

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).

@iricigor
Copy link

iricigor commented Nov 1, 2018

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.

@iSazonov
Copy link
Collaborator

iSazonov commented Nov 2, 2018

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).

@SteveL-MSFT SteveL-MSFT removed the Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors label Jun 10, 2020
@SteveL-MSFT SteveL-MSFT removed this from the Future milestone Jun 10, 2020
@SteveL-MSFT SteveL-MSFT added this to the 7.1-Consider milestone Jun 10, 2020
@SteveL-MSFT
Copy link
Member

Can also integrate with winget

@ThomasNieto
Copy link
Contributor

@StevenBucher98 can this issue be closed since its covered by feedback providers?

@StevenBucher98
Copy link
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
Labels
Issue-Enhancement the issue is more of a feature request than a bug Usability (LEGACY) Helps filter issues that might be higher priority because they directly affect usability WG-Interactive-Console the console experience
Projects
None yet
Development

No branches or pull requests