Git Product home page Git Product logo

homebrew-command-not-found's Introduction

Homebrew Command Not Found

This project reproduces Ubuntu’s command-not-found for Homebrew users on macOS.

GitHub Actions

On Ubuntu, when you try to use a command that doesn’t exist locally but is available through a package, Bash will suggest you a command to install it. Using this script, you can replicate this feature on macOS:

# on Ubuntu
$ when
The program 'when' is currently not installed.  You can install it by typing:
sudo apt-get install when

# on macOS with Homebrew
$ when
The program 'when' is currently not installed. You can install it by typing:
  brew install when

Over 5000 formulae are supported, representing more than 17000 different commands (100% of the main Homebrew repo).

Install

First, tap this repository:

brew tap homebrew/command-not-found
  • Bash and Zsh: Add the following line to your ~/.bash_profile (bash) or ~/.zshrc (zsh):

    HB_CNF_HANDLER="$(brew --repository)/Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
    if [ -f "$HB_CNF_HANDLER" ]; then
    source "$HB_CNF_HANDLER";
    fi
  • Fish: Add the following line to your ~/.config/fish/config.fish:

    set HB_CNF_HANDLER (brew --repository)"/Library/Taps/homebrew/homebrew-command-not-found/handler.fish"
    if test -f $HB_CNF_HANDLER
       source $HB_CNF_HANDLER
    end

Requirements

This tool requires one of the following:

  • Zsh (the default on macOS Catalina and above)
  • Bash (version 4 and higher)
  • Fish

How it works

When you tap the repo you’ll get two more brew commands: brew which-formula and brew which-update. The first one uses a database file which gives you the formula you have to install in order to get a specific command. The file is generated by the second command by crawling all installed formulae and collecting their binaries. Having this as a tap means you get an up-to-date binaries database each time you run brew update.

The handler.sh script defines a command_not_found_handle function which is used by Bash when you try a command that doesn’t exist. The function calls brew which-formula on your command, and if it finds a match it’ll print it to you. If not, you’ll get an error as expected.

homebrew-command-not-found's People

Contributors

alyssais avatar azure-pipelines[bot] avatar bfontaine avatar bo98 avatar branchvincent avatar brewtestbot avatar commitay avatar davelab6 avatar ericfromcanada avatar filosottile avatar ilovezfs avatar issyl0 avatar jeffwidman avatar kidonng avatar leonklingele avatar mcmahoniel avatar mikemcquaid avatar muhammedzakir avatar query avatar reitermarkus avatar revolter avatar rylan12 avatar woodruffw avatar xu-cheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

homebrew-command-not-found's Issues

Don’t suggest non-installable formulae

The current suggestion feature might show formulae that don’t work on the current OS version / architecture, e.g. on 10.10:

$ valgrind
The program 'valgrind' is currently not installed. You can install it by typing:
  brew install valgrind
$ brew install valgrind
valgrind: OS X Mavericks or older is required.
Error: An unsatisfied requirement failed this build.

Path given in README does not work for `/opt/homebrew` installations on M1 Macs

For example, the instructions for fish have:

set HB_CNF_HANDLER (brew --prefix)"/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.fish"

But /opt/homebrew/Homebrew does not exist in a standard installation. The Library has moved to /opt/homebrew/Library which means that the /Homebrew portion of the path should be removed.

`brew which-formula` doesn't work

For some reason brew which-formula doesn't work for me.

brew which-formula when returns:

Error: private method `flag?' called for ["when"]:Array
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-formula.rb:74:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/local/Homebrew/Library/Homebrew/utils.rb:82:in `require?'
/usr/local/Homebrew/Library/Homebrew/brew.rb:108:in `<main>'

I'm using:

macOS Catalina 10.15.2 19C57
Homebrew 2.2.3-11-gdfe028a

Command brew which-update fails

I'm just added command-not-found tap and cannot run which-update:

% brew which-update
==> Using executables list from '/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/executables.txt'
Error: missing keywords: flags, ignore_errors
Warning: Removed Sorbet lines from backtrace!
/opt/homebrew/Library/Homebrew/formulary.rb:132:in `load_formula_from_path'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:64:in `block (2 levels) in update!'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:63:in `each'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:63:in `block in update!'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:62:in `each'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:62:in `update!'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/which_update.rb:51:in `update_and_save!'
/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/which-update.rb:41:in `which_update'
/opt/homebrew/Library/Homebrew/brew.rb:86:in `<main>'

Homebrew version:

% brew --version
Homebrew 4.1.17-33-g13ca5c3
Homebrew/homebrew-core (git revision ca924ee3875; last commit 2023-10-28)
Homebrew/homebrew-cask (git revision c9f9182d01; last commit 2023-10-27)

Command which-formula is working:

% brew which-formula identify
imagemagick
imagemagick@6

(eval):1: no matches found: “#

I am having some issues after installing.

I am running zsh on OS X Yosemite 10.10.5. I tapped the repository and added the line if brew command command-not-found-init > /dev/null; then eval "$(brew command-not-found-init)"; fi to the .zshrc.

Now when I start a shell, I get:

Last login: Thu Aug 20 07:34:21 on ttys001
(eval):1: no matches found: “#
➜ ~

If I type when, I get:

➜ ~ when
zsh: command not found: when

I see that the new brew commands have been installed, so I am not sure what might be causing the issue. I wanted to check with you before posting an issue to ensure I am not making a foolish mistake.

I don't think there are issues with my .zshrc; when I enter zsh -n ~/.zshrc to the command prompt, no errors appear.

not working and I've installed and edited my bash_profile

still just shows up

$ when
-bash: when: command not found

$ brew tap homebrew/command-not-found
Warning: Tap homebrew/command-not-found already tapped.

.bash_profile

brew command not found

if brew command command-not-found-init > /dev/null; then eval "$(brew command-not-found-init)"; fi

Error with brew which-update: undefined method `split' for nil:NilClass

When I run brew which-update, I now get the following error:

==> Using executables list from '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/executables.txt'
Error: undefined method `split' for nil:NilClass
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:34:in `block in initialize'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:32:in `each'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:32:in `initialize'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:198:in `new'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:198:in `<top (required)>'
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/Homebrew/Library/Homebrew/utils.rb:20:in `require?'
/usr/local/Homebrew/Library/Homebrew/brew.rb:106:in `<main>'

The command brew which-update does not work anymore, it just generates that error message. brew command-not-found-init and brew which-formula still work, thankfully. This is a new problem and did not happen before, brew which-update worked until recently. I got this problem after running brew update as usual today. I am on macOS 10.12 Sierra and the rest of homebrew works fine right now, no problems at all with brew doctor.

Error: undefined method `install_tap' for Homebrew:Module

With Linuxbrew:

$ brew which-update --stats executables.txt 
Error: undefined method `install_tap' for Homebrew:Module
/home/baptiste/.linuxbrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:160:in `block in <top (required)>'
/home/baptiste/.linuxbrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:158:in `each'
/home/baptiste/.linuxbrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:158:in `flat_map'
/home/baptiste/.linuxbrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-which-update.rb:158:in `<top (required)>'
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/home/baptiste/.linuxbrew/Library/brew.rb:21:in `require?'
/home/baptiste/.linuxbrew/Library/brew.rb:89:in `<main>'

$ brew doctor
Your system is ready to brew.

$ brew config
HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/linuxbrew
HEAD: 44cbc9f7147442c2aa029795780833fa72ce69c9
Last commit: il y a 2 jours
HOMEBREW_PREFIX: /home/baptiste/.linuxbrew
HOMEBREW_REPOSITORY: /home/baptiste/.linuxbrew
HOMEBREW_CELLAR: /home/baptiste/.linuxbrew/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://linuxbrew.bintray.com
CPU: quad-core 64-bit broadwell
Kernel: Linux 4.2.0-35-generic x86_64 GNU/Linux
OS: Ubuntu 14.04.4 LTS
Codename: trusty
OS glibc: 2.19
OS gcc: 4.8.4
Linuxbrew glibc: N/A
Linuxbrew gcc: N/A
Clang: N/A
X11: 2.7.8 => /usr
System Ruby: 1.9.3-p484 => /usr/bin/ruby1.9.1
Perl: /usr/bin/perl
Python: /usr/bin/python => /usr/bin/python2.7
Ruby: /usr/bin/ruby => /usr/bin/ruby1.9.1
Java: 1.7.0_95

homebrew-command-not-found started displaying error on loading on macOS

Issue

Started up a terminal on macOS this morning and got the following error.

Error

Error:' uninitialized constant Utils::Shell
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-command-not-found-init.rb:4:in 'shell'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-command-not-found-init.rb:45:in 'init'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-command-not-found-init.rb:58:in '<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in 'require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in 'require'
/usr/local/Homebrew/Library/Homebrew/utils.rb:85:in 'require?'
/usr/local/Homebrew/Library/Homebrew/brew.rb:121:in '<main>'

Temp Fix

removing the homebrew-command-not-found tap resolves it

> brew untap homebrew/command-not-found

System Details

  • MacOS: 10.15.6 (19G2021)

  • Xcode : Version 11.6 (11E708)

  • Homebrew

> brew --version
Homebrew 2.4.13-41-g236cbbc
Homebrew/homebrew-core (git revision 90eb3; last commit 2020-08-22)
Homebrew/homebrew-cask (git revision 4602d; last commit 2020-08-22)
  • Defualt System Ruby (with macOS Catalina)
>  ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]
  • Brew Ruby
> pwd
/usr/local/Cellar/ruby/2.7.1_2/bin
> ./ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
  • Other

Possibly caused by Brew update on the 22/08/2020 ?

Homebrew/homebrew-core (git revision 90eb3; last commit 2020-08-22)
Homebrew/homebrew-cask (git revision 4602d; last commit 2020-08-22)

Using brew gives warnings

Warning: Calling Formula#all without --all or HOMEBREW_EVAL_ALL is deprecated! There is no replacement.
Please report this issue to the homebrew/command-not-found tap (not Homebrew/brew or Homebrew/homebrew-core):

/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/lib/executables_db.rb:57

Remove deleted formulae

e.g. shell.fm have been removed from the main repo but the update script didn’t remove it from executables.txt.

Commands starting with '-' can appear to be options and need quoting

$  -h
Usage: brew which-formula [--explain] command [...]

Prints the formula(e) which provides the given command.

      --explain                    Output explanation of how to get 'cmd' by
                                   installing one of the providing formulae.
  -d, --debug                      Display any debugging information.
  -q, --quiet                      Make some output more quiet.
  -v, --verbose                    Make some output more verbose.
  -h, --help                       Show this message.

Clearly we are running "brew which-formula $BADCMD" and -h gets passed to brew.

I tried this test:

$ brew which-formula -- -h

but that command appears to just hang so there are so other changes needed and it fell below the threshold where I have time to try putting together a patch.

But I figured I should at least make a bug report.

Support taps

We need to search in $(brew --prefix)/Library/Taps/*/*/ to find which formulae come from taps and prefix them in the database.

Takes 1-2 seconds before it shows the message

Every time I run an invalid command, it takes about 1-2 seconds before it shows the candidates of packages or shows the "Unknown command" message. The experience is far worse than the counterpart in, say, Ubuntu, where the results show up in like <0.1 seconds. I'm so impatient that I immediately type Ctrl-C once I recognized I typed a wrong command before the results are output. Now I'm going to disable this feature... Is there a room of improvement? If it is easy enough, maybe I can contribute.

Redundancy in installation instructions around `brew command-not-found-init`

The readme says I should add this to my .bashrc:

if brew command command-not-found-init > /dev/null 2>&1; then eval "$(brew command-not-found-init)"; fi

In pseudocode:

  • If command-not-found has been tapped, then read the output of brew command-not-found-init into the current shell.

Well, what is the output of brew command-not-found-init?

$ brew command-not-found-init
# To enable homebrew-command-not-found
# Add the following lines to ~/.bashrc

if brew command command-not-found-init > /dev/null; then
  eval "$(brew command-not-found-init)";
fi

So the test verifying the tap is tapped is getting executed twice. Why not just add eval "$(brew command-not-found-init)" to .bashrc and call it a day?

prompt if not in PATH?

$ brew install binutils
$ gaddr2line 
zsh: command not found: gaddr2line

Would be nice if it said "this should already be installed by package X, perhaps it's not in your PATH"? Just to avoid confusion for those of us using it :)

Remove jargon

We might want to use “package” instead of “formula”.

`brew tap` is unnecessary

brew install will automatically tap the tap when necessary. Also to be neater , brew install homebrew/tap/formula is preferred over brew install homebrew/homebrew-tap/formula.

command-not-found causes VERY long zsh startup times

This is my zshrc

start=`gdate +%s.%N`
export ZSH="$HOME/.oh-my-zsh"
fpath=(/usr/local/share/zsh-completions /usr/local/share/zsh-completions/conda-zsh-completion $fpath)
source $ZSH/oh-my-zsh.sh

end=`gdate +%s.%N`
runtime=$( echo "$end - $start"|bc -l )
echo "$runtime seconds"

when I open zsh the output is always around .118130000 seconds

Now when I try adding command-not found:

start=`gdate +%s.%N`
export ZSH="$HOME/.oh-my-zsh"
fpath=(/usr/local/share/zsh-completions /usr/local/share/zsh-completions/conda-zsh-completion $fpath)
if brew command command-not-found-init > /dev/null; then
  eval "$(brew command-not-found-init)";
fi
source $ZSH/oh-my-zsh.sh

end=`gdate +%s.%N`
runtime=$( echo "$end - $start"|bc -l )
echo "$runtime seconds"

Now when I open zsh, the output is around 2.358168000 seconds!

Clearly, this is unacceptable. What can be done to speed this up?

I followed the installation instructions but it doesn't work

I followed the installation instructions but it doesn't work

$ brew install bash;
$ brew tap homebrew/command-not-found;
$ # edit .bash_profile
$ tail -n3 .bash_profile;
if brew command command-not-found-init > /dev/null; then
  eval "$(brew command-not-found-init)";
fi
$ source .bash_profile;
$ when
-bash: when: command not found
$

FISH support

Is there going to be FISH support? I tried following the instructions but, it doesn't work for FISH.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.