-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
chore(ci): add direnv virtual env integration #13380
base: master
Are you sure you want to change the base?
Conversation
Could you explain a bit more on what are the differences of using direnv compared to |
So, direnv is quite different from just The end result is something I have found to be much more ergonomic than pure bash for "virtual" workspace environments. Here's a synopsis/workflow comparison: direnv workflow# ./workspace/.envrc file
export MY_VAR=123
export PATH=$PWD/bin:$PATH # init
$ echo $MY_VAR # => <nothing> (it's unset)
$ echo $PATH # => /usr/bin:/usr/local/bin
$ echo $MY_OTHER_VAR # => <nothing> (it's unset)
# changing to the workspace directory
$ cd ./workspace
# direnv automatically ingests ./workspace/.envrc
$ echo $MY_VAR # => 123
$ echo $PATH # => /path/to/workspace/bin:/usr/bin:/usr/local/bin
# update .envrc with a new var
$ echo 'export MY_OTHER_VAR=456' >> .envrc
# direnv detected the change to .envrc and automatically reloaded it
$ echo $MY_OTHER_VAR # => 456
# changing back out to the parent dir
$ cd ..
# direnv automatically resets variables to their prior state
$ echo $MY_VAR # => <nothing> (it's unset)
$ echo $PATH # => /usr/bin:/usr/local/bin
$ echo $MY_OTHER_VAR # => <nothing> (it's unset) pure bash workflow# ./workspace/.env file
OLD_MY_VAR=$MY_VAR; export MY_VAR=123
OLD_PATH=$PATH; export PATH=$PWD/bin:$PATH
deactivate() {
# this might not be 100% correct since we didn't check whether or not
# MY_VAR was declared, and empty/unset != undeclared
MY_VAR=$OLD_MY_VAR
PATH=$OLD_PATH
unset -f deactivate
} # init
$ echo $MY_VAR # => <nothing> (it's unset)
$ echo $PATH # => /usr/bin:/usr/local/bin
$ echo $MY_OTHER_VAR # => <nothing> (it's unset)
# changing to the workspace directory
$ cd ./workspace
# manually source env file to initialize
$ source ./.env
$ echo $MY_VAR # => 123
$ echo $PATH # => /path/to/workspace/bin:/usr/bin:/usr/local/bin
# update .env with a new var
$ echo 'export MY_OTHER_VAR=456' >> .env
# must manually reload .env to discover MY_OTHER_VAR
$ echo $MY_OTHER_VAR # => <nothing> (it's unset)
$ source ./.env
$ echo $MY_OTHER_VAR # => 456
# change out of workspace directory--this does nothing for now
$ cd ..
# explicitly reset w/ deactivate function
# OOPS!
# 1) when we re-sourced .env we updated the "old" values of
# MY_VAR and PATH, so they weren't reset properly
# 2) we forgot to follow the save/reset pattern for MY_OTHER_VAR,
# so it wasn't reset at all
$ deactivate
$ echo $MY_VAR # => 123
$ echo $PATH # => /path/to/workspace/bin:/usr/bin:/usr/local/bin
$ echo $MY_OTHER_VAR # => 456 The fact that direnv watches for changes to # /path/to/kong/.envrc
source_env_if_exists bazel-bin/build/kong-dev-venv.envrc Aside from initially setting up the virtual environment when you |
Co-authored-by: Wangchong Zhou <[email protected]>
I see, so it's like automatically handling activate and deactive the "virtual env". As right now in this PR we are just exporting env variables, will it work for just linking the existing file (Or simply |
@fffonion understood. I think some of the duplicate logic could be reduced, but I didn't want to touch Let me see if I can come up with something a little cleaner. Some refactoring of |
This adds an
.envrc
template to integrate local development with direnv workflows. I have been using a similar workflow with a self-maintained.envrc
file for some time, and it works great!It's not fully 1-1 with how the bash/fish env files work due to direnv not supporting exported functions, but I think it's fine for now. This could be solved by refactoring some things in
./scripts/dependency_services
, but I want to keep the PR small and simple.