vscode-direnv
vscode-direnv copied to clipboard
Allow user to provide path to direnv
There is a bit of a chicken-egg problem in getting this extension to work: I have a system for managing environment variables (including PATH) but it's built around using direnv and .envrc files, which of course is why I'm interested in using this extension. But since the desired environment isn't loaded when VS Code starts, direnv isn't on PATH yet at that point.
To bootstrap the process of setting environment variables as desired, it'd be great if I could help the extension find direnv by providing the absolute path to the executable.
Or maybe there is another way? (e.g. does VS Code include a setting that lets you make sure a directory is included in PATH, and once it is, the extension will be able to use that?) If so, it'd be helpful to be pointed toward this in an error message or description etc.
Update: I learned that VS Code does run .profile
in order to set PATH
how the user expects. But direnv
's mechanism for setting variables is triggered by a prompt (i.e. running the components of $PROMPT_COMMAND
). So with this .profile
script, PATH
doesn't contain $homebrew_bin_dir
when the script is finished.
homebrew_bin_dir=$HOME/brew/bin
direnv_path=$homebrew_bin_dir/direnv
eval "$($direnv_path hook bash)"
I thought maybe testing for a noninteractive shell would help differentiate. But when I did that, VS Code didn't run the 'noninteractive' branch of the if statement. So apparently it runs the script with the indicators of interactive mode (the 'i' flag and a value for $PS
) set.
I wound up adding this to .profile
:
# Ugly hack for VS Code direnv extension
if [[ -n "${VSCODE_PID+set}" && $SHLVL -lt "2" ]]; then
# This is being run by Visual Studio Code (and we're not in a terminal launched
# within VS Code) so there won't be a prompt that would trigger loading env
# vars, and homebrew_bin_dir hasn't yet been added to PATH. Add it now so
# that the VS Code direnv extension can find direnv
PATH=$homebrew_bin_dir:$PATH
fi
This is an old issue, but I thought I'd comment - if you're using VS Code remote (WSL or SSH), there's a server-env-setup file you can use to set things like this.