Unmangling paths set by direnv on Windows 11
Thu 4 Jan 2024 11:25 CET
direnv
works fine on Windows 11, but if an .envrc
tries to set the PATH
, the result will
be a path in Windows format, not Unix format.1
Instead of adding eval $(direnv hook bash)
to your .bashrc
, try the following snippet:
export _unmangle_direnv_names='PATH'
_unmangle_direnv_paths() {
for k in $_unmangle_direnv_names; do
eval "$k=\"\$(/usr/bin/cygpath -p \"\$$k\")\""
done
}
eval "$(direnv hook bash | sed -e 's@export bash)@export bash)\
_unmangle_direnv_paths@')"
This modifies the output of direnv hook bash
slightly, adding code to fix path-like variables
after direnv
sets the environment up.2
The variable names to unmangle are drawn from a new variable, _unmangle_direnv_names
,
initially set to PATH
, which should contain a space-separated list of variable names.
If, in a particular .envrc
, you need path-unmangling for an additional variable, you can add
that variable’s name to _unmangle_direnv_names
. For example,
_unmangle_direnv_names="$_unmangle_direnv_names XPATH"
export PATH="$PATH:some_addition"
export XPATH="$PATH:some_addition"
will unmangle both PATH
and XPATH
.
-
While experimenting, I discovered
direnv export json
! Very nice. It’s great to see more and more tools using structured data for their inputs and outputs. ↩