Don't use asdf if there is no .tool-versions file?
Asked Answered
M

2

6

I installed node.js:

brew install node

I also installed asdf with node.js plugin. Is there any way to use node.js installed via Homebrew globally if there is no .tool-versions file?

Right now I am getting an error if this file does not exist:

No version is set for command node
Consider adding one of the following versions in your config file at
nodejs 19.0.0
nodejs 19.0.1
nodejs 19.1.0
nodejs 19.2.0

I don't want to use asdf globally. Just for few projects.

Mountainside answered 3/12, 2022 at 18:36 Comment(1)
As a work-around, it is possible to create a ~/.tool-versions file with a one line content nodejs 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
P
3

Using Bash you can use PROMPT_COMMAND to check if the .tool-versions file exists in the current folder and add or remove .asdf folders from the PATH.

Just add this to your .bashrc:

conditional_asdf() {
  if [ ! -f "$PWD/.tool-versions" ]; then
    export PATH=`echo $PATH | sed -E 's/(.*)\.asdf([^:]*)://'`
  else
    export PATH="${HOME}/.asdf/shims:${HOME}/.asdf/bin:${PATH}"
  fi
}

PROMPT_COMMAND="conditional_asdf; $PROMPT_COMMAND"
Padua answered 25/8, 2023 at 22:22 Comment(0)
W
0

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.

Wardmote answered 4/7, 2023 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.