It doesn't completely answer your question, but I've stumbled upon the same problem and found the following workaround for using asdf
with some projects, but not others:
Instead of sourcing the asdf scripts for every shell, I wrap them in a condition. ~/.bashrc
(on Linux & Bash, in your case it will probably be ${ZDOTDIR:-~}/.zshrc
) might then contain the following:
# ASDF tool version manager
if [ -n "$WITH_ASDF" ]; then
. "$HOME/.asdf/asdf.sh";
. "$HOME/.asdf/completions/asdf.bash";
else
alias asdf="WITH_ASDF='true' /bin/bash";
fi;
With this asdf is initially not loaded at all and I can use NVM like before (or, in your case, the system's nodeJS). By simply typing asdf
into the terminal, a new shell is invoked with asdf loaded. To return to the non-asdf shell a simple exit
(or Ctrl+D) will do.
$> which node
/home/andi/.nvm/versions/node/v16.19.0/bin/node
$> asdf
$> which node
/home/andi/.asdf/shims/node
$> exit
(Obviously you'll have to adjust the above for your specific environment, i.e. Homebrew and probably ZSH, based on these code samples from asdf's getting started page.)
If you prefer, you could turn this principle around and make asdf the default (with some extra steps included that only apply if you use NVM) (untested):
# ASDF tool version manager
if [ -z "$NO_ASDF" ]; then
. "$HOME/.asdf/asdf.sh";
. "$HOME/.asdf/completions/asdf.bash";
# This alias could also be called "nvm"
alias noasdf="NO_ASDF='true' /bin/bash";
# This "else" block is optional if you only want to use the system's nodeJS
else
# Node version manager
export NVM_DIR="$HOME/.nvm";
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm;
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"; # This loads nvm bash_completion
fi;
You could probably build on that and have a script auto-evaluate whether a .tool-versions
file exists in the cwd at any given time and then automatically start or exit the shell, but that's quite a bit beyond my shell-fu.
~/.tool-versions
file with a one line contentnodejs system
. According to the documentation, this "causes asdf to passthrough to the version of the tool on the system that is not managed by asdf". Which should have a similar effect to what you are trying to achieve. – Trucker