Skip to content

ZSH chpwd hook - #1583

Open
crpb wants to merge 1 commit into
direnv:masterfrom
crpb:zsh/hook
Open

ZSH chpwd hook#1583
crpb wants to merge 1 commit into
direnv:masterfrom
crpb:zsh/hook

Conversation

@crpb

@crpb crpb commented May 2, 2026

Copy link
Copy Markdown

Prevent execution of hook when triggered by any subprocesses like completion calls which can trigger a chpwd which will not change our cwd.


I ran into this problem when using ssh which evidently ran a cd for completing hostnames.

The first call was with a hostname which can't be resolved, the second with a valid hostname.

% ssh moodirenv: unloading                                                                               :(
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
                                          ssh moo                                                                                                :(
% ssh knedirenv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
direnv: unloading
ipix.de

The chpwd was triggered by the following line:
https://github.com/zsh-users/zsh/blob/40625a597581e8d3fd3dc8dc71a97d6e15c57242/Completion/Unix/Type/_ssh_hosts#L39

Changing the cd $HOME to cd -q $HOME already resolved the issue but i was told from the zsh-developers that the hook shouldn't be triggered by the shown calls and pointed me to https://github.com/zsh-users/zsh/blob/40625a597581e8d3fd3dc8dc71a97d6e15c57242/Functions/Chpwd/chpwd_recent_dirs#L32-L36 where i stole this test.

Prevent execution of hook when triggered by any subprocesses like
completion calls which can trigger a chpwd which will not change our
cwd.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant