Skip to content
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

compdef: unknown command or service: rails #144

Closed
AlecRust opened this issue Jul 30, 2023 · 9 comments
Closed

compdef: unknown command or service: rails #144

AlecRust opened this issue Jul 30, 2023 · 9 comments

Comments

@AlecRust
Copy link

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
Copy link
Owner

mattmc3 commented Nov 12, 2023

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
Copy link
Author

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 added a commit to getantidote/zdotdir that referenced this issue Nov 14, 2023
@mattmc3
Copy link
Owner

mattmc3 commented Nov 14, 2023

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
Copy link
Author

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
Copy link
Owner

mattmc3 commented Nov 14, 2023

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
Copy link
Author

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
Copy link
Owner

mattmc3 commented Nov 19, 2023

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
Copy link
Author

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
Copy link
Owner

mattmc3 commented Jun 27, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants