mattmc3 / antidote

https://getantidote.github.io - the cure to slow zsh plugin management
MIT License
855 stars 21 forks source link

compdef: unknown command or service: rails #144

Closed AlecRust closed 10 months ago

AlecRust commented 1 year ago

I'm trying to switch from Oh My Zsh to Antidote. Here's my setup:

.zshrc

# Source Antidote
source $HOMEBREW_PREFIX/opt/antidote/share/antidote/antidote.zsh

# Set variables required by Oh My Zsh plugins
ZSH=$(antidote path ohmyzsh/ohmyzsh)
ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
[[ -d $ZSH_CACHE_DIR ]] || mkdir -p $ZSH_CACHE_DIR

# Load Antidote
antidote load

.zsh_plugins.txt

# .zsh_plugins.txt

# Initialize Zsh completions (must be first)
# NOTE: Required to fix "command not found: compdef" errors
zshzoo/completion

# Standard plugins
djui/alias-tips

# Oh My Zsh plugins (lib required first)
ohmyzsh/ohmyzsh path:lib
ohmyzsh/ohmyzsh path:plugins/git
ohmyzsh/ohmyzsh path:plugins/npm
ohmyzsh/ohmyzsh path:plugins/yarn
ohmyzsh/ohmyzsh path:plugins/brew
ohmyzsh/ohmyzsh path:plugins/ruby
ohmyzsh/ohmyzsh path:plugins/rails
ohmyzsh/ohmyzsh path:plugins/macos
ohmyzsh/ohmyzsh path:plugins/zoxide
ohmyzsh/ohmyzsh path:plugins/thefuck
ohmyzsh/ohmyzsh path:plugins/bundler
ohmyzsh/ohmyzsh path:plugins/composer
ohmyzsh/ohmyzsh path:plugins/1password

After digging to discover I need special OMZ vars defined in my .zshrc file and a special completion plugin to fix errors, I'm down to just one error when my terminal loads:

compdef: unknown command or service: rails

Any idea how I can fix this? Ruby is installed via rtx and rails is installed also as it's one of my default gems.

mattmc3 commented 10 months ago

OMZ completions get tricky because there's a chicken-and-egg issue where your fpath needs to be populated before running compinit so usually you want to run that late in your plugin list, but some OMZ plugins assume compinit was already run and call functions that don't exist yet like compdef. antidote, like antibody before it, doesn't do any special handling for OMZ. You can still use OMZ completion plugins, just use the kind:fpath notation to be sure they are in your fpath prior to running compinit.

See discussion here: #163

TLDR:

# .zsh_plugins.txt

# Standard plugins
djui/alias-tips

# Oh My Zsh plugins (lib required first)
ohmyzsh/ohmyzsh path:lib
ohmyzsh/ohmyzsh path:plugins/git
ohmyzsh/ohmyzsh path:plugins/npm
ohmyzsh/ohmyzsh path:plugins/yarn
ohmyzsh/ohmyzsh path:plugins/brew
ohmyzsh/ohmyzsh path:plugins/ruby
ohmyzsh/ohmyzsh path:plugins/rails kind:fpath  # NOTE: Required to fix "command not found: compdef" errors
ohmyzsh/ohmyzsh path:plugins/macos
ohmyzsh/ohmyzsh path:plugins/zoxide
ohmyzsh/ohmyzsh path:plugins/thefuck
ohmyzsh/ohmyzsh path:plugins/bundler
ohmyzsh/ohmyzsh path:plugins/composer
ohmyzsh/ohmyzsh path:plugins/1password

# Initialize Zsh completions (must be LAST because fpath needed populated first)
zshzoo/completion

# now load plugins that expected compinit
ohmyzsh/ohmyzsh path:plugins/rails
AlecRust commented 10 months ago

Thanks for the reply, but with:

# Standard plugins
djui/alias-tips

# Oh My Zsh lib (required first)
ohmyzsh/ohmyzsh path:lib

# Oh My Zsh plugins
ohmyzsh/ohmyzsh path:plugins/rtx
ohmyzsh/ohmyzsh path:plugins/git
ohmyzsh/ohmyzsh path:plugins/bun
ohmyzsh/ohmyzsh path:plugins/npm
ohmyzsh/ohmyzsh path:plugins/yarn
ohmyzsh/ohmyzsh path:plugins/brew
ohmyzsh/ohmyzsh path:plugins/ruby
ohmyzsh/ohmyzsh path:plugins/rails kind:fpath  # NOTE: Required to fix "command not found: compdef" errors
ohmyzsh/ohmyzsh path:plugins/macos
ohmyzsh/ohmyzsh path:plugins/zoxide
ohmyzsh/ohmyzsh path:plugins/gcloud
ohmyzsh/ohmyzsh path:plugins/thefuck
ohmyzsh/ohmyzsh path:plugins/bundler
ohmyzsh/ohmyzsh path:plugins/composer
ohmyzsh/ohmyzsh path:plugins/starship
ohmyzsh/ohmyzsh path:plugins/multipass
ohmyzsh/ohmyzsh path:plugins/1password

# Initialize Zsh completions (must be LAST because fpath needed populated first)
zshzoo/completion

# now load plugins that expected compinit
ohmyzsh/ohmyzsh path:plugins/rails

I get:

image

mattmc3 commented 10 months ago

Good catch Alec. I added a commit with a working example for you here: https://github.com/getantidote/zdotdir/tree/demo-antidote-issue-144

AlecRust commented 10 months ago

Thanks, but unless I'm missing something that still doesn't work.

.zsh_plugins.txt

# Standard plugins
djui/alias-tips

# Be sure completion plugins are added to fpath, but don't load them
ohmyzsh/ohmyzsh path:plugins/rails kind:fpath

# Initialize Zsh completions (must be called after fpath populated with all desired completions)
zshzoo/completion

# Oh My Zsh lib (required before OMZ plugins)
ohmyzsh/ohmyzsh path:lib

# Oh My Zsh plugins
ohmyzsh/ohmyzsh path:plugins/rtx
ohmyzsh/ohmyzsh path:plugins/git
ohmyzsh/ohmyzsh path:plugins/bun
ohmyzsh/ohmyzsh path:plugins/npm
ohmyzsh/ohmyzsh path:plugins/yarn
ohmyzsh/ohmyzsh path:plugins/brew
ohmyzsh/ohmyzsh path:plugins/ruby
ohmyzsh/ohmyzsh path:plugins/macos
ohmyzsh/ohmyzsh path:plugins/zoxide
ohmyzsh/ohmyzsh path:plugins/gcloud
ohmyzsh/ohmyzsh path:plugins/thefuck
ohmyzsh/ohmyzsh path:plugins/bundler
ohmyzsh/ohmyzsh path:plugins/composer
ohmyzsh/ohmyzsh path:plugins/starship
ohmyzsh/ohmyzsh path:plugins/multipass
ohmyzsh/ohmyzsh path:plugins/1password

# Plugins that expect compinit
ohmyzsh/ohmyzsh path:plugins/rails

When I launch a terminal I get:

compdef: unknown command or service: rails
mattmc3 commented 10 months ago

compdef: unknown command or service: rails - That's not a completion problem. The rails command isn't found in your path. The nice part about that repo is that I was able to replicate your issue and verify that the code I committed there worked. I would recommend reviewing your .zshenv or .zshrc and making sure you properly built your path variable.

AlecRust commented 10 months ago

The rails command isn't found in your path

Seems to be:

Screenshot 2023-11-18 at 11 04 12

I only add a couple things to my PATH. Even with this commented out I have the same problem.

In fact if I comment out everything from my .zshrc except for loading Antidote, same issue. I don't have a .zshenv.

# Source Antidote
source "$HOMEBREW_PREFIX/opt/antidote/share/antidote/antidote.zsh"

# Set variables required by Oh My Zsh plugins
ZSH=$(antidote path ohmyzsh/ohmyzsh)
export ZSH
export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
[[ -d $ZSH_CACHE_DIR ]] || mkdir -p "$ZSH_CACHE_DIR"

# Load Zsh plugins with Antidote
antidote load

I would put this down to some quirk of the environment on this machine. But I recently installed my dotfiles from scratch on a new work machine, same issue.

I found ohmyzsh/ohmyzsh/issues/2337 but solutions in there didn't work for me.

mattmc3 commented 9 months ago

Somewhere you need to initialize homebrew so its binaries are added to your path. Whether that's in a plugin, or manually, you need to run eval $(/opt/homebrew/bin/brew shellenv).

# Setup homebrew and your $PATH
eval $(/opt/homebrew/bin/brew shellenv)

# Source Antidote
source "$HOMEBREW_PREFIX/opt/antidote/share/antidote/antidote.zsh"

# Set variables required by Oh My Zsh plugins
ZSH=$(antidote path ohmyzsh/ohmyzsh)
export ZSH
export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
[[ -d $ZSH_CACHE_DIR ]] || mkdir -p "$ZSH_CACHE_DIR"

# Load Zsh plugins with Antidote
antidote load
AlecRust commented 9 months ago

Sorry I should have said, I do have the Homebrew path line in ~/.zprofile. Without it rails, Starship etc. wouldn't work.

Thinking that maybe it's due to the load order of .zprofile, I removed it and used your example as above. Same issue 😄

Thankfully I'm not working with Rails right now so I don't mind having this plugin excluded. However it does seem this plugin works fine when only using OMZ, but not when loading it with Antidote.

Regarding your reproduction repo there seems to be files like .zshenv which I don't have, and a number of things in .zprofile (mine just has the Homebrew line). There's very little to my entire setup, I basically just load the Zsh plugins above via Antidote and set some aliases.

Very odd this is happening on fresh machine too, I wonder how many other Antidote users are successfully using the rails OMZ plugin, or more importantly why it works fine for you 🤔

mattmc3 commented 2 months ago

As of June 2024, the recommended way of handling OMZ is now to include the new getantidote/use-omz plugin at the TOP of your .zsh_plugins.txt file. It handles all OMZ dependency resolution, setting OMZ variables, and deferred completion initialization for OMZ users. No more compdef errors!