Tarrasch / zsh-autoenv

Autoenv for zsh
702 stars 30 forks source link

Path to the .autoenv.zsh file when loaded from a parent directory #41

Closed real-milos closed 8 years ago

real-milos commented 8 years ago

Hello.

One of my use cases for zsh-autoenv is to automatically activate Python virtual environments within projects, and my issue with this particular use case is this:

Upon cd-ing to a project subdirectory, zsh-autoenv's default behaviour is to bubble up parent directories looking for a .autoenv.zsh script (which is desired). If one's found, it gets executed with $PWD set to the dir I cd'd into, not the dir where it was found. While this is a reasonable default, scripts relying on path relative to the project root (determined by the path to the .autoenv.zsh file) get broken.

My proposal is to (temporarily) export something like $AUTOENV_FILE_ENTER_PATH when looking for an env script in parent directories. This would allow us to write autoenv scripts relative to the project root without changing zsh-autoenv's default behaviour or impacting existing scripts.

Let me know what you think, or whether I'm missing something here.

Thanks in advance

Tarrasch commented 8 years ago

Hmm, doesn't shell scripts have a reasonably easy way to get the path to the currently running script? You use case seems common. Maybe @blueyed knows an ideal solution? :)

real-milos commented 8 years ago

They do - their first argument ($0) is the full path to the currently running script. I did not know this, but after a quick search I do.

Sorry for wasting your (and maybe blueyed's) time with such a pointless "issue", I could/should have figured it out on my own.

real-milos commented 8 years ago

Just for the sake of completeness: you can get the project root path with $(dirname $0), assuming that 1) the script is located in the project root 2) you have GNU coreutils installed.

blueyed commented 8 years ago

This also works:

local root=${autoenv_env_file:h}