Can Oh My Zsh plugin loader guarantee `compdef` availability before completion initialization during startup?
0 reputation · 24 Jul 2020, 08:29 UTC
0 reputation · 24 Jul 2020, 08:29 UTC
Goal: Determine whether Oh My Zsh plugin loader can ensure that a plugin's `compdef` call is valid before Zsh's completion system finishes initialization, given that plugin directories may be added to `fpath` and `_command` files may be sourced at different times.
Constraints: Verify that `compinit` runs after plugin directories are added to `fpath` but before plugins source their definitions, and confirm if the ordering is stable across Oh My Zsh releases and Zsh versions, as version changes may affect when completion directories are processed.
Questions: Does OMZ load `compinit` before sourcing all `.plugin.zsh` files, and can a plugin safely call `compdef` during its own source? Is the ordering of `compinit` relative to plugin directory addition stable across Oh My Zsh releases?
35275 reputation · 24 Jul 2020, 16:03 UTC
In the default Oh My Zsh (OMZ) distribution, compinit is executed before any .plugin.zsh files are sourced. This guarantees that a plugin can safely call compdef during its own source block, because the completion system has already been initialized.
~/.zshrc.source $ZSH/oh-my-zsh.sh call, which in turn
fpath with the default plugin directories.source $ZSH/lib/compinit.zsh (i.e., runs compinit).plugins and sources each *.plugin.zsh file.compdef during its sourcing registers completions directly with the already‑initialized system.To confirm that this ordering holds in your environment, run:
zsh -x -i -c 'echo "START"; source $HOME/.oh-my-zsh/oh-my-zsh.sh; echo "END"'
The trace will show source $ZSH/lib/compinit.zsh before any source .../*.plugin.zsh lines. If you see compdef calls before compinit, your .zshrc or a custom script has reordered the sequence.
.zshrc changes: If you call compinit after adding fpath entries or source plugins yourself, the guarantee is lost.fpath before compinit: Such plugins must manually re‑run compinit or use compdef -f to register completions.compdef parses completion files. The calling sequence still works, but behaviour may differ if you rely on edge cases.1. Check your OMZ version: omz --version (or look at ~/.oh-my-zsh/CHANGELOG.md).
2. Verify your Zsh version: zsh --version.
3. If the ordering is wrong, move the source $ZSH/lib/compinit.zsh line to the top of ~/.zshrc or remove any early compinit calls that you added.
Under the stock OMZ configuration, compinit runs before plugin loading, so compdef calls inside plugins are safe. Keep your .zshrc clean and avoid re‑ordering the sequence, and the guarantee remains stable across OMZ releases.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.