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

Direnv: unloading after every command #726

Open
marc-fez opened this issue Nov 29, 2020 · 16 comments
Open

Direnv: unloading after every command #726

marc-fez opened this issue Nov 29, 2020 · 16 comments
Labels

Comments

@marc-fez
Copy link

marc-fez commented Nov 29, 2020

direnv: unloading is displayed after every command I run in my shell. The environment is not unloaded.

  • Initialise pipenv environment with pipenv install --python=3.8

  • create .envrc with echo 'layout pipenv' > .envrc
    direnv: error /home/marc/workspace/direnv_bug/.envrc is blocked. Run direnv allow to approve its content
    direnv: unloading

  • run direnv allow, environment is loaded, then unloading message appears.
    $ direnv allow
    direnv: loading ~/workspace/direnv_bug/.envrc
    direnv: export +PIPENV_ACTIVE +VIRTUAL_ENV ~PATH
    direnv: unloading

  • pipenv environment is still loaded
    $ which python
    /home/marc/.local/share/virtualenvs/direnv_bug-kkqpciCf/bin/python
    direnv: unloading

  • Any command run with the environment loaded shows the same 'direnv: unloading' message.

Expected behavior
Don't expect to see the direnv: unloading message

Environment

  • OS: Arch
  • Shell: zsh
  • Direnv version 2.24.0 (installed from aur)

Additional context
I've tried uninstalling and re-installing direnv, but still seeing the message

@marc-fez marc-fez added the Bug label Nov 29, 2020
@wnkz
Copy link

wnkz commented Feb 11, 2021

I have a very similar problem ; but it happens even without layout pipenv.

Environment

  • OS: macOS 10.15.7
  • Shell: zsh 5.8 ( + oh-my-zsh + p10k )
  • Direnv versions 2.21 - 2.27 (installed with asdf)

Problem still happens with oh-my-zsh and powerlevel10k completely disabled ; however it does not happen with a default bash shell.
I had to rollback to 2.20 for the problem to disappear.

@a4z
Copy link

a4z commented Mar 1, 2021

same problem here, just created #771

@zimbatm
Copy link
Member

zimbatm commented Mar 1, 2021

@a4z are you also on zsh and macOS?

@zimbatm
Copy link
Member

zimbatm commented Mar 1, 2021

Can you give me these outputs:

$ echo "${chpwd_functions[@]}"
$ echo "${precmd_functions[@]}"
$ direnv status

@a4z
Copy link

a4z commented Mar 2, 2021

sure,

[a4z@mach2:~] (09:16:22)
-> cd tmp/denv 
direnv: loading ~/tmp/denv/.envrc
direnv: export +USERENV ~PATH

[a4z@mach2:~/tmp/denv] (09:16:33) (direnv)
-> echo "${chpwd_functions[@]}" 
_direnv_hook

[a4z@mach2:~/tmp/denv] (09:16:37) (direnv)
-> echo "${precmd_functions[@]}"
_direnv_hook asdf_update_java_home iterm2_precmd update_terminal_cwd

[a4z@mach2:~/tmp/denv] (09:16:45) (direnv)
-> direnv status                
direnv exec path /usr/local/bin/direnv
DIRENV_CONFIG /Users/a4z/.config/direnv
bash_path /usr/local/bin/bash
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
Loaded RC path /Users/a4z/tmp/denv/.envrc
Loaded watch: ".envrc" - 2021-03-01T18:06:01+01:00
Loaded watch: "../../.local/share/direnv/allow/b28459eff447d085029a441ab618864a910fb8e56e091126c1df128440588e87" - 2021-03-01T18:06:05+01:00
Loaded RC allowed false
Loaded RC allowPath 
Found RC path /Users/a4z/tmp/denv/.envrc
Found watch: ".envrc" - 2021-03-01T18:06:01+01:00
Found watch: "../../.local/share/direnv/allow/b28459eff447d085029a441ab618864a910fb8e56e091126c1df128440588e87" - 2021-03-01T18:06:05+01:00
Found RC allowed true
Found RC allowPath /Users/a4z/.local/share/direnv/allow/b28459eff447d085029a441ab618864a910fb8e56e091126c1df128440588e87

[a4z@mach2:~/tmp/denv] (09:16:52) (direnv)
-> cat .envrc 

export DIRENV_LOG_FORMAT=

export USERENV=direnv

PATH_add $(pwd)

btw, I remember something I did yesterday .. it was brew update && brew upgrade , so I got very probably a new version on my disk

@wnkz
Copy link

wnkz commented Mar 2, 2021

@zimbatm Hope this can help

Can you give me these outputs:

$ echo "${chpwd_functions[@]}"
$ echo "${precmd_functions[@]}"
$ direnv status

direnv @ 2.27.0

❯ echo "${chpwd_functions[@]}"
_direnv_hook
direnv: unloading

❯ echo "${precmd_functions[@]}"
_p9k_do_nothing _zsh_highlight_main__precmd_hook omz_termsupport_precmd asdf_update_java_home _direnv_hook iterm2_precmd _p9k_precmd
direnv: unloading

❯ direnv status
direnv exec path /Users/wnkz/.asdf/installs/direnv/2.27.0/bin/direnv
DIRENV_CONFIG /Users/wnkz/.config/direnv
bash_path /usr/local/bin/bash
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
Loaded RC path /private/var/folders/bf/f_z6y6n959193x3csh_pcb0m0000gn/T/tmp.R3JeXA7j/.envrc
Loaded watch: ".envrc" - 2021-03-02T14:52:06+01:00
Loaded watch: "../../../../../../../Users/wnkz/.local/share/direnv/allow/8a7bea4e5e94cde9d028afb33899ce0387d7204504df9b4147e141c7784af65c" - 2021-03-02T14:53:26+01:00
Loaded RC allowed false
Loaded RC allowPath
Found RC path /private/var/folders/bf/f_z6y6n959193x3csh_pcb0m0000gn/T/tmp.R3JeXA7j/.envrc
Found watch: ".envrc" - 2021-03-02T14:52:06+01:00
Found watch: "../../../../../../../Users/wnkz/.local/share/direnv/allow/8a7bea4e5e94cde9d028afb33899ce0387d7204504df9b4147e141c7784af65c" - 2021-03-02T14:53:26+01:00
Found RC allowed true
Found RC allowPath /Users/wnkz/.local/share/direnv/allow/8a7bea4e5e94cde9d028afb33899ce0387d7204504df9b4147e141c7784af65c
direnv: unloading

direnv @ 2.20.0

❯ echo "${chpwd_functions[@]}"


❯ echo "${precmd_functions[@]}"
_p9k_do_nothing _zsh_highlight_main__precmd_hook omz_termsupport_precmd asdf_update_java_home _direnv_hook iterm2_precmd _p9k_precmd

❯ direnv status
direnv exec path /Users/wnkz/.asdf/installs/direnv/2.20.0/bin/direnv
DIRENV_CONFIG /Users/wnkz/.config/direnv
DIRENV_BASH /usr/local/bin/bash
Loaded RC path /var/folders/bf/f_z6y6n959193x3csh_pcb0m0000gn/T/tmp.R3JeXA7j/.envrc
Loaded watch: ".envrc" - 2021-03-02T14:52:06+01:00
Loaded watch: "../../../../../../Users/wnkz/.config/direnv/allow/4afdf4dbcd8f58b88324e959999f3e16a379518eeba90cc38bfbd30747ef78c1" - 2021-03-02T14:51:15+01:00
Loaded RC allowed false
Loaded RC allowPath
Found RC path /var/folders/bf/f_z6y6n959193x3csh_pcb0m0000gn/T/tmp.R3JeXA7j/.envrc
Found watch: ".envrc" - 2021-03-02T14:52:06+01:00
Found watch: "../../../../../../Users/wnkz/.config/direnv/allow/4afdf4dbcd8f58b88324e959999f3e16a379518eeba90cc38bfbd30747ef78c1" - 2021-03-02T14:51:15+01:00
Found RC allowed true
Found RC allowPath /Users/wnkz/.config/direnv/allow/4afdf4dbcd8f58b88324e959999f3e16a379518eeba90cc38bfbd30747ef78c1

@zimbatm
Copy link
Member

zimbatm commented Mar 3, 2021

Can you try to set precmd_functions=(_direnv_hook)?

@zimbatm
Copy link
Member

zimbatm commented Mar 3, 2021

Another thing that might have caused an issue is that the hook now traps for errors.

$ type -f _direnv_hook
_direnv_hook() {
  trap -- '' SIGINT;
  eval "$("/nix/store/9lmh6ryhdkxbi4w5cp730s24ksa14vs1-direnv-2.27.0/bin/direnv" export zsh)";
  trap - SIGINT;
}

You might try to remove the traps around it like so:

_direnv_hook() {
  eval "$("/nix/store/9lmh6ryhdkxbi4w5cp730s24ksa14vs1-direnv-2.27.0/bin/direnv" export zsh)";
}

@a4z
Copy link

a4z commented Mar 3, 2021

thanks for the tips zimbatm ! I can do that tomorrow, today on a different machine, Linux, and here so far I do not have the problem (but have simpler direnvs here also)

@a4z
Copy link

a4z commented Mar 5, 2021

@zimbatm , that precmd_functions=(_direnv_hook) seems already to work


[a4z@mach2:~/tmp/denv] (08:34:18) (direnv)
-> ls
manual_check.py
direnv: unloading                                                               

[a4z@mach2:~/tmp/denv] (08:34:20) (direnv)
-> precmd_functions=(_direnv_hook)

[a4z@mach2:~/tmp/denv] (08:34:30) (direnv)
-> ls
manual_check.py

[a4z@mach2:~/tmp/denv] (08:34:31) (direnv)
-> 

shall/can I put this simply in my .zshrc ?

Edit: Haven't found a way to put that into .zshrc so it works, when I just put it there, I end up with an empty shell window .... looks like something missing

@zimbatm
Copy link
Member

zimbatm commented Mar 10, 2021

What that means is that one of the other commands in this list is interfering with direnv:

precmd_functions=(_p9k_do_nothing _zsh_highlight_main__precmd_hook omz_termsupport_precmd asdf_update_java_home _direnv_hook iterm2_precmd _p9k_precmd)

To debug that, try removing them one after the other until the problem goes away.

@a4z
Copy link

a4z commented Mar 11, 2021

this work
precmd_functions=(_p9k_do_nothing _zsh_highlight_main__precmd_hook omz_termsupport_precmd _direnv_hook iterm2_precmd _p9k_precmd)

so the rouble maker seems to be asdf_update_java_home , and that explains why not everyone has that problem

(now what do I need to put in my zshrc to remove that ?)

@wnkz
Copy link

wnkz commented Mar 11, 2021

@a4z This added by asdf-java, you probably have something like . ~/.asdf/plugins/java/set-java-home.zsh in your .zshrc

function absolute_dir_path {
    local absolute_path
    absolute_path="$( cd -P "$( dirname "$1" )" && pwd )"
    echo "$absolute_path"
}

asdf_update_java_home() {
  local java_path
  java_path="$(asdf which java)"
  if [[ -n "${java_path}" ]]; then
    export JAVA_HOME
    JAVA_HOME="$(dirname "$(absolute_dir_path "${java_path}")")"
  fi
}

autoload -U add-zsh-hook
add-zsh-hook precmd asdf_update_java_home

source: https://github.com/halcyon/asdf-java/blob/6936bdfb3c69b4cab86bc046dd046259f724e2e9/set-java-home.zsh

@zimbatm my guess for the culprit is that cd -P ? A question remains, why is it not showing on previous versions of direnv ?

@wnkz
Copy link

wnkz commented Mar 11, 2021

Tracked it down to halcyon/asdf-java#51 ; I'll open an issue to be sure they are aware of this.

@a4z
Copy link

a4z commented Mar 11, 2021

awesome, thanks a lot for the help @wnkz !

I suggested already a fix for zsh, will do bash when I have the same problem on bash, now where the problem is know it's not a problem anymore

TheLonelyGhost added a commit to TheLonelyGhost/dotfiles that referenced this issue Mar 27, 2021
@pati08
Copy link

pati08 commented Apr 19, 2024

As far as a temporary fix, I got it to stop by just exiting everywhere that direnv was (and was supposed to be) loaded with the problematic shell. Also, BTW starship showed that direnv had not, in fact, actually unloaded.

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

No branches or pull requests

5 participants