Does ohmyzsh automatically invalidate its completion cache when a new executable is added to $PATH?
0 reputation · 02 Jun 2021, 05:19 UTC
0 reputation · 02 Jun 2021, 05:19 UTC
Ohmyzsh stores generated completions in a cache directory to speed up shell startup. The cache is considered valid based on the modification times of the source completion scripts, but it does not automatically track newly added executables in the user's $PATH.
When a new command‑line tool that ships its own completion script is installed, users often observe that tab completion does not appear until the cache is manually cleared. This raises the question of whether ohmyzsh should provide an automatic invalidation hook for PATH changes, and what the current behavior actually is.
Does ohmyzsh automatically invalidate its completion cache when a new executable is added to $PATH? What mechanism, if any, does ohmyzsh currently use to detect changes in $PATH for cache invalidation?
34775 reputation · 02 Jun 2021, 13:10 UTC
No. Oh‑My‑Zsh does not automatically drop or rebuild its completion cache when a new executable appears in $PATH. The cache is managed by Zsh’s compinit function, which only watches the modification times of completion scripts themselves, not the presence of binaries.
When compinit -C (or the default compinit if caching is enabled) runs, it builds a binary dump of all completion definitions found in $fpath and stores it in $ZSH/cache/compcache. Subsequent shell starts or compinit invocations will reuse this dump unless:
$fpath.-u flag is supplied, forcing a rebuild.compinit run.Adding a new binary to $PATH does not touch any source completion files, so the cache remains unchanged until one of the above actions occurs.
Verify that caching is enabled by checking for compinit -C (or compinit with ZSH_CACHE_COMPDUMP set) in ~/.zshrc:
grep -E '^compinit' ~/.zshrc
After installing a new tool, force a rebuild with:
autoload -Uz compinit
compinit -u
Test completion:
mytool
Optionally, restart the terminal to confirm the change persists.
$PATH for the current shell?$fpath?Addressing any of the above will resolve most missing‑completion scenarios.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 02 Jun 2021, 16:38 UTC
One nuance worth adding: there are two different things people mean by "completion cache" here, and they behave differently.
The compinit dump (~/.zcompdump or $ZSH_CACHE_DIR/zcompdump) caches which _command completion functions exist. It is invalidated by mtime changes in $fpath, so if a newly installed tool ships its own completion script into a directory on $fpath, the dump is rebuilt on the next shell start — which can look like automatic PATH-based invalidation when it isn't.
Separately, plain command-name completion (no dedicated function) scans $PATH live at completion time, so the binary may be completable even with a stale dump — though rehash may be needed for zsh's command hash table.
To verify: check the dump timestamp before and after adding a PATH-only binary and starting a new session — it should be unchanged.