199

I am using apple M1 MacBook pro.

When I installed oh my zsh. When I addedexport PATH="/opt/homebrew/bin:$PATH" to my ~/.zshrc file. This error was shown in my terminal:

joe :: share/zsh/site-functions » source ~/.zshrc
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask

However, I checked and found that these two files do exsist. Can someone tell me that the problem is?

This is my ~/.zshrc file:

Last login: Sat Jan 16 14:53:34 on console
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
[oh-my-zsh] Random theme 'jnrowe' loaded
Ξ ~ → cd ~
Ξ ~ → source .zshrc

compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
# export MANPATH="/usr/local/man:$MANPATH"
[oh-my-zsh] Random theme 'cypher' loaded
joe :: ~ » chmod 755 /usr/local/share/zsh
chmod 755 /usr/local/share/zsh/site-functions

joe :: ~ » sudo chmod 755 /usr/local/share/zsh
Password:
joe :: ~ » sudo chmod 755 /usr/local/share/zsh/site-functions
joe :: ~ » ls
#ZSH_DISABLE_COMPFIX=true

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

#Homebrew
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/sbin:$PATH"
#Homebrew END

#Wget
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
#Wget END

 #Path to your oh-my-zsh installation.
export ZSH="/Users/caizhuoyue/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="random"

"~/.zshrc" 114L, 3999C




Ray Baxter
  • 3,181
  • 23
  • 27
Rachel_Miller
  • 2,093
  • 2
  • 6
  • 9

14 Answers14

483

I had a similar issue. I ran brew cleanup which fixed the symlinks.

Monica Granbois
  • 6,632
  • 1
  • 17
  • 17
58

I had the same this issue which I noticed when updating my dot files.

On the M1 I went from Intel brew to Intel and ARM brew then to just the ARM version. The problem for me was caused by two symbolic links pointing to the Intel version, which no longer existed, and not the ARM version.

I repaired it by changing the symbolic links to point to the right locations for the ARM version.

ln -fsv /opt/homebrew/completions/zsh/_brew /usr/local/share/zsh/site-functions/_brew

ln -fsv /opt/homebrew/completions/zsh/_brew /usr/local/share/zsh/site-functions/_brew_cask

thus

lrwxr-xr-x    35 xxxx  2 Jun 16:02  _brew -> /opt/homebrew/completions/zsh/_brew
lrwxr-xr-x    35 xxxx  2 Jun 16:01  _brew_cask -> /opt/homebrew/completions/zsh/_brew

I think _brew_cask pointing to the same _brew is okay since casks have been merged.

Danilo Cabello
  • 2,814
  • 1
  • 23
  • 26
AndrewC
  • 681
  • 4
  • 6
  • missing an `-` for `site-functions` in the first line – Sin Jul 11 '21 at 14:24
  • Worked for me. (and yes `-` is missing) – Betabong Jul 12 '21 at 08:55
  • I fixed the typo, should be good to go. Thank you for the answer. – Danilo Cabello Jul 14 '21 at 11:34
  • Thanks for correcting. Glad it’s helped some people. Yes some times on Mac a double hyphen gets turned into a double width hyphen I haven’t found out the cause yet probably a third-party tool . I will have a look this afternoon. – AndrewC Jul 14 '21 at 11:46
  • 1
    Heck yeah, nice find! Fixed it for me on my M1. – jcreamer898 Jul 20 '21 at 15:53
  • This worked for me. I still wonder why `compinit` would look for contents in `/usr/local/share/`. I checked `echo $FPATH` and there is `/usr/local/share/zsh/site-functions` after the ARM brew's `/opt/homebrew/share/zsh-completions`. I have no idea when and where was FPATH set to include the old paths. – edwardtoday Dec 17 '21 at 01:58
  • @edwardtoday not sure. However, brew has had many releases since. These links were set by brews installation IIRC the issue was caused by moving from i386 to ARM64 architecture version of brew or trying to use both you can only have one set of symbolic links at a time. I think zsh sets some fpaths. – AndrewC Dec 17 '21 at 15:22
  • Homebrew zsh is compiled using homebrew directories ( https://github.com/Homebrew/homebrew-core/blob/c91ac56da0745ebf129d7355887e4978ccec4bfe/Formula/zsh.rb#L45 ). You have directories for zsh completions and functions for the system, homebrew, zsh builtin, zsh third party (site-functions), user local under .zsh. – AndrewC Dec 17 '21 at 15:23
  • Functions are also used with completions. I think they could probably use fpaths on their own these days? but perhaps the symbolic links are for backward compatibility. – AndrewC Dec 17 '21 at 15:24
  • also brew cleanup didn’t solve the issue at the time of the post – AndrewC Dec 17 '21 at 16:00
  • This should be the accepted answer for M1. `brew cleanup` didn't work, at least for me. – vizFlux Feb 24 '22 at 02:39
  • 1
    Fixed on M2, though _brew_cask isn't present on my machine, I see _brew_services - I symlinked both as indicated, and the error message is gone now. – tgmerritt Jun 01 '23 at 15:42
45

An approach a little bit more detailed would be:

brew doctor
brew cleanup
source ~/.zshrc

Or one line:

brew doctor && brew cleanup && source ~/.zshrc

After this, you can see if you get any errors after using source.

Philipos D.
  • 2,036
  • 1
  • 26
  • 33
17

@sinestandly's answer above worked for me after the other methods failed. I ran brew install zsh-completions and then brew cleanup. The cleanup stopped throwing errors and I no longer get the error message compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask.

Thank you, @sinestandly!

Andrew Kawel
  • 191
  • 1
  • 3
  • I was migrated from Intel to Apple Silicon (M1) and these solved the issue. Thanks! `brew install zsh-completions` `brew cleanup` – bshears Nov 27 '21 at 10:06
  • worked because zsh-completions actually recreates symbolic links for _brew which is the same given in my answer replacing missing or incorrect links. However, since _brew symbolic links have been removed from zsh-completions it just uses fpaths. It has also removed some other functionality because it was just duplicating what brew did. Just in case anyone found this not to work as expected. – AndrewC Dec 17 '21 at 15:45
12

According to https://github.com/Homebrew/homebrew-core/issues/45009

try

sudo chown -R $(whoami):admin /usr/local/* \ && sudo chmod -R g+rwx /usr/local/*

then

brew cleanup

Jerry
  • 329
  • 1
  • 4
  • 13
  • thanks. This worked for me. I was getting errors otherwise. – Jasjeet Kaur Sep 15 '21 at 08:29
  • Not quite the same issue. You should be getting warnings from compinit regarding security permissions for this one. The above fixes this. However, should still work. – AndrewC Dec 17 '21 at 15:27
12

I got this issue after uninstalling brew. Just remove it if you've done the same:

rm -rf /usr/local/share/zsh/site-functions/_brew
Micael Mota
  • 433
  • 3
  • 7
  • I had a similar error not brew related but fix worked with `sudo rm /usr/share/vendor-completions/_docker` which was the line throwing errors. Basically a dead symlink pointer needing to be removed. – lacostenycoder Mar 20 '23 at 16:31
8

True, the easiest way to fix this problem is run: brew cleanup

Just, don't forget that run this command with the x86 brew version if u kept both of arm and x86 version.

This was a very low-level mistake of mine, I tried numerous times to fail with the default brew command (I installed it by the script and it already linked to the new arm version) before I finally realised I needed to use x86 brew to execute the cleanup command.

xsnaruto
  • 81
  • 1
  • if you're running both you should use the arch command to detect architecture then use if-else statements to load the paths, fpaths, symbolic links etc to match the version used and place all this into your .zshrc then either open your terminal for amd64 or use rosetta to load intel. You can actually duplicate your terminal app and have one set to each. just be wary of any background services. – AndrewC Dec 17 '21 at 15:52
4

Turns out these files are aliases of other two files that did not exist.

This is because the Homebrew of M1 macbook is under/opt/homebrew/ but the zsh assumed it is still under /usr/local.

So I deleted the two aliases and made new ones pointing to where the files actually are:/opt/homebrew/completions/zsh/_brewand/opt/homebrew/completions/zsh/_brew_cask.

Then I usedsource ~/.zshrc. No error messages. Problem solved!

Rachel_Miller
  • 2,093
  • 2
  • 6
  • 9
  • I guess you mean _symlink_, when you say 'aliases_? Files don't have aliases.... – user1934428 Jan 18 '21 at 15:41
  • 6
    Also, as it's been noted in https://stackoverflow.com/a/65924342/4764498, instead of removing the symlinks manually, it's more convenient/robust to run `brew cleanup` (which will remove any broken symlinks, including these ones). So I believe that should be the accepted answer. – Alexey Orlenko Feb 01 '21 at 12:36
  • I deleted the symlink `_brew_cask`, ran `brew doctor` and then `brew cleanup`. Nothing else worked for me – Eric Feb 10 '21 at 23:42
  • @user1934428 On Mac OS files have aliases. https://support.apple.com/guide/mac-help/create-and-remove-aliases-on-mac-mchlp1046/mac – Ray Baxter Oct 27 '21 at 15:25
  • @RayBaxter: Interesting. What is the difference/advantage of a file alias over a symbolic link? For achieving this purpose, I would simply have created a symlink. – user1934428 Oct 28 '21 at 06:52
  • @user1934428 I would have used a symlink as well. You can create an alias via the UI. – Ray Baxter Oct 28 '21 at 16:30
4
brew install zsh-completions

Fixed it.

sinestandly
  • 131
  • 2
  • 8
3

All Homebrew completions were broken in my case, running on Apple Silicon. The move from /usr/local to /opt/homebrew in Homebrew 3.0.0 seems to be the issue.

I appended the new directory to FPATH in ~/.zshrc like so:

HOMEBREW_PREFIX=$(brew --prefix)
export FPATH="${HOMEBREW_PREFIX}/share/zsh/site-functions:${FPATH}"

If running Oh-My-Zsh, the lines need to go above the line that sources OMZ, since it does some completion magic of its own. Also remember to clean out your .zcomdump -files, which will be re-created.

lapponiandevil
  • 413
  • 3
  • 7
  • Thank you! This works perfectly. One addition is that you need to add the eval that loads brew(`eval "$(/opt/homebrew/bin/brew shellenv)"`) before the two lines listed in the answer. – Eli Smith May 18 '22 at 18:19
  • Hi, somehow despite on a Apple Sillicon chips the brew --prefix still return me with /usr/local... is there a way to fix the problem? – JoZ Oct 13 '22 at 15:42
  • This fixed my errors. Thanks! Now GIT autocomplete works! – Jake Nov 24 '22 at 03:35
1

Got similar issue after I upgraded to macOS Bigsur. Got it fixed after doing brew update

agent_daemon
  • 112
  • 4
0

Got the similar issue with another path, even doesn't mentioned in my .zshrc

Running brew update && brew upgrade solved this.

0

I had issue with /usr/local/share/zsh/site-functions/_mdatp and I deleted this link file and it worked perfectly

a13e
  • 838
  • 2
  • 11
  • 27
0

I was able to solve the issue by deleting the file that it was looking for. I just ran:

rm /usr/local/share/zsh/site-functions/_brew

I then confirmed that it worked by running source ~/zshrc

The reason was because the environment variable $fpath was looking in the directory /usr/local/share/zsh/site-functions/, and _brew was an empty link. Which is why I was getting the error:

compinit:527: no such file or directory: /usr/local/share/zsh/site-functions/_brew