direnv and iterm2 shell integration

Thanks for filing an issue! Please answer the questions below so I can help you.

Detailed steps to reproduce the problem:

  1. Install iterm2 shell integration (bash)
  2. Install direnv
  3. Add hooks to end of the appropriate file, in my case ~/.profile

This issue has also been raised by someone else previously with direnv devs and they have suggested raising this here, see https://github.com/direnv/direnv/issues/502

What happened:

direnv requires this line to be added to the end of your profile ( or bash_profile or bashrc etc depending on OS and your setup)

eval "$(direnv hook bash)"

This sources some direnv code and also changes the value of $PROMPT_COMMAND

$ type $PROMPT_COMMAND  
_direnv_hook

The issue is that iterm shell integration clobbers the value of $PROMPT_COMMAND with __bp_precmd_invoke_cmd;__bp_interactive_mode

If I then manually $ source ~/.profile then direnv works as expected for the duration of the bash session.

What should have happened:

It looks to me like the __bp_install_after_session_init() function ln372 in the .iterm_shell_integration.bash file makes some attempt to capture the previous $PROMPT_COMMAND and stuff it into bash-preexec's precmd_functions, which would then be run in theory by __bp_precmd_invoke_cmd however that doesn't appear to be happening.

As a (possibly broken) workaround I've manually modified my copy of .iterm_shell_integration.bash, changing ln356 to PROMPT_COMMAND="__bp_precmd_invoke_cmd; __bp_interactive_mode; _direnv_hook", i.e. I appended _direnv_hook.

This does appear to work but I feel like this is a guess-hack at best.

Is there a way to provide/hook custom functions to the iterm2 shell integration so they run in precmd?

I also define a custom iterm2_generate_ps1 function in my profile which is working, which to me is shows there is precedent for hooking custom things into the shell integration.

Edited by davehowell